定义实体,例如,
using CustomerManagement.Tracking.Exceptions; using System; using System.Diagnostics.Contracts; using System.Numerics; using Volo.Abp.Domain.Entities.Auditing; namespace CustomerManagement.Tracking.CustomerArchives { public class PlanPrice : CreationAuditedEntity { protected PlanPrice() { } /// <summary> /// 方案Id /// </summary> public Guid PlanId { get; private set; } public Guid CustomerId { get; private set; } public Customer Customer { get; private set; } = null!; /// <summary> /// 方案 /// </summary> public CustomerPlan Plan { get; private set; } = null!; /// <summary> /// 合同签订时间 /// </summary> public DateTime? ContractTime { get; private set; } public PlanPriceStatus Status { get; set; } = PlanPriceStatus.报价中;public override object[] GetKeys() { return new object[] { PlanId }; } } }
\src\CustomerManagement.Tracking.EntityFrameworkCore\EntityFrameworkCore
builder.Entity<CustomerPlan>(typeBuilder => { typeBuilder.ConfigureByConvention(); // 只调用这个 }); builder.Entity<PlanPrice>(b => { b.ToTable("PlanPrice"); b.ConfigureByConvention(); // 1. 设置主键(既然你想主外键合一) b.HasKey(x => x.PlanId); // 2. 配置与 Customer 的关系---写明显示调用,拒绝喂饭 b.HasOne(x => x.Customer) .WithMany() // <--- 关键:不要在括号里写 x => x.PlanPrices .HasForeignKey(x => x.CustomerId) .OnDelete(DeleteBehavior.NoAction); // 3. 配置与 Plan 的关系 b.HasOne(x => x.Plan) .WithOne() // 1:1 关系 .HasForeignKey<PlanPrice>(x => x.PlanId) .IsRequired(); }); }
如果还是数据库迁移出现多余的列,搜索删除迁移文件的多余的列以及design.cs文件删除有关多余的列即可
浙公网安备 33010602011771号