2张桌子

时间:2017-09-22 14:25:51

标签: c# asp.net entity-framework database-design foreign-keys

我希望表格PaintballWorkerInvoicesReceipts与关系相关联。 PaintballWorker可以包含多个InvoicesAddedInvoices,还可以有多个ReceiptsAddedReceiptsPaintballWorkerInvoices之间的关系适用于InvoicesAddedInvoices。我想对Receipts做同样的事情,但我在更新数据库时遇到错误:

  

ALTER TABLE语句与FOREIGN KEY约束冲突   “FK_dbo.Receipts_dbo.PaintballWorkers_ReceiptAddedByID”。冲突   发生在数据库“1028_test”,表“dbo.PaintballWorkers”,列   'PaintballWorkerID'。

我的主要课程(PaintballWorker):

public class PaintballWorker : Person
    {
        [Key]
        public int PaintballWorkerID { get; set; }
        (other properties)
        [InverseProperty("PaintballWorker")]
        public virtual List<Invoice> Invoices { get; set; }

        [InverseProperty("InvoiceAddedBy")]
        public virtual List<Invoice> AddedInvoices { get; set; }

        [InverseProperty("PaintballWorker")]
        public virtual List<Receipt> Receipts { get; set; }

        [InverseProperty("ReceiptAddedBy")]
        public virtual List<Receipt> AddedReceipts { get; set; }
    }

我的Receipt课程:

public class Receipt
    {
        [Key]
        public int ReceiptID { get; set; }
         (other properties)
        public int PaintballWorkerID { get; set; }
        [ForeignKey("PaintballWorkerID")]
        public virtual PaintballWorker PaintballWorker { get; set; }

        public int ReceiptAddedByID { get; set; }
        [ForeignKey("ReceiptAddedByID")]
        public virtual PaintballWorker ReceiptAddedBy { get; set; }
    }

我的Invoice课程:

public class Invoice
    {
        [Key]
        public int InvoiceID { get; set; }
        (other properies)
        public int PaintballWorkerID { get; set; }
        [ForeignKey("PaintballWorkerID")]
        public virtual PaintballWorker PaintballWorker { get; set; }

        public int InvoiceAddedByID { get; set; }
        [ForeignKey("InvoiceAddedByID")]
        public virtual PaintballWorker InvoiceAddedBy { get; set; }
    }

有人可以解释这里有什么问题吗?

目前public virtual PaintballWorker PaintballWorker { get; set; } InvoiceReceipt工作正常,使用public int InvoiceAddedByID { get; set; } [ForeignKey("InvoiceAddedByID")] public virtual PaintballWorker InvoiceAddedBy { get; set; }更新了数据库 并且工作正常,但是当我想用数据库更新时 public int ReceiptAddedByID { get; set; } [ForeignKey("ReceiptAddedByID")] public virtual PaintballWorker ReceiptAddedBy { get; set; } 我收到的错误是我在开始时写的。

0 个答案:

没有答案