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
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
1.Initialize()
+62System.Data.Entity.Internal.Linq.InternalSet1.get_Provider()
+15 System.Data.Entity.Infrastructure.DbQuery
1.System.Linq.IQueryable.get_Provider()
+13 System.Linq.Queryable.Where(IQueryable1
source, Expression
1谓词)+63
但是,我第一次构建时,这是具体的错误:
[ArgumentNullException:值不能
是空的。参数名称:entitySet]
System.Data.Entity.ModelConfiguration.Utilities.RuntimeFailureMethods.ReportFailure(ContractFailureKind
contractFailureKind,String
userMessage,String conditionText,
异常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
型号)+168
System.Data.Entity.ModelConfiguration.Edm.EdmModelExtensions.GenerateDatabaseMapping(EdmModel
模型,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
没有找到相关结果
已邀请:
1 个回复
僻朵庙惩竣
应该只在项目中应用一次。我建议你把这个代码移出课堂。 您的数据库上下文还有更多内容吗? 任何配置? 是否可以看到BudgetnetDatabase? 我们还需要看到poco对象。 数据库是否正在创建?