gpt4 book ai didi

c# - 同一实体类型的一对一和一对多

转载 作者:行者123 更新时间:2023-11-30 17:29:55 26 4
gpt4 key购买 nike

我正在尝试设计以下实体关系

  • 程序
  • 大会

一个程序可以有多个程序集。其中一个组件将是主组件。每个程序集只属于一个程序。

类的建模如下:

public class Program
{
public int Id {get;set;}
public virtual ProgramAssembly MainAssembly {get;set;}
public virtual ICollection<ProgramAssembly> Assemblies {get;set;}
}

public class ProgramAssembly
{
public int Id {get;set;}
public virtual Program Program {get;set;}
public int ProgramId {get;set;}
}

我没有对 FluentApi 指定任何内容(但是,我并不反对)。
使用上面的代码,我得到这个错误:

An error occurred while saving entities that do not expose foreign key properties for their relationships. The EntityEntries property will return null because a single entity cannot be identified as the source of the exception. Handling of exceptions while saving can be made easier by exposing foreign key properties in your entity types. See the InnerException for details.

InnerException: Unable to determine a valid ordering for dependent operations. Dependencies may exist due to foreign key constraints, model requirements, or store-generated values.

我尝试将 ProgramAssembly 更改为以下内容

public class ProgramAssembly
{
[ForeignKkey("Program")]
public int Id {get;set;}
public virtual Program Program {get;set;}
}

但是,我得到这个错误:

ProgramAssembly_Program_Source: : Multiplicity is not valid in Role 'ProgramAssembly_Program_Source' in relationship 'ProgramAssembly_Program'. Because the Dependent Role refers to the key properties, the upper bound of the multiplicity of the Dependent Role must be '1'.

我还尝试了以下流利的 API 方法

modelBuilder.Entity<ProgramAssembly>()
.HasRequired(a => a.Program)
.WithMany(p => p.Assemblies);

public class ProgramAssembly
{
public int Id { get; set; }
public virtual Program Program { get; set; }
[ForeignKey("Program")] //same error with or without this attribute
public int ProgramId { get; set; }
}

那么错误就是:

An error occurred while saving entities that do not expose foreign key properties for their relationships. The EntityEntries property will return null because a single entity cannot be identified as the source of the exception. Handling of exceptions while saving can be made easier by exposing foreign key properties in your entity types. See the InnerException for details.' UpdateException: Unable to determine a valid ordering for dependent operations. Dependencies may exist due to foreign key constraints, model requirements, or store-generated values.

如何在不更改在程序集上添加“IsPrimary”属性的方法的情况下允许这种关系?

我在 SO 上看到了一些类似的问题,但它们要么是关于 EF 核心的,比如 this one ,或建议重大逻辑更改,如 this one ,甚至 this one甚至无法编译。

最佳答案

您需要告知 EF 您的主键和外键如何工作。您需要添加属性来定义主键 [Key] 并定义外键,并将属性添加到导航属性以定义作为外键的字段。

public class Program
{
[Key]
public int Id {get;set;}
public int MainAssemblyId {get;set;}
[ForeignKey("MainAssemblyId ")]
public virtual ProgramAssembly MainAssembly {get;set;}
public virtual ICollection<ProgramAssembly> Assemblies {get;set;}
}

public class ProgramAssembly
{
[Key]
public int Id {get;set;}
[ForeignKey("ProgramId")]
public virtual Program Program {get;set;}
public int ProgramId {get;set;}
}

关于c# - 同一实体类型的一对一和一对多,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50593388/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com