【发布时间】:2021-12-27 11:53:37
【问题描述】:
我正在将一个 netcore 项目从 2.2 版升级到 3.1 版。我已经成功构建了它,但是在运行时我收到了这个错误:
{"'RestaurantCustomQuestionCategory.QuizLimits'和'CustomCategoryQuizLimits.RestaurantCustomCategory'之间的一对一关系无法确定子/依赖方。要识别关系的子/依赖方,请配置外键属性。如果这些导航不应该是同一关系的一部分,请在不指定反向的情况下配置它们。有关更多详细信息,请参阅http://go.microsoft.com/fwlink/?LinkId=724062。"}
这是代码:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<RestaurantCustomQuestionCategory>()
.HasKey(t => new {t.RestaurantId, t.QuestionCategoryId});
}
餐厅自定义问题类别:
public class RestaurantCustomQuestionCategory : IQuizTimeLimitable
{
public RestaurantCustomQuestionCategory()
{
QuizLimits = new CustomCategoryQuizLimits(this);
}
public int QuestionCategoryId { get; set; }
[ForeignKey("QuestionCategoryId")]
public CustomCategory QuestionCategory { get; set; }
public int RestaurantId { get; set; }
[ForeignKey("RestaurantId")]
public Restaurant Restaurant { get; set; }
public CustomCategoryQuizLimits QuizLimits { get; set; }
public void UpdateQuizTimeLimit(CustomCategoryQuizLimits item)
{
this.QuizLimits = item;
}
public QuizLimitsBase GetQuizLimits()
{
return QuizLimits;
}
}
CustomCategoryQuizLimits:
public class CustomCategoryQuizLimits : QuizLimitsBase
{
public CustomCategoryQuizLimits(RestaurantCustomQuestionCategory restaurantCustomCategory)
{
RestaurantCustomCategory = restaurantCustomCategory;
}
public CustomCategoryQuizLimits()
{
}
public CustomCategoryQuizLimits(
RestaurantCustomQuestionCategory restaurantCustomCategory
, QuizQuestionsLimit limitsQuizQuestionsLimit
, QuizTimeLimit limitsQuizTimeLimit
) : base(limitsQuizQuestionsLimit, limitsQuizTimeLimit)
{
RestaurantCustomCategory = restaurantCustomCategory;
}
[Column("CustomCategoryId")]
public int QuestionCategoryId { get; set; }
[Column("CustomCategory_RestaurantId")]
public int RestaurantId { get; set; }
public RestaurantCustomQuestionCategory RestaurantCustomCategory { get; private set; }
public override QuizLimitsBase GetDefault()
{
return new CustomCategoryQuizLimits();
}
public override int GetChainId()
{
return RestaurantCustomCategory.Restaurant.ChainId;
}
public override IEnumerable<int> GetRestaurantIds()
{
return new List<int>() {RestaurantCustomCategory.RestaurantId};
}
}
我不知道我必须改变什么来解决这个问题,所以我希望得到一些帮助。
【问题讨论】:
-
您确定
[Column("CustomCategoryId")]和[Column("CustomCategory_RestaurantId")]正确吗? -
@Dai 是的。据我所知,其 2.2 原始版本的项目运行良好,并且具有相同的代码。
标签: entity-framework asp.net-core-3.1 ef-core-3.1