簡體   English   中英

在沒有外鍵的情況下首先在數據庫中映射表

[英]Mapping Table in DB first without foreign key

我有一個用數據庫優先方法編寫的程序; 我有一個表ServicePlan和另一個ServicePlanDetails 它們沒有相互映射,但它們有一個公共列PlanId 一個servicePlan可以像一個列表一樣包含多個ServicePlanDetails

我不想對數據庫進行任何更改,但我也想對它們進行 map。 我怎樣才能做到這一點? 在 model 創建的方法中執行此操作是否會為我完成工作並且不會更改數據庫中的任何內容? 我試過這個但可以得到結果。

為簡單起見,我只添加了幾列及其映射,而不是全部:

public partial class ServicePlan
{
    public ServicePlan()
    {
        ServicePlanDetails = new HashSet<ServicePlanDetail>();
    }

    public long PlanId { get; set; }
    public decimal PhoneId { get; set; }
    public byte? NLines { get; set; }
    public DateTime? DateStart { get; set; }
    public DateTime? DateEnd { get; set; }
    public virtual ICollection<ServicePlanDetail> ServicePlanDetails { get; set; }
}

public partial class ServicePlanDetail
{
    public long PlanId { get; set; }
    public string? ServCode { get; set; }
    public string? CountryCode { get; set; }
    public bool? IsPlan { get; set; }
    public decimal? Cost { get; set; }
}

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<ServicePlan>(entity =>
    {
        entity.HasKey(e => e.PlanId).HasName("PK_UsersPlan");

        entity.ToTable("ServicePlan");

        entity.HasIndex(e => e.VideoTronId, "IDX_VTID").HasFillFactor(80);

        entity.HasIndex(e => new { e.PhoneId, e.IsApproved }, "Ix_SrvcPlan").HasFillFactor(80);

        entity.Property(e => e.Zone).HasMaxLength(50);
        entity.HasMany(p => p.ServicePlanDetails)
            .WithOne()
            .HasPrincipalKey(p => p.PlanId)
            .HasForeignKey(p => p.PlanId);
    });
}

我得到的錯誤是:

無法確定類型為“ICollection”的導航“ServicePlan.ServicePlanDetails”所表示的關系。 手動配置關系,或使用“[NotMapped]”屬性或使用“OnModelCreating”中的“EntityTypeBuilder.Ignore”忽略此屬性。

我想將與serviceplandetails具有相同planidserviceplan放入serviceplan的列表中。

PlanId不能同時是一對多的外鍵和主鍵。

public partial class ServicePlanDetail
{
    public long Id { get; set; }
    public long PlanId { get; set; }
    public string? ServCode { get; set; }
    public string? CountryCode { get; set; }
    public bool? IsPlan { get; set; }
    public decimal? Cost { get; set; }
}

配置

 entity.HasMany(p => p.ServicePlanDetails)
            .WithOne()
            .HasPrincipalKey(p => p.PlanId)
            .HasForeignKey(p => p.PlanId);

如果在數據庫中,一個計划可以有多個 ServicePlanDetails,並且您通過計划 ID 鏈接它們,您如何區分一個 ServicePlanDetail 與該計划的另一個? 是什么讓兩個 ServicePlanDetail 記錄獨一無二? 這就是你問題的症結所在。 您的 FK 映射是正確的,但如果 PlanId 是 ServicePlanDetail 上的 PK,它將不起作用。 PK 必須唯一標識單個記錄。 例如,如果您的計划與應用於不同用戶的服務計划詳細信息相關聯,其中多個用戶引用同一計划並且 ServicePlanId 上有一個 UserID,則 PK 應該是 PlanId + UserId 的組合。

作為 DB-First 方法,數據庫應該已經設置了 PK 和約束。 您只需設置 EF 鍵和關系類型來匹配它。

現在,如果 ServicePlanDetail 的 PK 僅聲明為 PlanId,那么答案是 Plan 和 ServicePlanDetail 之間的關系是一對一,而不是一對多。 這變成了.HasOne(p => p.ServicePlanDetail).WithOne(sp => sp.Plan)並且在不改變數據關系的情況下你真的無能為力。 如果基礎數據庫架構不支持該關系,則您無法神奇地更改 EF 將使用的關系。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM