簡體   English   中英

使用 Entity Framework Core 在具有多對多關系的數據透視表中插入新記錄

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

我正在使用 ASP.NET Core MVC 構建應用程序。 我正在嘗試插入具有多對多關系的新記錄。 我有 3 個表RepairsPartsRepairParts

如何將RepairIdPartId插入到RepairParts表中? EF Core 是否為此提供了一些東西? 我搜索了文檔,但沒有提到有關如何執行此操作的任何信息。

實體框架會自動執行此操作嗎? (插入數據透視表)還是我需要手動完成? 一個例子會有所幫助。

維修類:

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

零件類:

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

維修零件類:

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類:

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

這里有兩個選項供您選擇:

  1. 通過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. 通過_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