簡體   English   中英

由於類型xx不可用,因此未加載關系xx

[英]The relationship xx was not loaded because the type xx is not available

當我嘗試為代碼第一個上下文創建遷移時,出現以下錯誤:

錯誤:由於類型'Exiled_Trader.Models.TradeContexts.Property'不可用,因此未加載關系'Exiled_Trader.Models.TradeContexts.Item_AdditionalProperties'。

我的項目模型與“屬性”表具有多個關系,這些關系用在少數幾個不同的屬性中。 這里看起來像:

 public class Item
    {
        public Item()
        {
            AdditionalProperties = new List<Property>();
            NextLevelRequirements = new List<Property>();
            Properties = new List<Property>();
            Requirements = new List<Property>();

        }

            public int ItemId { get; set; }
            public List<Property> AdditionalProperties { get; set; } 
            public List<Property> NextLevelRequirements { get; set; }
            public List<Property> Properties { get; set; }
            public List<Property> Requirements { get; set; }
        }

這是屬性模型:

public class Property
{
public Property()
        {
            Values = new List<PropertyValue>();
        }
    public int PropertyId { get; set; }
    public int ItemId { get; set; }
    public Item Item { get; set; }

    public List<PropertyValue> Values { get; set; }

    public string Name { get; set; }
    public int? DisplayMode { get; set; }
    public int? Type { get; set; }
    public int? Progress { get; set; }
}

這是Item流暢的api配置:

 HasKey(i => i.ItemId);

            HasMany(i => i.AdditionalProperties)
                .WithRequired(p => p.Item)
                .HasForeignKey(p => p.ItemId);

            HasMany(i => i.Properties)
                .WithRequired(p => p.Item)
                .HasForeignKey(p => p.ItemId);

            HasMany(i => i.NextLevelRequirements)
                .WithRequired(p => p.Item)
                .HasForeignKey(p => p.ItemId);

            HasMany(i => i.Requirements)
                .WithRequired(p => p.Item)
                .HasForeignKey(p => p.ItemId);

誰能告訴我我想念什么,為什么我會收到這個錯誤? 其他屬性,屬性,NextLevelRequirements和需求都使用相同的模型類,這是問題所在,即使這些屬性相同,我是否應該為每個屬性創建不同的模型類? 我應該改用復合鍵嗎?

嘗試刪除Item類中的構造函數以及相同類型的多個導航屬性。 EF將為創建導航屬性的實例進行所有艱苦的工作,因此您無需這樣做。 同樣,您的流利的api配置將需要放松與Property 1:M關系的額外定義。

    public class Item
    {

        public int ItemId { get; set; }
        public virtual ICollection<Property> Properties { get; set; }

    }

    public class Property
    {
        public int PropertyId { get; set; }
        public int ItemId { get; set; }
        public string Name { get; set; }
        public int? DisplayMode { get; set; }
        public int? Type { get; set; }
        public int? Progress { get; set; }

        public Item Item { get; set; }
        public virtual ICollection<PropertyValue> Values { get; set; }
    }

這將為您創建兩個表-一個用於Item ,一個用於具有1:M關系的Property

暫無
暫無

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

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