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

标签 c# entity-framework asp.net-core many-to-many

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

如何将 RepairIdPartId 插入到 RepairParts 表中? EF Core 是否为此提供了一些东西?我搜索了文档,但它没有提到如何执行此操作。

Entity Framework 会自动执行此操作吗? (插入到数据透视表)还是我需要手动执行?一个例子会有所帮助。

维修类:

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. 插入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. 插入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();
    

关于c# - 使用 Entity Framework Core 在具有多对多关系的数据透视表中插入新记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53972328/

相关文章:

c# - WPF 窗口边框上的双击事件

c# - 自定义验证唯一属性 - 泛型类

c# - .Net 核心 : Reading data from CSV & Excel files

asp.net - 使用 ASP.NET Core Web Api 验证第 3 方 Cookie

c# - User.IsInRole 错误

c# - 如何拆分 mysql 命令;不包括拆分时;在顶点之间?

c# - 从 RGB 格式的文件加载位图(无 Alpha)

c# - 为什么 ASP.NET Identity 的 `UserStore` 中有这么多存储库?

c# - Entity Framework 5 - 使用参数调用存储过程。无法识别参数

linux - 在 Linux 上升级 Aspnet Core 应用程序出现错误