EF 6 Code First不创建数据库

时间:2014-11-18 16:05:37

标签: asp.net-mvc-4 ef-code-first database-connection connection-string code-first

我搜索了很多解决方案,但我无法理解为什么数据库不会创建?我的类和上下文类非常简单。

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>

0 个答案:

没有答案
相关问题