简体   繁体   中英

Multiple classes mapping to the same table in Entity Framework 4.1 Fluent API

I have a very simple model that maps to one table (Projects) in my database. I have chosen to abstract out images to its own 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 would I go about wiring up my model to the table in the database using the code below:

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

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

Thanks

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

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>();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM