Entity framework EF代码前6位和多对多实体映射

Entity framework EF代码前6位和多对多实体映射,entity-framework,ef-code-first,many-to-many,entity-framework-6,Entity Framework,Ef Code First,Many To Many,Entity Framework 6,按照这个问题中的示例:我希望有一个表映射,在这里我可以添加或删除多对多关系,而无需通过媒体或合同实体 基本上,我希望: public class Media // One entity table { public int Id { get; set; } public string Name { get; set; } public bool Enabled { get; set; } public virtual ICollection<Contrac

按照这个问题中的示例:我希望有一个表映射,在这里我可以添加或删除多对多关系,而无需通过
媒体
合同
实体

基本上,我希望:

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; }
}
公共类媒体//一个实体表
{
公共int Id{get;set;}
公共字符串名称{get;set;}
已启用公共bool的{get;set;}
公共虚拟ICollection协定{get;set;}
}
公共类契约//第二个实体表
{
公共int Id{get;set;}
公共字符串代码{get;set}
公共虚拟ICollection媒体{get;set;}
}
公共类ContractMedia//关联表作为实体实现
{
公共媒体媒体{get;set;}
公共int MediaId{get;set;}
公共合同{get;set;}
公共int压缩{get;set;}
}

是否可以使用FluentAPI配置此场景?

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; }
}
公共类媒体//一个实体表
{
公共int Id{get;set;}
公共字符串名称{get;set;}
已启用公共bool的{get;set;}
公共虚拟ICollection协定{get;set;}
}
公共类契约//第二个实体表
{
公共int Id{get;set;}
公共字符串代码{get;set}
公共虚拟ICollection媒体{get;set;}
}
公共类ContractMedia//关联表作为实体实现
{
公共媒体媒体{get;set;}
公共int MediaId{get;set;}
公共合同{get;set;}
公共int压缩{get;set;}
}

公共类媒体//一个实体表
{
公共int Id{get;set;}
公共字符串名称{get;set;}
已启用公共bool的{get;set;}
公共虚拟ICollection协定{get;set;}
}
公共类契约//第二个实体表
{
公共int Id{get;set;}
公共字符串代码{get;set}
公共虚拟ICollection媒体{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; }
}