Andres
Andres

Reputation: 1

EF6: Mapping entity with complex type to one table

I have a entity that have a complex type and I'm using CodeFirst like this:

public class Session
{
    public int SessionId { get; set; }

    public DateTime CreationDate { get; set; }

    public Folder SessionFolder { get { set; } 

}

public class Folder
{
    string _path;

    public Folder(string path)
    {
        _path = path;
    }

    public string Path
    {
        get { return _path; }
    }
}

I'm using Fluent API and I'd like to make only one table, Session table with this struct:

SessionId as int
CreationDate as datetime
Path as nvarchar(255)

How can I do?

Upvotes: 0

Views: 296

Answers (1)

Jan
Jan

Reputation: 230

In Entity Framework 6 you can specify in which tables an entity is saved, via the DbContext:

public class YourContext : DbContext
{
    public YourContext() : base("YourContext")
    {
    }

    public DbSet<Session> Sessions{ get; set; }
    public DbSet<Folder> Folders{ get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        // Saves the Session and folder in the same table, but allows for individual fetches
        modelBuilder.Entity<Session>().HasRequired(s => s.Folder).WithRequiredPrincipal();
        modelBuilder.Entity<Session>().ToTable("SessionFolderTable");
        modelBuilder.Entity<Folder>().ToTable("SessionFolderTable");
    }
}

Upvotes: 1

Related Questions