如何使用fluent api代码首先创建循环引用c#

本文关键字:创建 循环 引用 何使用 fluent api 代码 | 更新日期: 2023-09-27 17:58:54

我需要在fluent api中创建两个对象的多对多循环关系。我不知道如何使用流利的api来实现这个目标。

关系应为一对多(必需)和一对多我试过这个场景,但它不起作用。

这里是我的班级模型:

[Table("Question")]
    public class Question
    {
        public int ID { get; set; }
        public int IDQuestion { get; set; }
        public int IDForm { get; set; }
        public virtual AnswerSelectionType AssociateAnswerSelectionType { get; set; }
        public virtual ICollection<AnswerSelectionType> AnswerSelectionTypes { get; set; }
    }
    [Table("AnswerSelectionType")]
    public class AnswerSelectionType
    {
        public int ID { get; set; }
        public int IDQuestionForm { get; set; }
        public int? IDAssociateQuestion { get; set; }
        public int? DataValue { get; set; }
        public virtual Question AssociateQuestion { get; set; }
        public virtual Question QuestionForm { get; set; }
    }

之后,在这里流利的API

modelBuilder.Entity<AnswerSelectionType>()
        .HasRequired(choixRep => choixRep.QuestionForm)
        .WithMany(questionTe => questionTe.AnswerSelectionTypes)
        .HasForeignKey(fk => fk.IDQuestionForm);
    modelBuilder.Entity<AnswerSelectionType>()
       .HasOptional(choixRep => choixRep.AssociateQuestion)
       .WithMany(many => many.AnswerSelectionTypes)
       .HasForeignKey(fk => fk.IDAssociateQuestion);

我得到的错误是:类型上声明的导航属性配置了冲突的多重性

你能帮我找出我出错的原因吗?

非常感谢!!

如何使用fluent api代码首先创建循环引用c#

AnswerSelectionType需要在多个方面拥有实体的集合。我在AnswerSelectionType中没有看到任何集合。

但我不知道你说的循环是什么意思。你心目中的结构是什么?

Question --> 1:n --> AnswerSelectionType --> 1:n -->  Question

在这种情况下,AnswerSelectionType中的属性会有所帮助。

ICollection<Question> Questions { get; set; }

然后

.WithMany(many => many.Questions)

(注意,从下面的文本中删除"NHEntity")

试试这个:

    public class AnswerSelectionTypeNHEntityMapping : ClassMap<AnswerSelectionTypeNHEntity>
    {
        public AnswerSelectionTypeNHEntityMapping()
        {
            Schema("dbo");
            Table("AnswerSelectionTypeNHEntity");
            Id(x => x.AnswerSelectionTypeNHEntityKey).GeneratedBy.Increment();
            Map(x => x.IDQuestionForm);
            Map(x => x.IDAssociateQuestion).Not.Nullable();
            Map(x => x.DataValue).Not.Nullable();
            References<QuestionNHEntity>(x => x.QuestionForm)
                .Class(typeof(QuestionNHEntity))
                /*.Not.Nullable() */
                 .Nullable()
                .Column("ParentQuestionFormKey")
                .Index("IX_AnswerSelectionType_ParentQuestionFormKey")
                .Cascade.SaveUpdate()
                ;

            References<QuestionNHEntity>(x => x.AssociateQuestion )
                .Class(typeof(QuestionNHEntity))
                /*.Not.Nullable() */
                 .Nullable()
                .Column("OneToOneAssociateQuestionKey")
                .Index("IX_AnswerSelectionType_OneToOneAssociateQuestionKeyy")
                .Cascade.SaveUpdate()
                ;

        }

    }

   public class QuestionNHEntityMapping : ClassMap<QuestionNHEntity>
    {
        public QuestionNHEntityMapping()
        {
            Schema("dbo");
            Table("QuestionNHEntity");
            Id(x => x.QuestionNHEntityKey).GeneratedBy.Assigned();
            Map(x => x.IDQuestion).Not.Nullable().Index("IX_Question_IDQuestion"); ;
            Map(x => x.IDForm).Not.Nullable();

            HasMany<AnswerSelectionTypeNHEntity>(x => x.AnswerSelectionTypes)
                .Inverse()
                .KeyColumns.Add("MyAnswerSelectionTypesColumnName")
                ;

            References<AnswerSelectionTypeNHEntity>(x => x.AssociateAnswerSelectionType)
                .Class(typeof(AnswerSelectionTypeNHEntity))
                /*.Not.Nullable() */
                 .Nullable()
                .Column("OneToOneAnswerSelectionTypeNHEntityKey")
                .Index("IX_AnswerSelectionType_OneToOneAnswerSelectionTypeNHEntityKey")
                .Cascade.SaveUpdate()
                ;

        }
    }