簡體   English   中英

如何使用EF fluentApi並設置具有2個不同外鍵的復合鍵的類?

[英]How to use EF fluentApi and set a class with composite key of 2 different foreign keys?

我在這里找到了一些類似的問題,但是沒有一個問題對我有用。

我有一個包含大約60個實體的項目,其中包含許多場景,所有這些都通過簡單使用一些基本的EF注釋映射到數據庫。 我無法使用的一種情況是:

public class Client {
  [Key]
  public int IDClient { get; set; }
  (...)
}

public class Research {
  [Key]
  public int IDReasearch { get; set; }
  (...)
}

public class ClientReaserach {
  [Key, Column(Order = 1)]
  public int IDClient { get; set; }
  [ForeignKey("IDClient")]
  public virtual Client Client { get; set; }

  [Key, Column(Order = 2)]
  public int IDResearch { get; set; }
  [ForeignKey("IDResearch")]
  public virtual Research Research { get; set; }

  (...)
}

盡管到目前為止似乎還可以使用其他方案,但在我看來,這似乎是一種fluentAPI(據我所知,注釋僅提供了EF實際功能的子集)。

所以我有一些問題:

  • 我可以繼續使用我的注釋並使用fluentAPI來僅映射上述情況嗎?

  • EF將如何“知道”此實體及其關系是通過fluentAPI映射的(在添加新遷移時不嘗試再次映射它們)?

  • 可以使用fluentAPI重建我的所有項目嗎? 我應該如何處理所有已經生成的遷移(我應該刪除它們)?

  • 我將如何通過fluentAPI完成上述方案?

編輯1:

這是我用來完成上述方案的真實代碼,如下所示:

public class PacientePesquisa
{
    [Key, Column(Order = 1)]
    [Required(ErrorMessage = Msg_Critica_Req_IDPesquisa)]
    [Display(Name = "Pesquisa")]
    public int IDPesquisa { get; set; }

    [ForeignKey("IDPesquisa")]
    public virtual Pesquisa Pesquisa { get; set; }

    [Key, Column(Order = 2)]
    [Required(ErrorMessage = Msg_Critica_Req_NRProntuario)]
    [StringLength(NRProntuario_MaxLength, ErrorMessage = Msg_Critica_Tam_NRProntuario)]
    [Display(Name = "Prontuário")]
    public string NRProntuario { get; set; }

    [ForeignKey("NRProntuario")]
    public virtual Paciente Paciente { get; set; }

    (...)


public class Pesquisa 
{
    [Key]
    public int IDPesquisa { get; set; }

    (...)

public class Paciente 
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    public string NRProntuario { get; set; }

    (...)

並且在運行“ add-migration”時,我收到以下錯誤:

System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.Data.Entity.ModelConfiguration.ModelValidationException: One or more validation errors were detected during model generation:

System.Data.Entity.Edm.EdmAssociationConstraint: : The number of properties in the Dependent and Principal Roles in a relationship constraint must be identical.        

我將嘗試回答您的問題,但首先要回答一個問題:帶注釋的映射有什么問題? 對我來說看起來不錯,我認為不需要Fluent API。 這個答案中,有一個類似的模型(“與有效負載的多對多關系”),它僅使用注釋和映射約定,而沒有任何Fluent API映射。

關於您的問題:

我可以繼續使用我的注釋並使用fluentAPI來僅映射上述情況嗎?

是的,您可以通過數據注釋和Fluent API混合使用映射而不會出現問題-只要您不創建矛盾的映射即可,例如將帶有注釋的列重命名為與Fluent API不同的名稱。

EF將如何“知道”此實體及其關系是通過fluentAPI映射的(在添加新遷移時不嘗試再次映射它們)?

EF處理上下文的OnModelCreating方法並應用您定義的映射。 它還處理注釋並應用在此定義的映射。 它將兩個映射合並為一個模型。 該模型被序列化為存儲在數據庫的MigrationHistory表中的“模型哈希”。 如果模型哈希與已經存儲的最新哈希值相同,EF就會知道模型和數據庫是同步且最新的,並且不會創建新的遷移。

可以使用fluentAPI重建我的所有項目嗎? 我應該如何處理所有已經生成的遷移(我應該刪除它們)?

好的,可以。 無需刪除舊的遷移。 使用Fluent API添加新映射只是實體模型的新改進-就像添加新注釋一樣。

我將如何通過fluentAPI完成上述方案?

modelBuilder.Entity<ClientResearch>()
    .HasKey(cr => new { cr.IDClient, cr.IDResearch });

modelBuilder.Entity<ClientResearch>()
    .HasRequired(cr => cr.Client)
    .WithMany() // or .WithMany(c => c.ClientResearches) if you have a collection
    .HasForeignKey(cr => cr.IDClient);

modelBuilder.Entity<ClientResearch>()
    .HasRequired(cr => cr.Research)
    .WithMany() // or .WithMany(r => r.ClientResearches) if you have a collection
    .HasForeignKey(cr => cr.IDResearch);

暫無
暫無

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

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