I am using the Entity Framework 4.0 and have a conceptual model that makes heavy use of inheritance.
I can perform the standard inheritance functionality just fine but I am having trouble with the namespacing of the generated C# classes.
My problem is that the Entity Framework always generates all the classes (both base and derived) in the same C# namespace. A hypothetical example of the fully qualified names of the classes generated by the Entity Framework (both base and derived) with a root namespace of "MyApp" would look like the following
MyApp.MyBaseClass
MyApp.MyDerivedClassA开发者_如何转开发
MyApp.MyDerivedClassB
MyApp.MyDerivedClassC
etc...
However, I want to to put the base and derived classes into different namespaces. For example, the base class would still be defined in the root namespace (eg. MyApp) but the derived classes would be defined in a "sub namespace" (eg. MyApp.MyDerivedClasses). The fully qualified names of the derived classes would then look like the following:
MyApp.MyDerivedClasses.MyDerivedClassA
MyApp.MyDerivedClasses.MyDerivedClassB
MyApp.MyDerivedClasses.MyDerivedClassC
etc...
I managed to achieve this with Linq To Sql by manually creating (ie. not using the Linq To Sql Designer) InheritanceMappingAttribute
on the base class and pointing the mapping to the derived classes that are defined in the "derived" namespace.
However, I can not figure out if it is possible to use different namespaces between the base and derived classes with the Entity Framework 4.0 (with or without the designer). Can this be done and if so how can I do it?
No it is not possible using the standard set of tools. The namespace specification on the designer file applies to all entities contained in it. You could try a code first approach to get around this (prolly your best option). Other than that get a good book on T4 templates and modify the T4 POCO generator to let you specify these things.
精彩评论