.netentity-framework-4entity-framework-ctp5ef-model-first

DbContext fails to initialize Model-first database


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
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.InternalSet1.get_Provider() +15 System.Data.Entity.Infrastructure.DbQuery1.System.Linq.IQueryable.get_Provider() +13 System.Linq.Queryable.Where(IQueryable1 source, Expression1 predicate) +63

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


Solution

  • the

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

    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?