I m currently working on an application that works with dynamic questionnaire forms. forms that are defined in a database like so: Forms > Sections > Controls > Questions.
每个问题都有许多业务规则,例如:要求、长度、长度等。
每项《业务规则》都是一个问题的规则,然而,有些复杂的业务规则要求获取另一个问题的价值,因此,每项业务规则都有许多相互关联的问题从中获取必要的价值。
我首次首先使用代码,并有以下定义类别和这一关系的绘图:
public class Question
{
public int Id { get; set; }
public string Name { get; set; }
public int ValueTypeId { get; set; }
public int ParentQuestionId { get; set; }
public virtual ValueType ValueType { get; set; }
public virtual ICollection<Question> ChildQuestions { get; set; }
public virtual ICollection<BusinessRule> BusinessRules { get; set; }
public virtual ICollection<BusinessRule> LinkedBusinessRules { get; set; }
}
public class BusinessRule
{
public int Id { get; set; }
public string ValidationMessage { get; set; }
public string ConditionValue { get; set; }
public int QuestionId { get; set; }
public int BusinessRuleTypeId { get; set; }
public virtual BusinessRuleType BusinessRuleType { get; set; }
public virtual Question Question { get; set; }
public virtual ICollection<Question> LinkedQuestions { get; set; }
}
internal class QuestionConfigruation : EntityTypeConfiguration<Question>
{
public QuestionConfigruation()
{
this.HasMany<Question>(q => q.ChildQuestions)
.WithOptional()
.HasForeignKey(q => q.ParentQuestionId);
this.HasMany(q => q.BusinessRules)
.WithRequired(br => br.Question)
.WillCascadeOnDelete(false);
}
}
internal class BusinessRuleConfiguration : EntityTypeConfiguration<BusinessRule>
{
public BusinessRuleConfiguration()
{
this.HasMany(b => b.LinkedQuestions)
.WithMany(q => q.LinkedBusinessRules)
.Map(map =>
{
map.ToTable("BusinessRuleLinkedQuestions");
map.MapLeftKey("LinkedQuestionId");
map.MapRightKey("BusinessRuleId");
});
}
}
由此在数据库中产生了以下结果:
""https://i.sstatic.net/jTFBE.png" alt="此处的内置图像描述"/ >
最后,我的问题:
<坚固> 坚固> 坚固>[解决] 坚固>
为什么许多人到许多人会坐到一起的桌边, 忽略了《商业规则配置》中所指定的表格名称和钥匙的映射?
<强>[/解决] 强>
当我试图使用自定义端口插入测试表时, 这样做 :
var companyName = new Question
{
Name = "CompanyName",
ValueTypeId = _typeRepository.GetValueType(ValueTypes.String).Id,
BusinessRules = new List<BusinessRule>{
new BusinessRule
{
BusinessRuleTypeId = _typeRepository.GetBusinessRuleType(BusinessRuleTypes.required).Id,
ValidationMessage = "Company name is required.",
}
}
};
var form = new Form
{
Sections = new List<Section>
{
new Section(){
Controls = new List<Control>
{
new Control{
ControlTypeId = _typeRepository.GetControlType(ControlTypes.Textbox).Id
Questions = new List<Question>
{
companyName,
}
}
}
}
}
}
我得到以下错误:
Unable to determine a valid ordering for dependent operations. Dependencies may exist due to foreign key constraints, model requirements, or store-generated values.
我在这上面花费了太多的时间,试图解决它,所以非常感激在EF和代码1方面经验较多的人的帮助。 我开始后悔首先选择代码,但我不知道是代码第一位的问题,还是我对它的理解。
谢谢!