如何在Entity Framework中创建多对多映射? [英] How to create a many-to-many mapping in Entity Framework?

查看:98
本文介绍了如何在Entity Framework中创建多对多映射?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是这种情况,我有2个实体,如合同,媒体。

  public class Media:Entity 
{
public string Name {get; set;}
public bool启用
* //其他属性可以忽略.. *
}

public class Contract:Entity
{
public string Code {get; set;}
* //其他属性可以忽略.. *
}

合同有很多Medias,似乎是很多到很多。



但是!在ef代码首先,我需要在ContractMedia表中有3个字段(ef自动生成)。
,如StartDate,EndDate和Price。



如何在这种情况下映射?

解决方案

如果要与关联表中的附加数据创建多个关系,则必须将关联表作为实体。纯粹的多对多的关系只是在纯表中具有实体id。



在你的情况下,它将是:

  public class Media //一个实体表
{
public int Id {get;组; }
public string Name {get;组; }
public bool Enabled {get;组; }

public virtual ICollection< ContractMedia> ContractMedias {get;组; }
}

public class Contract //第二个实体表
{
public int Id {get;组; }
public string Code {get; set}

public virtual ICollection< ContractMedia> ContractMedias {get;组;
}

public class ContractMedia //作为实体实现的关联表
{
public int MediaId {get;组; }
public int ContractId {get;组; }
public DateTime StartDate {get;组; }
public DateTime EndDate {get;组; }
public double价格{get;组; }

public virtual Media Media {get;组; }
public virtual Contract Contract {get;组; }
}

创建模型/实体后,需要在上下文中定义关系:

  protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity< ContractMedia>()
.HasKey(c => new {c.MediaId,c.ContractId});

modelBuilder.Entity< Contract>()
.HasMany(c => c.ContractMedias)
.WithRequired()
.HasForeignKey(c => c.ContractId);

modelBuilder.Entity< Media>()
.HasMany(c => c.ContractMedias)
.WithRequired()
.HasForeignKey(c => c.MediaId);
}

您还可以参考以下链接:

与Fluent API中的多个字段进行多对多映射

实体框架CodeFirst与附加信息的许多关系

首先创建代码,多对多,在关联表中添加其他字段


Here is the case, I have 2 entities, such as Contract、Media。

public class Media : Entity
{
    public string Name {get; set;}
    public bool Enabled
    *//other properties can be ignored..*
}

public class Contract : Entity
{
    public string Code {get; set;}
    *//other properties can be ignored..*
}

Contract has many Medias, it seems that they are many to many.

But!! at ef code first, i need 3 more fields in the ContractMedia table(ef auto generated). such as StartDate,EndDate and Price. these could not be added in Media entity.

How to map at this case??

解决方案

If you want to create many to many relationship with additional data in association table, you have to make the association table as entity. The pure many to many relationship is only in pure table with entity id's.

In you case it will be:

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> ContractMedias { get; set; }
}

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

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

public class ContractMedia // Association table implemented as entity
{
    public int MediaId { get; set; }
    public int ContractId { get; set; }
    public DateTime StartDate { get; set; }
    public DateTime EndDate { get; set; }
    public double Price { get; set; }

    public virtual Media Media { get; set; }
    public virtual Contract Contract { get; set; }
}

And after you created models/entities, you need to define relationships in context:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
   modelBuilder.Entity<ContractMedia>()
       .HasKey(c => new { c.MediaId, c.ContractId });

   modelBuilder.Entity<Contract>()
       .HasMany(c => c.ContractMedias)
       .WithRequired()
       .HasForeignKey(c => c.ContractId);

   modelBuilder.Entity<Media>()
       .HasMany(c => c.ContractMedias)
       .WithRequired()
       .HasForeignKey(c => c.MediaId);  
}

Also you can refer to these links:
Many to many mapping with extra fields in Fluent API
Entity Framework CodeFirst many to many relationship with additional information
Create code first, many to many, with additional fields in association table

这篇关于如何在Entity Framework中创建多对多映射?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆