Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 Patient
{
    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?

like image 544
101100 Avatar asked Aug 16 '11 21:08

101100


1 Answers

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.

like image 153
Ladislav Mrnka Avatar answered Oct 02 '22 12:10

Ladislav Mrnka