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

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

本文介绍了使用Entity Framework Core在具有多对多关系的数据透视表中插入新记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用ASP.NET Core MVC构建应用程序.我正在尝试插入具有多对多关系的新记录.我有3个表 Repairs Parts RepairParts .

I am building an application using 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.

如何将 RepairId PartId 插入 RepairParts 表中?EF Core是否为此提供了某些东西?我搜索了文档,但没有提及如何执行此操作.

How can I insert the RepairId and PartId into the RepairParts table? Does EF Core provide something for this? 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.

修复类:

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

RepairPart 类:

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. 通过公共虚拟ICollection< RepairParts>插入 RepairParts .RepairParts {获取;放;}

  1. Insert RepairParts by 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();

  • 通过 _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();
    

  • 这篇关于使用Entity Framework Core在具有多对多关系的数据透视表中插入新记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

    09-05 02:58