我搜索了很多解决方案,但我无法理解为什么数据库不会创建?我的类和上下文类非常简单。
public class News
{
[Key]
public int NewsID { get; set; }
public string NewsHeader { get; set; }
public string News { get; set; }
public int CategoryID;
public virtual Category Category { get; set; }
}
public class Category
{
[Key]
public int CategoryID { get; set; }
public string CategoryName{ get; set; }
public virtual ICollection<News> Newss { get; set; }
}
public class NewsContext: DbContext
{
public NewsContext(): base("NewsContext")
{
Database.SetInitializer<NewsContext>(new CreateDatabaseIfNotExists<NewsContext>());
}
public DbSet<News> Newss { get; set; }
public DbSet<Category> Categories { get; set; }
}
我的连接字符串就在
之下 <connectionStrings>
<add name="NewsContext" connectionString="Data Source=nevra\sqlexpress;Initial Catalog=NewsContext;Integrated Security=True;" providerName="System.Data.SqlClient" />
</connectionStrings>