简体   繁体   中英

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.

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM