Reputation: 171
when i try to create migration for my code first context i get this error:
Errors: The relationship 'Exiled_Trader.Models.TradeContexts.Item_AdditionalProperties' was not loaded because the type 'Exiled_Trader.Models.TradeContexts.Property' is not available.
my Item Model has more than one many to many relationships to Property table which are used in few different properties. here how it looks like:
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; }
}
Here is the property model:
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; }
}
and here is the Item fluent api configuration:
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);
Could anyone tell me what am i missing and why im getting this error? Additional Properties, Properties, NextLevelRequirements and Requirements all use the same model class, is this the problem and should i make a different model class for each of those properties even if they are the same? Should i use composite keys instead maybe?
Upvotes: 0
Views: 93
Reputation: 4812
Try and remove the constructors as well as the multiple navigation properties of the same type in your Item
class. The EF will do all the hard work in terms of creating instances for your navigation properties so you don't need to do that. Likewise, your fluent api configuration will need to loose the extra definitions for 1:M relationships with the Property
.
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; }
}
This will create you the two tables - one for Item
and one for Property
with the 1:M relationship.
Upvotes: 2