Aridane Álamo
Aridane Álamo

Reputation: 334

EF4.0 wrong namespace for System.Data.Metadata.Edm.EntityType

i am having a little problem in EF4 code-first (i think that may be a problem only using CF actually).

I am trying to get some info of an entity type from my context metadata using a code similar to this:

return (context as System.Data.Entity.Infrastructure.IObjectContextAdapter)
            .ObjectContext.MetadataWorkspace
            .GetItems<EntityType>(DataSpace.CSpace)
            .Where(x => x.FullName == ModelType.FullName)
            .FirstOrDefault();

ModelType is a parameter wich contains the System.Type of the entity to search for. It should return a System.Data.Metadata.Edm.EntityType instance with the type's metadata.

The model referenced in the DbSet is named MyApp.Models.User and the DbContext class was created under the MyApp.Servicing namespace

Now the problem i am having is that the FullName property of System.Data.Metadata.Edm.EntityType is MyApp.Servicing.User (O_O) instead of MyApp.Models.User.

I think it may be that EF is mocking the edm Metadata as i don't have an EDMX in code-first but that's just guessing.

Any idea of why does this happens? i can solve it another way but would like to know why.

Upvotes: 1

Views: 1171

Answers (1)

Claude
Claude

Reputation: 56

Your using the wrong Space. CSpace is the top level layer. If you're using the "DataSpace.OSpace" you will get the full qualified class names you are looking for.

   var OSpaceEntityType= (context as System.Data.Entity.Infrastructure.IObjectContextAdapter)
     .ObjectContext.MetadataWorkspace
     .GetItems<EntityType>(DataSpace.OSpace)
     .FirstOrDefault(x => x.FullName == ModelType.FullName);

...and then to get the corresponding EntityType from the CSpace store:

   var CSpaceEntityType= (context as System.Data.Entity.Infrastructure.IObjectContextAdapter)
     .ObjectContext.MetadataWorkspace
     .GetItems<EntityType>(DataSpace.CSpace)
     .FirstOrDefault(e => e.Name == OSpaceEntityType.Name);

Upvotes: 4

Related Questions