Entity framework 4 – CTP3 – Code first – Bug

So I finally got some time to start looking at Code first with the Entity framework CTP3 add-on, again. While doing some testing in a test library, where I have two different StorageProviders: One for Linq to Sql and another for Entity framework 4; I found something I call a bug.

I have two classes that shares nothing except that they lie in the same assembly and have the same name. One of them is decorated with mapping attributes for L2Sql and the other has no mapping attributes, but is instead mapped via the EntityConfiguration class, used by EF4.
The Customer classes lies in different namespaces and doesn’t share any specific base-class or interface.

First I register an ObjectSet/EntitySet and then the mappings (EntityConfiguration) for the Ef.Customer with my Contextbuilder. When I then invoke CreateObjectSet() I get promted with the exception:

Schema specified is not valid. Errors:
The mapping of CLR type to EDM type is ambiguous because multiple CLR types match the EDM type ‘Customer’. Previously found CLR type ‘Pls.IntegrationTestsOf.Core.Storage.L2Sql.TestModel.Customer’, newly found CLR type ‘Pls.IntegrationTestsOf.Core.Storage.Ef.TestModel.Customer’.

My opinions: If I specify the Ef.Customer with generics the EF-infrastructure shouldn’t care about the L2Sql.Customer. They aren’t the same. They don’t share anything except lying in the same assembly an having the same name.

Bug or by design? What do you think? Or have I done it wrong?


6 thoughts on “Entity framework 4 – CTP3 – Code first – Bug

  1. The EF runtime is not capable of distinguishing between CLR classes of the same name in different namespaces.

    So despite Code First knowing exactly which class you mean it has no way of fully-qualifying it for the EF runtime.


    • I guess this is a side effect of me having entered the EF4 world via the Code-only approach and thereby not knowing the details of the EDMX etc. But why aren’t namespaces taken into concern? Isn’t it part of the uniqueness of a class?

      Can’t you add something like “aliases” when going via the code-only approach? That way the innerworkings of EF will have distinct names while the class-model and code-only will work with the “real” names?


  2. There is nothing Code First can do – aliases or otherwise – the CLR type will be looked up at runtime without namespaces being a consideration. There is no intermediate layer that can be used.

    I do not believe the EF team considers this a bug.


    • Mailed with Daniel Simmons and as I interpreted it the team was aware of this “problem” and that eventually they will try to solve the issue of not being able to have two entities named e.g “Customer” in the same assembly. As I see it, it’s an issue. Customer isn’t at all unique, with the namespace included it can be put in a certain context, e.g Sales, Marketing etc.


  3. Definitely a bug, something similar has already been fixed by Microsoft in EF 4 Beta 2, when classes with same `local` names located in `different` assemblies (such as .NET assemblies) caused collisions (http://stackoverflow.com/a/919515).

    They left the bug when classes are stored within the assembly containing Entities, but belong to multiple EF4 edmx files (ex: when `Country` entity exists in both GeographyConfigurationModel.edmx, and CustomerServiceModel.edmx. The two `Country` classes contain different sets of properties in each model).

  4. Well, however they “fixed” it, they made it worse. I don’t HAVE two classes with the same name, I have ONE class.
    The mapping of CLR type to EDM type is ambiguous because multiple CLR types match the EDM type ‘Classname’. Previously found CLR type ‘App.Domain.Classname’, newly found CLR type ‘App.Domain.Classname’.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s