【发布时间】:2021-11-17 14:41:36
【问题描述】:
我需要使用代码优先的方法在 .NET Core 中的两个库之间建立链接。
我需要在标准库中的国家表和带有贸易表格的表之间建立一个链接。
两个库都在不同的上下文中运行。
项目之间的关系可以单向添加,因为如果您尝试添加双向约束,则会出现循环错误。
有没有办法建立这样的关系?
public class CountryStandard
{
[Key]
public int IdCountryStandard { get; set; }
[Required(ErrorMessage = "Select Country")]
[Display(Name = "Name Country")]
[MaxLength(80, ErrorMessage = "Name max 80")]
public string NameCountryStandard { get; set; }
public bool IsActive { get; set; }
[JsonIgnore]
//public virtual Standard Standard { get; set; }
public virtual ICollection<Standard> Standard { get; set; }
public virtual ICollection<TradeForms> TradeForms { get; set; }
}
public class TradeForms
{
[Key]
public int TradeFormsId { get; set; }
/...
.../
public int IdCountryStandard { get; set; }
public CountryStandard CountryStandard { get; set; }
}
建立关系后,我将添加到 Fluent API 他做了数据库迁移。
但我不知道是否有可能以这种方式从代码优先建立单独的库之间的关系。
【问题讨论】:
-
你能指定场景吗?也许有更多代码?
-
我已经添加了类的代码,我想在这些类之间建立关系
-
我从标准库下载状态。之后标准将显示在表格中。通过类之间的关系,我想从标准库中取状态的名称。
标签: c# database .net-core ef-code-first