In one of our .Net Applications we have one big DBContext with all entities. In order to speed up the instantiation of the DBContext, I'm trying to break the DBContext into smaller chunks.
My DBContext class looks something like this:
public class DALContext : DBContext
{
static DALContext()
{
Database.SetInitializer<DALContext>(null);
}
public DALContext(string connectionString)
: base(connectionString)
{
this.Configuration.ProxyCreationEnabled = false;
this.Configuration.LazyLoadingEnabled = false;
this.Configuration.AutoDetectChangesEnabled = true;
IObjectContextAdapter objectContextAdapter = this as IObjectContextAdapter;
if (objectContextAdapter.ObjectContext != null)
{
objectContextAdapter.ObjectContext.CommandTimeout = 300;
}
}
public DbSet<tblMyEntity> tblMyEntities { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Configurations.Add(new tblMyEntityMap());
}
}
Trying to initialize the DBContext in a UnitTest results in the following error with a Message:
System.Data.Entity.Edm.EdmEntityType: : EntityType 'tblPropertyValue' has no key defined. Define the key for this EntityType.
System.Data.Entity.Edm.EdmEntitySet: EntityType: EntitySet 'tblPropertyValues' is based on type 'tblPropertyValue' that has no keys defined.
And a stacktrace that looks like this:
bei System.Data.Entity.ModelConfiguration.Edm.EdmModelExtensions.ValidateAndSerializeCsdl(EdmModel model, XmlWriter writer)
bei System.Data.Entity.DbModelBuilder.Build(DbProviderManifest providerManifest, DbProviderInfo providerInfo)
bei System.Data.Entity.DbModelBuilder.Build(DbConnection providerConnection)
bei System.Data.Entity.Internal.LazyInternalContext.CreateModel(LazyInternalContext internalContext)
bei System.Data.Entity.Internal.RetryLazy`2.GetValue(TInput input)
bei System.Data.Entity.Internal.LazyInternalContext.InitializeContext()
bei System.Data.Entity.Internal.InternalContext.ForceOSpaceLoadingForKnownEntityTypes()
bei System.Data.Entity.DbContext.System.Data.Entity.Infrastructure.IObjectContextAdapter.get_ObjectContext()
bei DAL.DALContext..ctor(String connectionString)
The Problem where I get lost, is that the DBSet with the class tblPropertyValue
isn't declared in my DBContext.
The EntitySet 'tblPropertyValues' is declared in this other big DBContext, which uses the same DBConnection-String. But the other DBContext isn't instantiated by my UnitTest.
What am I doing wrong?
Update: the problem was caused by a base class of all entities which had a method returning all child classes for the known-types
attribute for WCF.
boutta, I am really trying to help you. The exception you are seeing is a very common EF exception. It is telling you that EF can’t figure out the mapping for all of the properties on the object. You have not provided EF the object mapping, so it is trying to do the mapping for you and it can’t. The a solution to your problem is to create a mapping class. See https://msdn.microsoft.com/en-us/data/jj591617.aspx
Do the following to fix and your class will work:
The code would look like this
public class tblMyEntityMapper : EntityTypeConfiguration<tblMyEntityMap>
{
public tblMyEntityMapper()
{
this.Ignore(t => t. tblPropertyValues);
}
}
And in change the DbContext to this
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Configurations.Add(new tblMyEntityMapper());
}
EF is very forgiving and it will auto map the rest of your properties if they match the column names.