[英]Entity Framework CTP4: where to put SetInitializer?
I am attempting to add Entity Framework, code first, to an MVC application that's been running with test data, using the CTP4 preview. 我试图使用CTP4预览将实体框架(代码优先)添加到使用测试数据运行的MVC应用程序。
I am currently getting this error: 我目前收到此错误:
The model backing the 'SchedulerContext' context has changed since the database was created. 自创建数据库以来,支持“SchedulerContext”上下文的模型已更改。 Either manually delete/update the database, or call Database.SetInitializer with an IDatabaseInitializer instance.
手动删除/更新数据库,或使用IDatabaseInitializer实例调用Database.SetInitializer。 For example, the RecreateDatabaseIfModelChanges strategy will automatically delete and recreate the database, and optionally seed it with new data.
例如,RecreateDatabaseIfModelChanges策略将自动删除并重新创建数据库,并可选择使用新数据对其进行种子设定。
I do not want to generate a database at all, as I already have a database. 我根本不想生成数据库,因为我已经有了一个数据库。 So I tried adding the following to the SchedulerContext constructor:
所以我尝试将以下内容添加到SchedulerContext构造函数中:
Database.SetInitializer<SchedulerContext>(new CreateDatabaseOnlyIfNotExists<SchedulerContext>());
which had no effect at all -- I got the same error the next time it ran. 它完全没有效果 - 下次运行时我得到了同样的错误。 The error seems to occur when it is executing a LINQ statement that accesses the database -- the first, I think.
当它执行访问数据库的LINQ语句时,似乎发生了错误 - 我认为第一个。
Where should I put this statement, or is this statement the answer to this problem at all? 我应该把这个陈述放在哪里,或者这个陈述是否是这个问题的答案?
I simply glossed over the fact you already have a database and don't want to create another one...in that case the answer is to put this in your SchedulerContext class 我只是掩饰你已经有一个数据库, 不想再创建一个事实......在这种情况下,答案是把这个在您的SchedulerContext类
protected override void OnModelCreating(System.Data.Entity.ModelConfiguration.ModelBuilder modelBuilder) {
modelBuilder.IncludeMetadataInDatabase = false;
}
You usually put it in the Global.asax 您通常将它放在Global.asax中
protected void Application_Start() {
Database.SetInitializer<SchedulerContext>(new CreateDatabaseOnlyIfNotExists<SchedulerContext>());
}
Note that it will only be initialized on first use of the data context. 请注意,它仅在首次使用数据上下文时初始化。
public class DataContextInitializer : CreateDatabaseOnlyIfNotExists<SchedulerContext> {
protected override void Seed(SchedulerContext context) {
}
}
Then you modify the SetInitializer like so. 然后像这样修改SetInitializer。
System.Data.Entity.Infrastructure.Database.SetInitializer<SchedulerContext>(new DataContextInitializer());
顺便说一句,从CTP5开始,这是在Global.asax中设置初始化程序的正确方法
protected void Application_Start() { System.Data.Entity.Database.DbDatabase.SetInitializer<NerdDinners>(new System.Data.Entity.Database.DropCreateDatabaseIfModelChanges<NerdDinners>()); }
In EF 4.1 在EF 4.1中
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<IncludeMetadataConvention>();
}
The problem is that ADO.NET team creates the connection as 问题是ADO.NET团队创建了连接
connectionBuilder = new SqlConnectionStringBuilder(sqlConnection.ConnectionString)
{
InitialCatalog = "master",
AttachDBFilename = string.Empty,
};
BUT sqlConnection.ConnectionString is the pure connection string specified in the application config file but without password! 但是sqlConnection.ConnectionString是应用程序配置文件中指定的纯连接字符串,但没有密码! due to the security reasons, I think ... so then opening such connection fails !
由于安全原因,我认为......所以打开这样的连接失败了 !
The only possible solution I found is to use integrated security, which means to set the connection string property Integrated Security=SSPI 我发现唯一可行的解决方案是使用集成安全性,这意味着设置连接字符串属性Integrated Security = SSPI
And we need to wait while there will be a patch or CTP5 ;o) 我们需要等待有补丁或CTP5; o)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.