【问题标题】:The child/dependent side could not be determined for the one-to-one relationship. Net Core 2.2 to 3.1无法确定一对一关系的孩子/依赖方。净核 2.2 至 3.1
【发布时间】: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


【解决方案1】:

我找到了解决方案。似乎在过渡到 3.0 EF 时会混淆一对一的关系。所以我在 Fluent 上添加了这个:

protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<RestaurantCustomQuestionCategory>()
                .HasKey(t => new {t.RestaurantId, t.QuestionCategoryId});
            modelBuilder.Entity<RestaurantCustomQuestionCategory>()
                .HasOne(t => t.QuizLimits)
                .WithOne(i => i.RestaurantCustomCategory)
                .HasForeignKey<CustomCategoryQuizLimits>(t => new { t.RestaurantId, t.QuestionCategoryId });
}

【讨论】:

    猜你喜欢
    • 2022-01-02
    • 2019-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-26
    • 2020-07-11
    • 2020-09-17
    • 2020-10-04
    相关资源
    最近更新 更多