EF Code First 6和多对多实体映射

时间:2015-02-24 11:51:42

标签: entity-framework ef-code-first many-to-many entity-framework-6

按照此问题中的示例:How to create a many-to-many mapping in Entity Framework?我希望有一个表映射,我可以在其中添加或删除多对多关系,而无需通过Media或{{1实体。

基本上,我想:

Contract

是否可以使用FluentAPI配置此方案?

1 个答案:

答案 0 :(得分:0)

afaik不在ContractMedia实体,但您可以:

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

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

将导致在数据库中创建非映射关联表。