EF Core 3支架(数据库优先)不会生成主键Fluent API

我已经使用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.CompanyaccountGroupId,e.RegionId })
            .Hasname("IX_AppUseraccountaccess");

        builder.HasIndex(e => new { e.accountId,e.RegionId,e.Gpslong,e.BillingStart,e.Address,e.Email,e.IncludeEscalationEmail,e.Gpslat,e.ArrivalInterval,e.Telephone,e.Vatnumber,e.AutoReceive,e.AutoIssue,e.IsBillableToaccount,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代码中添加对此的映射?

lx53166753 回答:EF Core 3支架(数据库优先)不会生成主键Fluent API

之所以没有添加,是因为约定该属性为主键。

来自Conventions in Entity Framework Core

  

如果属性名为ID<entity name>ID(不区分大小写),它将被配置为主键。如果一个类同时包含ID<entity name>ID,则它们更倾向于sudo date +%T -s "11:14:00"

本文链接:https://www.f2er.com/3112330.html

大家都在问