开发者

Using Nested Classes with Entity Framework

I've tried to do this in the designer, but I wasn't able to figure it out. Is it possible to persist nested classes using Entity Framework?

Note: I am just curious whether this is possible or not. I can't think, at this point, if there would ever be a reason to do this, but it might be nice to know how if it is possible.

Example:

public class Normal开发者_StackOverflowClass
{
    public class NestedClass { }
}

Update:

Danny Varod had a good idea for how to accomplish this. When I have some spare time, I'm going to try it out, and I'll post the results on here, unless someone else gets to it first.


EF classes are declared partial, so you can add whatever you want to them.

Note that the inner-class's properties won't be persisted to the DB, if you want that, use a navigation property instead.


A nested class in .NET is basically the same as a class within another level of namespace (accept for the fact the inner class can access private parts of outer class, as Ladislav Mrnka pointed out - you could use internal instead of private to get around this), there is no change in behaviour (unlike in Java), so there is not much point in using nested classes.

You can define sub objects using complex properties or using a navigation properties, however, complex properties have limited capabilities (no navigation properties or keys in them) and neither are created as nested classes.

To force EF to use nested classes, you could try creating the classes yourself, then mapping them either with a Code-First approach or by cancelling the auto-creation of the class and writing them yourself (or changing the .tt file to created classes nested) and then editing the .emdx as an xml to map entity to a different class.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜