[英]Entity Framework is not creating database or tables
Verified that SQL Express was running. 验证SQL Express是否正在运行。
Using Visual Studio 2015 使用Visual Studio 2015
/App_code/departments.cs /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 /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 /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 dbContextRun.cs
namespace cfsEnergyManagement.App_Code
{
public class dbContextRun
{
cfsEnergyDb CfsEnergyDb = new cfsEnergyDb();
}
}
Connection String: web.config 连接字符串:web.config
<connectionStrings>
<add name="cfsEnergyDb" connectionString="server=SQLEXPRESS;integrated security=SSPI;database=cfsEnergy" providerName="System.Data.SqlClient" />
</connectionStrings>
Because you are not instantiating cfsEnergyDb class. 因为您没有实例化cfsEnergyDb类。 Entity framework only and only create database when you will try to access any data for the first time from database table(any db queries).
仅当您尝试从数据库表(任何数据库查询)首次访问任何数据时,仅实体框架,并且仅创建数据库。 So try to access data from table and EF will create all the tables for you in database(specified in connection string).
因此,尝试访问表中的数据,EF将在数据库中为您创建所有表(在连接字符串中指定)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.