简体   繁体   English

使用 Entity Framework Core 在具有多对多关系的数据透视表中插入新记录

[英]Insert new record in pivot table with many to many relationship using Entity Framework Core

I am building an application using ASP.NET Core MVC.我正在使用 ASP.NET Core MVC 构建应用程序。 I am trying to insert a new record with a many-to-many relationship.我正在尝试插入具有多对多关系的新记录。 I have 3 tables Repairs , Parts and RepairParts .我有 3 个表RepairsPartsRepairParts

How can I insert the RepairId and PartId into the RepairParts table?如何将RepairIdPartId插入到RepairParts表中? Does EF Core provide something for this? EF Core 是否为此提供了一些东西? I searched the documentation but it doesn't mention anything about how to do this.我搜索了文档,但没有提到有关如何执行此操作的任何信息。

Does Entity Framework do this automatically?实体框架会自动执行此操作吗? (insert to the pivot table) Or do I need to do it manually? (插入数据透视表)还是我需要手动完成? An example would help.一个例子会有所帮助。

Repair class:维修类:

public class Repair
{
    [Key]
    public int RepairId { get; set; }

    public int VehicleId { get; set; }
    public string Notes { get; set; }
    public string Mileage { get; set; }
    public DateTime RepairDate { get; set; }
    public string uuid { get; set; }

    [ForeignKey("VehicleId")]
    public Vehicle Vehicle { get; set; }

    public virtual ICollection<RepairParts> RepairParts { get; set; }
}

Part class:零件类:

public class Part
{
    public int PartId { get; set; }
    public int Code { get; set; }
    public string PartCode { get; set; }
    public string Type { get; set; }
    public string Name { get; set; }
    public string Descr { get; set; }
    public string Manufacturer { get; set; }
    public string uuid { get; set; }

    public virtual ICollection<RepairParts> RepairParts { get; set; }
}

RepairPart class:维修零件类:

public class RepairParts
{
    public int RepairId { get; set; }
    public Repair Repair { get; set; }

    public int PartId { get; set; }
    public Part Part { get; set; }

    public decimal price { get; set; }
}

DbContext class: DbContext类:

public class ApplicationDbContext : IdentityDbContext
{
    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
        : base(options)
    {
    }

    public DbSet<Customer> Customers { get; set; }
    public DbSet<TaxOffice> TaxOffices { get; set; }
    public DbSet<Vehicle> Vehicles { get; set; }
    public DbSet<Part> Parts { get; set; }
    public DbSet<Repair> Repairs { get; set; }

    protected override void OnModelCreating(ModelBuilder builder)
    {
        base.OnModelCreating(builder);

        builder.Entity<Customer>()
              .HasMany(v => v.Vehicles)
              .WithOne(b => b.Customer);

        builder.Entity<Vehicle>()
             .HasOne(c => c.Customer)
             .WithMany(b => b.Vehicles);

        builder.Entity<Vehicle>()
              .HasMany(v => v.Repairs)
              .WithOne(c => c.Vehicle);

        builder.Entity<RepairParts>()
            .HasKey(t => new { t.RepairId, t.PartId });

        builder.Entity<RepairParts>()
            .HasOne(r => r.Repair)
            .WithMany(t => t.RepairParts)
            .HasForeignKey(f => f.RepairId);

        builder.Entity<RepairParts>()
            .HasOne(r => r.Part)
            .WithMany(p => p.RepairParts)
            .HasForeignKey(f => f.PartId);
    }
}

Here are two options for you:这里有两个选项供您选择:

  1. Insert RepairParts by public virtual ICollection<RepairParts> RepairParts { get; set; }通过public virtual ICollection<RepairParts> RepairParts { get; set; }插入RepairParts public virtual ICollection<RepairParts> RepairParts { get; set; }

     var repaire = new Repair { uuid = "R3" }; var part = new Part { Code = 3 }; var reparePart = new RepairParts { Repair = repaire, Part = part }; repaire.RepairParts = new List<RepairParts>() { reparePart }; _context.Add(repaire); _context.SaveChanges();
  2. Insert RepairParts explictly by _context.Add(reparePart1);通过_context.Add(reparePart1);显式插入RepairParts _context.Add(reparePart1);

     var repaire1 = new Repair { uuid = "RR1" }; var part1 = new Part { Code = 4 }; var reparePart1 = new RepairParts { Repair = repaire1, Part = part1 }; _context.Add(repaire1); _context.Add(part1); _context.Add(reparePart1); _context.SaveChanges();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM