[英]How create circular reference with fluent api code first c#
I need to create a many to many circular relation with two objects in fluent api. 我需要在流畅的api中使用两个对象创建多对多循环关系。 And I don't know how to use fluent api to achieve that goal.
而且我不知道如何使用流利的api来实现该目标。
The relation should be one to many (required) and one to many (optionnel) I tried this scenario and it's not working. 我尝试了这种情况,该关系应该是一对多(必需)和一对多(选项)。
Here my class model: 这是我的班级模型:
[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; }
}
After that, here the fluent API 之后,这里的流利的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);
The error I got is: The navigation property declared on type has been configured with conflicting multiplicities. 我得到的错误是: 在类型上声明的导航属性已配置有冲突的多重性。
Could you help me to find why I got the error? 您能帮我找到我为什么出错的原因吗?
Thank you very much!! 非常感谢你!!
AnswerSelectionType
would need to have a collection of the entity on the many side. AnswerSelectionType
将需要在多个方面收集实体。 I don't see any collection in AnswerSelectionType
. 我没有在
AnswerSelectionType
看到任何集合。
But I'm not sure what you mean by circular. 但是我不确定你的意思是通函。 What is the structure you have in mind?
您要考虑的结构是什么?
Question --> 1:n --> AnswerSelectionType --> 1:n --> Question
In this case a property in AnswerSelectionType
would help. 在这种情况下,
AnswerSelectionType
的属性会有所帮助。
ICollection<Question> Questions { get; set; }
And then 接着
.WithMany(many => many.Questions)
(Note, remove "NHEntity" from the text below) (注意,从下面的文本中删除“ NHEntity”)
Try this: 尝试这个:
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()
;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.