Display multiple classes in one table in Entity Framework 4.1 Fluent API

I have a very simple model that maps to a single table (Projects) in my database. I decided to abstract the images in my class.

public class Project
{
    public long Id { get; set; }
    public string Name { get; set; }
    public Image Images { get; set; }
}

public class Image
{
    public string Thumbnail { get; set; }
    public string PrimaryImage { get; set; }
}

How can I connect my model to a table in the database using the following code:

public class Context : DbContext
{
    public DbSet<Project> Projects { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        ????
    }
}

thank

+3
source share
2 answers

The object model will be mapped to a single table, as it stands now, no free API is required. Code First automatically labels the Image class as Complex Type based on conventions.

+3
source
public class Project
{
    public long Id { get; set; }
    public string Name { get; set; }
    public Image Image { get; set; }
}

public class Image
{
    public string Thumbnail { get; set; }
    public string PrimaryImage { get; set; }
}    

public class YourContext : DbContext
{
    public DbSet<Project> Projects{ get; set; }        

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.ComplexType<Image>();
    }
}
0
source

All Articles