Entity Framework 6 code first DbContext issue with multiple DbContext ( .NET 6 ) - entity-framework

I am getting below error
System.InvalidOperationException : The default DbConfiguration instance was used by the Entity Framework before the 'DbConfig' type was discovered. An instance of 'DbConfig' must be set at application start before using any Entity Framework features or must be registered in the application's config file.
expecting same DbConfig instance should be used for the multiple dbcontext instead of using default instance of the entity framework

Related

Entity Framework 6.0: how to use edmx context when connection is configured in code?

I use Visual Studio 2013, Entity Framework 6.0.
So far I had the connection string in the app config, and that worked.
The DB context was created by adding a "ADO .Net Entity Data Model" with the option "EF designer from database"
But now I need to move it to code since it will depend on user input.
I followed this example for the connection string and my DB successfully connects
http://msdn.microsoft.com/library/vstudio/bb738533
But now I need to use the connection with my database context, and I can't get this done.
Say my data model is named MyDB, then I had under MyDB.edmx the MyDB.Context.cs, and there the MyDBEntities class derived from DbContext.
I re-created the data model, but this time did not select the option to store the connection string in the app config. First difference in result is that MyDBEntities is now only called Entities. Why that?
I see that the DbContext has a constructor which accepts a EntityConnection.
I was able to create a DbContext with the connection, but that is not linked with the datatypes under MyDB.edmx. And MyDBEntities ( or just Entities after the change ) has no constructor accepting an EntityConnection.
So how can I use the generated edmx model but configure/open the connection at runtime?
Found it. Ok so in EF 6.0 the DbContext no longer has a constructor that accepts a connection, but there is one that accepts a connection string.
Just modify the generated code and add the following constructor:
public Entities(String connectString) : base(connectString) { }
Then directly pass the connection string and it will work.

Distribute entity classes over multiple projects (EF Database First)

I am using an Entity Framework database first approach. Is it possible to use an entity from another assembly in a DbContext?
I have a central database layer project containing the DbContext. It should reference entities from different projects/assemblies.
My idea was the following which does not work:
Create an *.edmx file in a central database project and use DbContext code generation to generate entity classes.
Move each entity class to their correct project. For example the User entity class will be moved to the "Authentication" class library.

Entity Framework 4.3 to Entity Framework 5 Mapping Exception

I'm in the process of migrating a project from Entity Framework 4.3 running on .net 4 to Entity Framework 5 running on .net 4.5. Without making any changes, when I try to run the project the code-first model configuration fails with a System.Data.MappingException with the message:
(495,10) : error 3034: Problem in mapping fragments starting at lines 495, 536:Two entities with different keys are mapped to the same row. Ensure these two mapping fragments do not map two groups of entities with different keys to the same group of rows.
[5 other similar paragraphs removed]
The message does not specify which entity or relationship is causing the problem and my model is reasonably complex. Is there any way that I can get some more helpful information to to make it easier to diagnose the problem?
Ladislav was correct to suggest an inheritance problem. It looks like Entity Framework 4.3 and Entity Framework 5 behave a little differently when it comes to code-first Table Per Hierarchy configurations.
In this case I had four derived types each with their own configuration class derived from EntityTypeConfiguration<T>. The base, abstract type did not have a configuration registered with the model builder. This was not a problem under EF 4.3 which simply created a table named after the base type with a 'Discriminator' column to distinguish between the types.
To get the same behaviour with EF 5 it was necessary to create an empty configuration class
public class MyBaseConfiguration : EntityTypeConfiguration<MyBase>
{
// Nothing happening here
}
and then register it with the model builder
public class MyContext : DbContext
{
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Configurations.Add(new MyBaseConfiguration());
// Add configurations for derived and other types as normal
}
}

Entity Framework 4.1 set EntityState on derived class throws Exception

I am facing a problem with EF 4.1. I am trying to Add a detached object to the DbContext. Problem is it is not the emd mapped object, but derived from it. Changing the mapping is not an option as some teams are using the model with the regular mapped BL-classes, but my project created a derived model for UI stuff. Even with casting I always receive a
InvalidOperationException ("Mapping and metadata information could not be found for EntityType ...").
What I want is EF to treat this as the base class and put the object into the DbSet of the BaseClass. The current EF code is:
Context.Entry(object).State = EntityState.Added
But I am open for other suggestions, even
via IObjectContextAdapter, as long as it can save the Base and the Supertype. This should be simple, right?! Mapping to a new Base-class instance is not good idea as the main objects temporary Id would not be updated after saving...
Thanks!
As I know this is not possible. You cannot use derived class from the entity instead of the entity. You must either map derived class as well or create new instance of the entity for persistence and copy all fields from your derived class instance to the entity instance.

Asp.NET MVC 2 EF4 Dynamic Proxies

Im working on Asp.NET MVC 2 project with EF 4(POCO, Lazy disabled) and have a problem with DynamicProxies generated by EF runtime.
I have strongly typed partial view for EF POCO class ie Property, but from EF context i get Property object wrapped by Dynamic Proxy Class, so after calling RenderPartial i get this error:
The model item passed into the dictionary is of type 'System.Data.Entity.DynamicProxies.Property_3A99F7A8373036F07B8DA663F79779BF4FE2B241564ECC50420D0228E5B35C1E', but this dictionary requires a model item of type 'GenReal.Shared.Entities.Property'.
I dont know how to disable EF to do not use Dynamic Proxies, using interfaces instead of classes did not help.
Thank you