开发者

Is it possible to store a struct's fields in the same table as the containing class?

I'm currently using Entity Framework 4 with the June 2011 CTP (for enum support) and I'm having difficulty with embedding a struct in a persisted class. I have a Patient class with an InclusionCriteria struct field (among others):

public class P开发者_如何学编程atient
{
    public InclusionCriteria inclusionCriteria
    {
        get;
        set;
    }

    ...
}

The struct is fairly simple, with primitive fields:

public struct InclusionCriteria
{
    public bool withStemi
    {
        get;
        set;
    }

    ...
}

After running the system, it became apparent that the struct's data was not being saved. When I view the generated table, the struct is not there (as a foreign key or otherwise). I would like the struct's fields to be located in the Patient table, so I haven't made a DbSet for the criteria. I could potentially do this, but I'd rather keep it all in the same table. Is this possible in the entity framework?


Structs are not supported. You must use class and map it as complex type. If you are using code first approach you can try to make it class and simply recreate database - if you are lucky it will be mapped as complex type automatically. Otherwise you can use data annotation:

[ComplexType]
public class InclusionCriteria { ... } 

or fluent API:

modelBuilder.ComplexType<InclusionCriteria>(); 

If you are using EDMX follow this article to create complex type and this article to use it.

Btw. CTP 2011 is only for testing new features, not for real development. Its content can change in next CTP.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜