简体   繁体   English

如何将两个表中的两个外键转发到第三个表中? ASP.NET 内核和 C#

[英]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.我想要的是将这两个表中的ClientIDContractID转发到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.

相关问题 "我们如何在 ASP.NET Core &amp; EF Core &amp; C# 上将多个外键从一个模型添加到另一个模型" - How can we add multiple foreign keys from one model to another on ASP.NET Core & EF Core & C# C# Asp.net 核心。 使用 2 个密钥在两个服务之间进行授权 - C# Asp.net core. Authorization between two services using 2 secret keys 在C#ASP.NET Web API中使用2个外键从表中检索数据 - retrieving data from a table with 2 foreign keys in c# asp.net web api 从 ASP.NET 内核中的两个表中检索数据 - Retrieve data from two tables in ASP.NET Core ASP.NET EF CORE:由两个外键组成的复合键无法正确评估 - ASP.NET EF CORE: Composite key composed of two foreign keys not evaluating properly asp.net EF 核心 MVC 无法访问来自第三个外键表的数据 - asp.net EF core MVC cant access data from third foreign key table 如何从 asp.net 核心 c# 中的 object 列表中提取键和值 - how to extract keys and values from a list of object in asp.net core c# 在ASP.NET Web表单中使用SQL Server在一个表中使用两个外键 - Two foreign keys in one table using SQL Server in asp.net webform 从 C# (ASP.NET Core MVC) 中的两个不同下拉列表中传递 2 个值 - 不确定 - Passing 2 values from two different dropdownlists in C# (ASP.NET Core MVC) - not sure 如何在C#linq asp.net MVC中使用逗号分隔值列连接两个表 - how to join two tables with comma separated values column in c# linq asp.net mvc
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM