我已经使用EntityFramework的Scaffold-DbContext
命令从现有数据库中简化了该项目的所有模型。
EntityFramework在创建映射到表的所有模型并生成Fluent API
配置代码方面做得很出色。
据我所知,Fluent API
代码缺少Primary Key
字段的配置,尽管它已为生成的模型添加了属性。
以下是我生成的类之一及其对应的Fluent API
代码的示例:
public partial class Account
{
public int AccountId { get; set; } //Primary Key
public int CompanyId { get; set; }
public int CompanyAccountTypeId { get; set; }
public int? CompanyAccountGroupId { get; set; }
public int? RegionId { get; set; }
public string Name { get; set; }
public string Code { get; set; }
public string Address { get; set; }
public string Email { get; set; }
public string IncludeEscalationEmail { get; set; }
public decimal? Gpslat { get; set; }
public decimal? Gpslong { get; set; }
public string Telephone { get; set; }
public string Vatnumber { get; set; }
public bool AutoReceive { get; set; }
public bool AutoIssue { get; set; }
public bool? IsBillableToAccount { get; set; }
public DateTime? BillingStart { get; set; }
public bool? IsEquipmentDepot { get; set; }
public bool? IsShiftAttendanceEnabled { get; set; }
public int? ShiftMinHoursForLunchDeduction { get; set; }
public TimeSpan? NightShiftStart { get; set; }
public TimeSpan? NightShiftEnd { get; set; }
public int? ShiftStartDayOfMonth { get; set; }
public TimeSpan? OperatingHoursStart { get; set; }
public TimeSpan? OperatingHoursEnd { get; set; }
public int? LoadBays { get; set; }
public int? LoadInterval { get; set; }
public int? ArrivalInterval { get; set; }
public TimeSpan? OverrideStockTakeCloseBalanceTime { get; set; }
public bool? TempIgnoreVendorIssueViaSap { get; set; }
public bool Archived { get; set; }
public DateTime CreatedDate { get; set; }
public int CreatedByPersonId { get; set; }
public DateTime UpdatedDate { get; set; }
public int UpdatedByPersonId { get; set; }
public virtual Company Company { get; set; }
public virtual CompanyAccountGroup CompanyAccountGroup { get; set; }
public virtual CompanyAccountType CompanyAccountType { get; set; }
public virtual Region Region { get; set; }
public virtual ICollection<AccountBalance> AccountBalance { get; set; }
public virtual ICollection<AccountContact> AccountContact { get; set; }
public virtual ICollection<AccountEquipment> AccountEquipment { get; set; }
public virtual ICollection<AccountHrlookup> AccountHrlookup { get; set; }
public virtual ICollection<AccountPickVolumeDefault> AccountPickVolumeDefaultAccount { get; set; }
public virtual ICollection<AccountPickVolumeDefault> AccountPickVolumeDefaultPartnerAccount { get; set; }
public virtual ICollection<AccountPickVolumeDetail> AccountPickVolumeDetail { get; set; }
public virtual ICollection<AccountPickVolumePartner> AccountPickVolumePartner { get; set; }
public virtual ICollection<AppUserAccount> AppUserAccount { get; set; }
public virtual ICollection<BiometricTerminal> BiometricTerminal { get; set; }
public virtual ICollection<CompanyVendorAccount> CompanyVendorAccount { get; set; }
public virtual ICollection<EquipmentCheck> EquipmentCheckAccount { get; set; }
public virtual ICollection<EquipmentCheck> EquipmentCheckAccountMaintenance { get; set; }
public virtual ICollection<GantryTransfer> GantryTransferCreditAccount { get; set; }
public virtual ICollection<GantryTransfer> GantryTransferDebitAccount { get; set; }
public virtual ICollection<Order> OrderDepotAccount { get; set; }
public virtual ICollection<Order> OrderPrimaryAccount { get; set; }
public virtual ICollection<Rfequipment> Rfequipment { get; set; }
public virtual ICollection<RfmoveTransaction> RfmoveTransactionPartnerAccount { get; set; }
public virtual ICollection<RfmoveTransaction> RfmoveTransactionPrimaryAccount { get; set; }
public virtual ICollection<ShiftCalendar> ShiftCalendar { get; set; }
public virtual ICollection<ShiftSchedule> ShiftSchedule { get; set; }
public virtual ICollection<ShiftTeam> ShiftTeam { get; set; }
public virtual ICollection<TransferDeviance> TransferDevianceIssueAccount { get; set; }
public virtual ICollection<TransferDeviance> TransferDevianceIssuePartnerAccount { get; set; }
public virtual ICollection<TransferDeviance> TransferDevianceReceiptAccount { get; set; }
public virtual ICollection<TransferDeviance> TransferDevianceReceiptPartnerAccount { get; set; }
public virtual ICollection<TransferJournal> TransferJournalCreditAccount { get; set; }
public virtual ICollection<TransferJournal> TransferJournalDebitAccount { get; set; }
public virtual ICollection<Transfer> TransferPartnerAccount { get; set; }
public virtual ICollection<Transfer> TransferPrimaryAccount { get; set; }
public virtual ICollection<VehicleCheckIn> VehicleCheckIn { get; set; }
public virtual ICollection<XAppUserAccountAccess> XAppUserAccountAccess { get; set; }
}
这是Fluent API:
builder.HasIndex(e => new { e.AccountId, e.CompanyAccountTypeId, e.Name, e.Code, e.CompanyId })
.HasName("IX_AccountCompanyType");
builder.HasIndex(e => new { e.Archived, e.AccountId, e.UpdatedDate, e.CompanyId, e.CompanyAccountTypeId, e.CompanyAccountGroupId, e.RegionId })
.HasName("IX_AppUserAccountAccess");
builder.HasIndex(e => new { e.AccountId, e.CompanyId, e.CompanyAccountGroupId, e.RegionId, e.Name, e.Gpslong, e.BillingStart, e.Code, e.Address, e.Email, e.IncludeEscalationEmail, e.Gpslat, e.ArrivalInterval, e.Telephone, e.Vatnumber, e.AutoReceive, e.AutoIssue, e.IsBillableToAccount, e.UpdatedDate, e.IsEquipmentDepot, e.OperatingHoursStart, e.OperatingHoursEnd, e.LoadBays, e.LoadInterval, e.UpdatedByPersonId, e.OverrideStockTakeCloseBalanceTime, e.TempIgnoreVendorIssueViaSap, e.Archived, e.CreatedDate, e.CreatedByPersonId, e.CompanyAccountTypeId })
.HasName("IX_vAccount");
builder.Property(e => e.Address)
.HasMaxLength(500)
.IsUnicode(false);
builder.Property(e => e.BillingStart).HasColumnType("date");
builder.Property(e => e.Code)
.HasMaxLength(50)
.IsUnicode(false);
builder.Property(e => e.CreatedByPersonId).HasColumnName("CreatedBy_PersonId");
builder.Property(e => e.CreatedDate).HasColumnType("datetime");
builder.Property(e => e.Email)
.HasMaxLength(200)
.IsUnicode(false);
builder.Property(e => e.Gpslat)
.HasColumnName("GPSLat")
.HasColumnType("decimal(18, 6)");
builder.Property(e => e.Gpslong)
.HasColumnName("GPSLong")
.HasColumnType("decimal(18, 6)");
builder.Property(e => e.IncludeEscalationEmail)
.HasMaxLength(500)
.IsUnicode(false);
builder.Property(e => e.Name)
.IsRequired()
.HasMaxLength(255)
.IsUnicode(false);
builder.Property(e => e.Telephone)
.HasMaxLength(20)
.IsUnicode(false);
builder.Property(e => e.TempIgnoreVendorIssueViaSap).HasColumnName("temp_IgnoreVendorIssueViaSAP");
builder.Property(e => e.UpdatedByPersonId).HasColumnName("UpdatedBy_PersonId");
builder.Property(e => e.UpdatedDate).HasColumnType("datetime");
builder.Property(e => e.Vatnumber)
.HasColumnName("VATNumber")
.HasMaxLength(50)
.IsUnicode(false);
builder.HasOne(d => d.CompanyAccountGroup)
.WithMany(p => p.Account)
.HasForeignKey(d => d.CompanyAccountGroupId)
.HasConstraintName("FK_Account_CompanyAccountGroup");
builder.HasOne(d => d.CompanyAccountType)
.WithMany(p => p.Account)
.HasForeignKey(d => d.CompanyAccountTypeId)
.OnDelete(DeleteBehavior.ClientSetNull)
.HasConstraintName("FK_Account_CompanyAccountType");
builder.HasOne(d => d.Company)
.WithMany(p => p.Account)
.HasForeignKey(d => d.CompanyId)
.OnDelete(DeleteBehavior.ClientSetNull)
.HasConstraintName("FK_Account_Company");
builder.HasOne(d => d.Region)
.WithMany(p => p.Account)
.HasForeignKey(d => d.RegionId)
.HasConstraintName("FK_Account_Region");
如您所见,我的模型具有名为AccountId
的主键字段。为什么没有在Fluent API代码中添加对此的映射?
答案 0 :(得分:0)
之所以没有添加,是因为约定该属性为主键。
来自Conventions in Entity Framework Core:
如果属性名为
ID
或<entity name>ID
(不区分大小写),它将被配置为主键。如果一个类同时包含ID
和<entity name>ID
,则它们更倾向于sudo date +%T -s "11:14:00"
。