Asp.net 代码优先-数据库isn';t创造
我试图在ASP.NET中创建一个简单的项目,“代码优先” 因此,我创建了一个类EFDbContext:Asp.net 代码优先-数据库isn';t创造,asp.net,sql-server,entity-framework,ef-code-first,Asp.net,Sql Server,Entity Framework,Ef Code First,我试图在ASP.NET中创建一个简单的项目,“代码优先” 因此,我创建了一个类EFDbContext: using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Data.Entity; using System.Data.Entity.ModelConfiguration.Conventions; namespace MVCRubenWouters.Mod
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions;
namespace MVCRubenWouters.Models
{
public class EFDbContext: DbContext
{
public EFDbContext() : base("name=rubenwoutersdbCF")
{
Database.SetInitializer<EFDbContext>(new EFDbInitializer());
}
public DbSet<Types> Types { get; set; }
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.Data.Entity;
使用System.Data.Entity.ModelConfiguration.Conventions;
命名空间MVCRubenWouters.Models
{
公共类EFDbContext:DbContext
{
public EFDbContext():base(“name=rubenwoutersdbCF”)
{
SetInitializer(新的EFDbInitializer());
}
公共数据库集类型{get;set;}
}
}
并在“web.config”中添加了连接字符串
<connectionStrings>
<add name="rubenwoutersdbCF" connectionString="Data Source=.\SQLSERVER2012;Initial Catalog=rubenwoutersdbCF;Integrated Security=True;MultipleActiveResultSets=true"
providerName="System.Data.SqlClient" />
</connectionStrings>
然后,我创建了一个类“EFDbInitializer”来向数据库添加一些内容:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
namespace MVCRubenWouters.Models
{
public class EFDbInitializer: DropCreateDatabaseAlways<EFDbContext>
{
protected override void Seed(EFDbContext context)
{
Types t1 = new Types()
{
PK_TypeNr = 1,
TypeKeuken = "Belgisch",
TypeZaak = "Restaurant",
Vegetarisch = false
};
context.Types.Add(t1);
context.SaveChanges();
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.Data.Entity;
命名空间MVCRubenWouters.Models
{
公共类EFDbInitializer:DropCreateDatabaseAlways
{
受保护的重写无效种子(EFDbContext上下文)
{
类型t1=新类型()
{
PK_TypeNr=1,
TypeKeuken=“Belgisch”,
TypeZaak=“餐厅”,
Vegetarisch=false
};
context.Types.Add(t1);
SaveChanges();
}
}
}
当我运行项目并转到SQL server management studio(并刷新)时,那里没有新的数据库
我在这里做错了什么吗?:/我建议您在SQL server中创建数据库,构建表,然后使用代码填充它们。在我的系统上运行的方式是在应用程序中强制数据库启动() 试试这个:
Database.SetInitializer(new CreateDatabaseIfNotExists<EFDbContext>());
var context = new EFDbContext("Data Source=(local);Integrated Security=SSPI;Initial Catalog=myDB;");
context.Database.Initialize(true);
Database.SetInitializer(新的CreateDatabaseIfNotExists());
var-context=new-EFDbContext(“数据源=(本地);集成安全性=SSPI;初始目录=myDB;”);
context.Database.Initialize(true);
我认为永远不会调用seed方法,以确保设置制动点
您可以开发自定义初始化器,如下链接所示:
然后在应用程序_Start()中调用它
Database.SetInitializer(新的EFDbInitializer());
我认为代码首先意味着通过代码创建数据库(这是学校分配的任务,所以需要这样:/)@user1418016问题首先与代码有关,您的答案没有任何意义。您是否将Mgmt Studio连接到\SQLSERVER2012
实例?尝试调用实体框架,如果有问题,您应该会得到一个异常。尝试调用此:new EFDbContext().Types.ToList()
@Dan我应该把它放在哪里?@RW24 Anywhere就可以了,Page_Load方法,任何有效的方法。@Dan在MVCRubenWouters.dll中发生“System.StackOverflowException”类型的未处理异常
Database.SetInitializer<EFDbContext>(new EFDbInitializer());