DbContext 无法初始化模型优先数据库
更新:事实证明我有(隐藏在我的 POCO 对象中)一个具有抽象类型的属性。删除此属性可以解决该问题。
我正在尝试使用实体框架 4 ctp 5 的模型优先场景从我的模型生成我的数据库(当前使用 SQL Server CE4 后端,但我可以在 Sql Server 2008 后端上重现)。
我想我可能以某种方式弄乱了我的模型定义,但我似乎无法弄清楚如何做,并且错误消息让我一无所知。
我的 DbContext 对象设置如下:
public class MyDb : DbContext
{
public MyDb()
{
// Apply forced recreation tactics for now
DbDatabase.SetInitializer<MyDb>(new CreateDatabaseIfNotExists<MyDb>());
}
public DbSet<UserAccount> UserAccounts { get; set; }
public DbSet<OtherData> OtherDatas { get; set; }
// etc
}
当我查询它时(如下所示):
MyDb db = new MyDb();
var matchingAccount = from user in db.UserAccounts where user.Email == email select user;
return matchingAccount.SingleOrDefault();
我收到以下错误:
[ArgumentNullException:值不能 为空。参数名称:entitySet]
System.Lazy1.get_Value() +9591079
1.Initialize() +62 System.Data.Entity.Internal.Linq.InternalSet
System.Data.Entity.Internal.LazyInternalContext.InitializeContext() +371 System.Data.Entity.Internal.InternalContext.Initialize() +16 System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(类型 实体类型)+15
System.Data.Entity.Internal.Linq.InternalSet1.get_Provider() +15 System.Data.Entity.Infrastruct.DbQuery
1.System.Linq.IQueryable.get_Provider() +13 System.Linq.Queryable.Where(IQueryable1 源,表达式
1 谓词)+63
但是,第一次构建时,这是具体错误:
[ArgumentNullException:值不能 为空。参数名称:entitySet]
System.Data.Entity.ModelConfiguration.Utilities.RuntimeFailureMethods.ReportFailure(ContractFailureKind ContractFailureKind,字符串 用户消息,字符串条件文本, 异常内部异常)+970860
System.Data.Entity.ModelConfiguration.Edm.Db.Mapping.DbDatabaseMappingExtensions.GetEntitySetMapping(DbDatabaseMapping 数据库映射、EdmEntitySet 实体集)+147
System.Data.Entity.ModelConfiguration.Edm.Services.EntityTypeMappingGenerator.Generate(EdmEntityType 实体类型、DbDatabaseMapping 数据库映射)+206
System.Data.Entity.ModelConfiguration.Edm.Services.DatabaseMappingGenerator.GenerateEntityTypes(EdmModel 模型、数据库映射 数据库映射)+253
System.Data.Entity.ModelConfiguration.Edm.Services.DatabaseMappingGenerator.Generate(EdmModel 型号)+168
System.Data.Entity.ModelConfiguration.Edm.EdmModelExtensions.GenerateDatabaseMapping(EdmModel 模型、DbProviderManifest 提供商清单)+233
System.Data.Entity.ModelConfiguration.ModelBuilder.Build(DbProviderManifest 提供者清单、DbProviderInfo 提供者信息,布尔验证模型) +280 System.Data.Entity.ModelConfiguration.ModelBuilder.Build(DbConnection 提供商连接)+173
System.Data.Entity.Internal.LazyInternalContext.CreateModel() +61
Update: It turns out I had (hidden away in my POCO objects) a property with an abstract type. Removing this property solves the issue.
I'm trying to have a model-first scenario with entity framework 4 ctp 5 generate my database (currently using a SQL Server CE4 backend, but I can reproduce on a Sql Server 2008 backend) from my model.
I think I might have messed up my model definition somehow, but I can't seem to figure out how and the error message leaves me none the wiser.
My DbContext object is set up as such:
public class MyDb : DbContext
{
public MyDb()
{
// Apply forced recreation tactics for now
DbDatabase.SetInitializer<MyDb>(new CreateDatabaseIfNotExists<MyDb>());
}
public DbSet<UserAccount> UserAccounts { get; set; }
public DbSet<OtherData> OtherDatas { get; set; }
// etc
}
When I query it (as follows):
MyDb db = new MyDb();
var matchingAccount = from user in db.UserAccounts where user.Email == email select user;
return matchingAccount.SingleOrDefault();
I get the following error:
[ArgumentNullException: Value cannot
be null. Parameter name: entitySet]
System.Lazy1.get_Value() +9591079
1.Initialize()
System.Data.Entity.Internal.LazyInternalContext.InitializeContext()
+371 System.Data.Entity.Internal.InternalContext.Initialize()
+16 System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type
entityType) +15
System.Data.Entity.Internal.Linq.InternalSet
+62 System.Data.Entity.Internal.Linq.InternalSet1.get_Provider()
1.System.Linq.IQueryable.get_Provider()
+15 System.Data.Entity.Infrastructure.DbQuery
+13 System.Linq.Queryable.Where(IQueryable1
1 predicate) +63
source, Expression
However, the first time I build, this is the specific error:
[ArgumentNullException: Value cannot
be null. Parameter name: entitySet]
System.Data.Entity.ModelConfiguration.Utilities.RuntimeFailureMethods.ReportFailure(ContractFailureKind
contractFailureKind, String
userMessage, String conditionText,
Exception innerException) +970860
System.Data.Entity.ModelConfiguration.Edm.Db.Mapping.DbDatabaseMappingExtensions.GetEntitySetMapping(DbDatabaseMapping
databaseMapping, EdmEntitySet
entitySet) +147
System.Data.Entity.ModelConfiguration.Edm.Services.EntityTypeMappingGenerator.Generate(EdmEntityType
entityType, DbDatabaseMapping
databaseMapping) +206
System.Data.Entity.ModelConfiguration.Edm.Services.DatabaseMappingGenerator.GenerateEntityTypes(EdmModel
model, DbDatabaseMapping
databaseMapping) +253
System.Data.Entity.ModelConfiguration.Edm.Services.DatabaseMappingGenerator.Generate(EdmModel
model) +168
System.Data.Entity.ModelConfiguration.Edm.EdmModelExtensions.GenerateDatabaseMapping(EdmModel
model, DbProviderManifest
providerManifest) +233
System.Data.Entity.ModelConfiguration.ModelBuilder.Build(DbProviderManifest
providerManifest, DbProviderInfo
providerInfo, Boolean validateModel)
+280 System.Data.Entity.ModelConfiguration.ModelBuilder.Build(DbConnection
providerConnection) +173
System.Data.Entity.Internal.LazyInternalContext.CreateModel()
+61
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
应该
只在您的项目中应用一次。我建议你将这段代码移出类。
您的数据库上下文还有更多内容吗?
有什么配置吗?
可以看到 BudgetnetDatabase 吗?
我们还需要查看 poco 对象。
数据库正在创建吗?
the
should only be applied once in your project. i suggest you move this code out of the class.
is there more to your db context?
any configuration?
is it possible to see BudgetnetDatabase ?
we will also need to see the poco objects.
is the database getting create at all?