Reputation: 1284
I am currently working with ASP.NET core.
I have a BaseModel
and SubModel
which is structured like this:
class BaseClass{
SubClass MainSubClass {get;set;}
List<SubClass> SubClasses {get;set;}
}
When I try to add migrations with this, I get this error:
Unable to determine the relationship represented by navigation property 'StoreModel.StoreMainPhoto' of type 'StorePhotoModel'. Either manually configure the relationship, or ignore this property from the model.`*
The real data models are...
in StoreModel.cs
[Table("Stores")]
public class StoreModel
{
[Key]
public int Id { get; set; }
[Required]
[MaxLength(25)]
public string StoreName { get; set; }
[MaxLength(80)]
public string StoreShortDescription { get; set; }
[MaxLength(4000)]
public string StoreFullDescription { get; set; }
public string StoreLocation { get; set; }
public string StoreAddress { get; set; }
#region graphics Data
public StorePhotoModel StoreMainPhoto { get; set; }
public ICollection<StorePhotoModel> StorePhotos { get; set; }
#endregion
public ICollection<MenuModel> Menus { get; set; }
public ICollection<StoreReviewModel> StoreReviews { get; set; }
#region store contacts
public string StorePhoneNumber { get; set; }
public string StoreExtraContacts { get; set; }
#endregion
public StoreModel()
{
StorePhotos = new Collection<StorePhotoModel>();
Menus = new Collection<MenuModel>();
StoreReviews = new Collection<StoreReviewModel>();
}
}
in StorePhotoModel.cs
[Table("StorePhotos")]
public class StorePhotoModel : MediaModel
{
// base Store
public int BaseStoreId { get; set; }
[ForeignKey(nameof(BaseStoreId))]
public StoreModel BaseStore { get; set; }
}
Upvotes: 2
Views: 2269
Reputation: 1200
Entity Framework is confused to what the relationship between these two models is.
In StoreModel
you have this:
public StorePhotoModel StoreMainPhoto { get; set; } //one to one
public ICollection<StorePhotoModel> StorePhotos { get; set; } //one to many
Which would indicate that StoreModel
has a one to one and and one to many relationships with StorePhotoModel
.
You have not indicated in the question what the relationship should be though so I cannot comment on which would be correct.
Try this:
[Table("Stores")]
public class StoreModel
{
[Key]
public int Id { get; set; }
[Required]
[MaxLength(25)]
public string StoreName { get; set; }
[MaxLength(80)]
public string StoreShortDescription { get; set; }
[MaxLength(4000)]
public string StoreFullDescription { get; set; }
public string StoreLocation { get; set; }
public string StoreAddress { get; set; }
public int StoreMainPhotoId {get;set;}
#region graphics Data
[ForeignKey(nameof(StoreMainPhotoId))]
public StorePhotoModel StoreMainPhoto { get; set; }
public ICollection<StorePhotoModel> StorePhotos { get; set; }
#endregion
public ICollection<MenuModel> Menus { get; set; }
public ICollection<StoreReviewModel> StoreReviews { get; set; }
#region store contacts
public string StorePhoneNumber { get; set; }
public string StoreExtraContacts { get; set; }
#endregion
public StoreModel()
{
StorePhotos = new Collection<StorePhotoModel>();
Menus = new Collection<MenuModel>();
StoreReviews = new Collection<StoreReviewModel>();
}
}
You can override the OnModelCreating to something like this.
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<StoreModel>()
.HasMany(x => x.StorePhotos)
modelBuilder.Entity<StoreModel>()
.HasRequired(x => x.StoreMainPhoto)
.HasForeignKey(x => x.StoreMainPhotoId)
}
Please note this is untested so you may need to tweak. You can find similar questions to yours here:
Upvotes: 1