开发者

Is it possible to determine which Entity a Poco maps to, using code, for Entity Framework CTP5?

开发者 https://www.devze.com 2023-02-08 04:37 出处:网络
I\'m not sure how I can get the EntityFramework meta-data for an Entity (i have on my EF designer / edmx) for a Poco object.

I'm not sure how I can get the EntityFramework meta-data for an Entity (i have on my EF designer / edmx) for a Poco object.

For example.

My diagram has an Entity call开发者_C百科ed User. I also have a Poco class called User. I'm under the impression that the Entity name and Poco need to be the same name, so the convention can auto-map the two (along with the poco having the same property names, etc...)

So if i have a type Poco, how can i retrieve the Entity and therefore check that entity to see it's meta-data, like EntityKey or StoreGeneratedPattern, etc ?

Oh - by the way... I don't know what the Poco type is .. meaning .. the class uses Generics...

public class GenericRepository<T> : IRepository<T> where T : class
{ ... }

So, i thought i was going to have to ask the context ... grab me the entity which has a name == typeof(T).Name or whatever...


Access to mapping metadata is normally performed by ObjectContext.MetadataWorkspace. CTP5 hides ObjectContext instance and most of its mapping classes related to mapping (based on DbMappingMetadataItem) are internal or contains only internal members.

It looks like your DbContext is autogenerated from EDMX. What you can probably do it this case is create DbContext by calling constructor which accepts ObjectCotnext. In that case you will have access to ObjectContext and all its methods / properties.

Why do you need it? If you need something special to do in your repository you should create special repository for that type. Generic repository is concept only for very basic solutions.


If you have access to the ObjectContext (which I think you MUST have, since EntityKey/EntityState wouldn't make sense without the context), you can use the following

ObjectStateEntry ose = 
    context.ObjectStateManager.GetObjectStateEntry(yourObject);

From there, you can get all sorts of fun properties: http://msdn.microsoft.com/en-us/library/system.data.objects.objectstateentry.aspx

You may also find TryGetObjectStateEntry(...) to be handy.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号