[英]How to forward two foreign keys from two tables into third table? ASP.NET Core and C#
I am newbie in .NET Core and I am facing this problem: I have three tables(models): Cases, Contracts and Clients.我是 .NET Core 的新手,我面临这个问题:我有三个表(模型):案例、合同和客户。
Cases.cs案例.cs
public class Cases
{
[Key]
public int CaseID { get; set; }
[Required]
[Column(TypeName ="nvarchar(50)")]
public string OrdinalNumber { get; set; }
[Required]
[Column(TypeName = "nvarchar(100)")]
public string CaseName { get; set; }
public int ClientID { get; set; }
[ForeignKey("ClientID")]
public virtual Clients ClientsID{ get; set; }
public int ContractID { get; set; }
[ForeignKey("ContractID")]
public virtual Contracts ContractsID{ get; set; }
public int Priority { get; set; }
[Required]
[Column(TypeName ="nvarchar(11)")]
public string DateTimeOfOpening { get; set; }
[Required]
[Column(TypeName = "nvarchar(11)")]
public string RequiredDateTime { get; set; }
[Required]
[Column(TypeName = "ntext")]
public string DescriptionOfFault { get; set; }
[Required]
[Column(TypeName = "nvarchar(11)")]
public string Status { get; set; }
}
Contracts.cs合同.cs
public class Contracts
{
[Key]
public int ContractID { get; set; }
[Required]
[Column(TypeName = "nvarchar(50)")]
public string ContractNumber { get; set; }
[Required]
[Column(TypeName = "nvarchar(100)")]
public string ContractName { get; set; }
public int ClientID { get; set; }
[ForeignKey("ClientID")]
public virtual Clients Clients { get; set; }
[Required]
[Column(TypeName = "nvarchar(11)")]
public string StartDate { get; set; }
[Required]
[Column(TypeName = "nvarchar(11)")]
public string EndDate { get; set; }
[Required]
[Column(TypeName = "nvarchar(100)")]
public string ResponseTime { get; set; }
}
and Clients.cs和 Clients.cs
public class Clients
{
[Key]
public int ClientID { get; set; }
public int Code { get; set; }
[Required]
[Column(TypeName = "nvarchar(100)")]
public string ClientName { get; set; }
public int IDNumber { get; set; }
[Column(TypeName = "nvarchar(100)")]
public string Address { get; set; }
[Column(TypeName = "nvarchar(100)")]
public string Place { get; set; }
[Column(TypeName = "nvarchar(100)")]
public string Phone { get; set; }
[Column(TypeName = "nvarchar(100)")]
public string Email { get; set; }
[Column(TypeName = "nvarchar(100)")]
public string ContactPerson { get; set; }
public virtual ICollection<Cases> Cases{ get; set; }
}
What I want is to forward ClientID and ContractID from these two tables to Cases table, but it's not possible.我想要的是将这两个表中的ClientID和ContractID转发到Cases表,但这是不可能的。 Everytime I try to do that there is an error like:
每次我尝试这样做时都会出现如下错误:
Introducing FOREIGN KEY constraint 'FK_Cases_Contracts_ContractID' on table 'Cases' may cause cycles or multiple cascade paths.
在表 'Cases' 上引入 FOREIGN KEY 约束 'FK_Cases_Contracts_ContractID' 可能会导致循环或多个级联路径。 Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.
指定 ON DELETE NO ACTION 或 ON UPDATE NO ACTION,或修改其他 FOREIGN KEY 约束。 Could not create constraint or index.
无法创建约束或索引。 See previous errors.
请参阅以前的错误。 What I am doing wrong?
我做错了什么? Thanks in advance!
提前致谢!
To help yourself in the future, I would write or say the relationship out loud, it'll help you understand how it works.为了将来帮助自己,我会大声写下或说出这段关系,这将帮助您了解它是如何工作的。
If I understand correctly, A Client Has A Contract and this relationship is defined as a Case with additional properties.如果我理解正确,客户有合同,这种关系被定义为具有附加属性的案例。
So a better way of putting it, A client has a Case which has a contract.所以一个更好的说法是,客户有一个有合同的案例。
To fix this, remove the relationship between clients and contracts, this relationship is achieved through the Case table.为了解决这个问题,删除客户和合同之间的关系,这种关系是通过 Case 表来实现的。
Instead A client has a case, and A contract has a case, and a case has a contract and a client.相反,一个客户有一个案例,一个合同有一个案例,一个案例有一个合同和一个客户。
in Contracts.cs Remove ClientId and Client, replace with CaseId and Case在 Contracts.cs 中移除 ClientId 和 Client,替换为 CaseId 和 Case
This should fix your problem.这应该可以解决您的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.