簡體   English   中英

EF6 Code First“外鍵多重性在角色中無效”

[英]EF6 Code First “Foreign Key Multiplicity is not valid in Role”

我對EF有點新鮮。 我的情況不可能那么獨特,但我一直無法找到一個好的答案。

我有三張桌子。 前兩個是CompanyClient Client有一個CompanyId外鍵返回其客戶所在的公司。 CompanyClient都具有Address表的AddressId外鍵。

我通過地址表消除了級聯刪除,以消除循環級聯。 現在,當我嘗試生成我的數據庫時,我得到:

Multiplicity在關系“Client_Address”中的Role'Client_Address_Source'中無效。 由於“從屬角色”屬性不是關鍵屬性,因此從屬角色的多重性的上限必須為“*”。

我在公司表上也得到了同樣的錯誤。 這是我的實體(代碼縮寫為post): *編輯包含更多細節

namespace ForeignKeyExample.DataModel
{
    [Table("Address")]
    public class Address
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int AddressId { get; set; }
        public int AddressTypeId { get; set; }
        public string Address1 { get; set; }
        public string Address2 { get; set; }
        public string City { get; set; }
        public string State { get; set; }
        public string PostalCode { get; set; }
    }
}

namespace ForeignKeyExample.DataModel
{
    [Table("Company")]
    public class Company
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int CompanyId { get; set; }
        public string Name { get; set; }
        public int? AddressId { get; set; }

        [ForeignKey("AddressId")]
        public Address Address { get; set; }
    }
}

namespace ForeignKeyExample.DataModel
{
    [Table("Client")]
    public class Client
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int ClientId { get; set; }
        public string Name { get; set; }
        public int? AddressId { get; set; }
        public int CompanyId { get; set; }

        [ForeignKey("AddressId")]
        public Address Address { get; set; }

        [ForeignKey("CompanyId")]
        public Company Company { get; set; }
    }
}

namespace ForeignKeyExample
{
    public class ExampleContext : DbContext
    {
        public ExampleContext() : base("name=ExampleContext"){  this.Configuration.ProxyCreationEnabled = false; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Client>().HasOptional(a => a.Address).WithOptionalDependent().WillCascadeOnDelete(false);
            modelBuilder.Entity<Company>().HasOptional(a => a.Address).WithOptionalDependent().WillCascadeOnDelete(false);
        }

        public DbSet<Address> Addresses { get; set; }
        public DbSet<Company> Companies { get; set; }
        public DbSet<Client> Clients { get; set; }
    }
}

也許有一些方法可以“流利地”解決OnModelCreating中的問題? 這個錯誤讓我相信(我已經在其他帖子中看到)EF希望我翻轉引用並讓地址表引用回公司或客戶端,但這意味着我需要兩個地址表,這似乎嗯,跛腳。 任何幫助贊賞。

謝謝
布蘭登

*編輯
根據評論中的要求,我在上面的課程中添加了更多細節。 這三個模型類位於主項目下的子文件夾模型中。 EF6安裝在項目中。 如前所述,OnModelCreating中的兩行是刪除關系中的循環引用,如果刪除了可選的Address項,則刪除級聯。 運行enable-migrations后,我收到上面列出的錯誤。 希望這將使其可重現。 感謝您的評論。

只想分享這個問題的積極解決方案 - 即使在這種情況下不相關 - 這適用於VS2012:

1)為項目選擇.edmx。 這 - 希望 - 顯示數據庫/表和“模型瀏覽器”MB的圖表。

2)在MB中打開“關聯”並單擊相關性的FK_定義。

3)在'屬性'中查看'End1 Multiplicity'和'End2 Multiplicity'並將它們適當地更改為例如'0 ... 1(零或一個任務)'。

在我的情況下,我忘記在定義表時將字段定義為“Nullable”並隨后進行更改。 因此在我的MVC項目中遇到問題,當基礎發生變化時,它不會自行重新配置。 糾正這是一個挑戰,這個修復是最后一件事要改變.-)

到目前為止,我只是MVC的初學者......

只需注釋掉OnModelCreating中的兩行就可以修復錯誤。 生成ED遷移並查看生成的表,我認為它們是您想要的外鍵:

        CreateTable(
            "dbo.Address",
            c => new
                {
                    AddressId = c.Int(nullable: false, identity: true),
                    AddressTypeId = c.Int(nullable: false),
                    Address1 = c.String(),
                    Address2 = c.String(),
                    City = c.String(),
                    State = c.String(),
                    PostalCode = c.String(),
                })
            .PrimaryKey(t => t.AddressId);

        CreateTable(
            "dbo.Client",
            c => new
                {
                    ClientId = c.Int(nullable: false, identity: true),
                    Name = c.String(),
                    AddressId = c.Int(),
                    CompanyId = c.Int(nullable: false),
                })
            .PrimaryKey(t => t.ClientId)
            .ForeignKey("dbo.Address", t => t.AddressId)
            .ForeignKey("dbo.Company", t => t.CompanyId, cascadeDelete: true)
            .Index(t => t.AddressId)
            .Index(t => t.CompanyId);

        CreateTable(
            "dbo.Company",
            c => new
                {
                    CompanyId = c.Int(nullable: false, identity: true),
                    Name = c.String(),
                    AddressId = c.Int(),
                })
            .PrimaryKey(t => t.CompanyId)
            .ForeignKey("dbo.Address", t => t.AddressId)
            .Index(t => t.AddressId);

關於級聯刪除,如果外鍵可以為空,則Code First不會在關系上設置級聯刪除,並且當刪除主體時,外鍵將設置為null。 因此,如果您未在此處執行顯式映射,則級聯刪除將設置為false。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM