简体   繁体   中英

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.

Cases.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

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

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. 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. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints. 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.

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

This should fix your problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM