gpt4 book ai didi

c# - ASP.NET Core, EF Core 同时一对一和一对多

转载 作者:太空宇宙 更新时间:2023-11-03 12:25:05 25 4
gpt4 key购买 nike

我目前正在使用 ASP.NET 核心。我有一个 BaseModelSubModel,其结构如下:

class BaseClass{
SubClass MainSubClass {get;set;}
List<SubClass> SubClasses {get;set;}
}

当我尝试用它添加迁移时,我得到这个错误:

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.`*

真正的数据模型是...

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>();
}
}

StorePhotoModel.cs

[Table("StorePhotos")]
public class StorePhotoModel : MediaModel
{

// base Store
public int BaseStoreId { get; set; }
[ForeignKey(nameof(BaseStoreId))]
public StoreModel BaseStore { get; set; }
}

最佳答案

Entity Framework 对这两个模型之间的关系感到困惑。

StoreModel 中你有这个:

public StorePhotoModel StoreMainPhoto { get; set; } //one to one
public ICollection<StorePhotoModel> StorePhotos { get; set; } //one to many

这表明 StoreModelStorePhotoModel 具有一对一和一对多关系。

你没有在问题中指出应该是什么关系,所以我无法评论哪个是正确的。

更新

试试这个:

[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>();
}
}

更新2

您可以将 OnModelCreating 重写为这样的内容。

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<StoreModel>()
.HasMany(x => x.StorePhotos)

modelBuilder.Entity<StoreModel>()
.HasRequired(x => x.StoreMainPhoto)
.HasForeignKey(x => x.StoreMainPhotoId)
}

请注意这是未经测试的,因此您可能需要进行调整。您可以在这里找到与您的问题类似的问题:

关于c# - ASP.NET Core, EF Core 同时一对一和一对多,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45409810/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com