1

更新:原来我已经(藏在我的POCO对象)有一个抽象类型的属性。删除此属性解决了这个问题。的DbContext初始化失败模型的第一个数据库


我试图与实体框架模型的第一个场景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: Value cannot be null. Parameter name: entitySet]
System.Lazy 1.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() +62 System.Data.Entity.Internal.Linq.InternalSet 1.get_Provider() +15 System.Data.Entity.Infrastructure.DbQuery 1.System.Linq.IQueryable.get_Provider() +13 System.Linq.Queryable.Where(IQueryable 1 source, Expression 1 predicate) +63

然而,我第一次构建,这是具体的错误:

[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

+0

非常感谢!我疯狂地想知道为什么我的分贝已经停止工作。就在昨天,我添加了一个抽象类作为属性给我的一个域对象。回到我认为的界面! – nialljsmith 2011-06-21 11:39:40

回答

-1

// Apply forced recreation tactics for now 
     DbDatabase.SetInitializer<BudgetnetDatabase>(new CreateDatabaseIfNotExists<BudgetnetDatabase>()); 

只应在一次项目中应用。我建议你将这些代码移出课程。

有更多的分贝范围内?

任何配置?

是有可能看到BudgetnetDatabase?

,我们还需要看到POCO对象。

是数据库中获取创作呢?

+0

SetInitializer实际上是在静态构造函数中。我移动它看看是否是这个问题。我有6个POCO课程。它们每个都包含公共的基元/字符串属性。使用[http://blogs.msdn.com/b/alexj/archive/2009/06/05/tip-23-how-to-fake-enums-in-ef-4]中的技术打包的单个枚举类型属性.aspx](Alex James的博客)。关系式都是“public virtual ICollection ”的形式,每个PocoObject类在上下文中都有相应的'DbSet'属性。数据库没有被创建。 – ulrikj 2011-02-26 13:52:02