我可以看到我的解决方案中有2个项目:
我想首先使用代码来创建我的数据库,所以我创建模型的代码是:
namespace CMSDataLayer
{
public class CMSDataContext : DbContext
{
public CMSDataContext()
: base("DefaultConnection")
{
this.Configuration.LazyLoadingEnabled = false;
this.Configuration.ProxyCreationEnabled = false;
Database.SetInitializer(
new MigrateDatabaseToLatestVersion<CMSDataContext, CMSMigrationsConfiguration>()
);
}
public DbSet<table1> Topics { get; set; }
}
}
和
public class CMSMigrationsConfiguration : DbMigrationsConfiguration<CMSDataContext>
{
public CMSMigrationsConfiguration()
{
this.AutomaticMigrationDataLossAllowed = true;
this.AutomaticMigrationsEnabled = true;
}
}
}
我将连接字符串添加到MVC项目webconfig
运行数据库后没有创建。我在CMSDataContext类中设置了一个断点,但是断点从未调用。为什么?
我是EF代码的新手。
最好的问候
答案 0 :(得分:0)
据我所知,它将在使用之前创建数据库。 尝试将实体添加到数据库,然后查看它是否创建它。
如果要添加一些虚拟数据,可以使用种子功能: seeding tutorial
答案 1 :(得分:0)
在您的项目中,您没有手动建立任何数据库连接,如何显示:
View -> Server Explorer Right click on Data Connections and select Add Connection…
如果在需要选择Microsoft SQL Server作为数据源之前尚未从服务器资源管理器连接到数据库
Here是一篇文章,解释了如何添加EF