0

Following the example in this question: How to create a many-to-many mapping in Entity Framework? I would like to have a table mapping where I can add or remove many-to-many relationships without having to go through the Media or Contract entities.

Essentially, I would like to have:

public class Media // One entity table
{
    public int Id { get; set; }
    public string Name { get; set; }
    public bool Enabled { get; set; }

    public virtual ICollection<Contract> Contracts { get; set; }
}

public class Contract // Second entity table
{
    public int Id { get; set; }
    public string Code { get; set }

    public virtual ICollection<Media> Medias { get; set; }
}

public class ContractMedia // Association table implemented as entity
{
    public Media Media { get; set; }
    public int MediaId { get; set; }

    public Contract Contract { get; set; }
    public int ContractId { get; set; }
}

Is it possible to configure this scenario using the FluentAPI?

Community
  • 1
  • 1
vtortola
  • 34,709
  • 29
  • 161
  • 263

1 Answers1

0

afaik not with the ContractMedia entity, but you can:

public class Media // One entity table
{
    public int Id { get; set; }
    public string Name { get; set; }
    public bool Enabled { get; set; }

    public virtual ICollection<ContractMedia> Contracts { get; set; }
}

public class Contract // Second entity table
{
    public int Id { get; set; }
    public string Code { get; set }

    public virtual ICollection<ContractMedia> Medias { get; set; }
}

public class ContractMedia // Association table implemented as entity
{
    public Media Media { get; set; }
    public int MediaId { get; set; }

    public Contract Contract { get; set; }
    public int ContractId { get; set; }
}

or

public class Media // One entity table
{
    public int Id { get; set; }
    public string Name { get; set; }
    public bool Enabled { get; set; }

    public virtual ICollection<Contract> Contracts { get; set; }
}

public class Contract // Second entity table
{
    public int Id { get; set; }
    public string Code { get; set }

    public virtual ICollection<Media> Medias { get; set; }
}

that will lead to the creation of a non mapped association table in the database.

tschmit007
  • 7,559
  • 2
  • 35
  • 43