已验证SQL Express正在运行。
使用Visual Studio 2015
/App_code/departments.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace cfsEnergyManagement.App_Code
{
public class department
{
public string ID { get; set; }
public string title { get; set; }
public List<utilityUse> utilityUse { get; set; }
}
}
/App_code/utilityUse.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace cfsEnergyManagement.App_Code
{
public class utilityUse
{
public string utilityID { get; set; }
public string title { get; set; }
public string year { get; set; }
public string month { get; set; }
public int kiloWattHour { get; set; }
public int tonHour { get; set; }
public int kiloPoundsHour { get; set; }
public int netCost { get; set; }
}
}
/App_Code/dbContext.cs
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
namespace cfsEnergyManagement.App_Code
{
public class cfsEnergyDb: DbContext
{
public DbSet<department> departments { get; set; }
public DbSet<utilityUse> utilityUses { get; set; }
}
}
dbContextRun.cs
namespace cfsEnergyManagement.App_Code
{
public class dbContextRun
{
cfsEnergyDb CfsEnergyDb = new cfsEnergyDb();
}
}
连接字符串:web.config
<connectionStrings>
<add name="cfsEnergyDb" connectionString="server=SQLEXPRESS;integrated security=SSPI;database=cfsEnergy" providerName="System.Data.SqlClient" />
</connectionStrings>
答案 0 :(得分:1)
因为您没有实例化 cfsEnergyDb 类。实体框架,仅在您尝试从数据库表(任何数据库查询)首次访问任何数据时创建数据库。因此,尝试从表中访问数据,EF将在数据库中为您创建所有表(在连接字符串中指定)。