【问题标题】:Entity Framework Code First Many to Many Mapping is Being Ignored and also not saving实体框架代码优先多对多映射被忽略且不保存
【发布时间】:2012-06-01 01:47:50
【问题描述】:

我目前正在开发一个适用于动态调查表的应用程序。 在数据库中定义的表单,如下所示:表单 > 部分 > 控件 > 问题。

每个问题可以有许多业务规则,例如:required、minlength、maxlength 等。

每个 BusinessRule 都是针对一个问题的规则。但是,有一些复杂的业务规则需要获取另一个问题的值。因此,每个业务规则都可以有许多链接问题以从中获取所需的值。

我是第一次使用代码,并为这种关系定义了以下类和映射:

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");
                });
    }
}

这导致在数据库中生成以下内容:

最后,我的问题:

[已解决]

为什么多对多连接表忽略了我在 BusinessRuleConfiguration 中指定的表名和键的映射?

[/已解决]

当我尝试使用自定义初始化程序插入测试表单时:

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 和 Code First 方面有更多经验的人提供帮助。我开始后悔选择代码优先,但我不知道是代码优先还是我对它的理解。

谢谢!

【问题讨论】:

  • 您是否将BusinessRuleConfiguration 添加到OnModelCreating 中的modelBuilder 配置中?
  • 是的,我也在想。您是否通过覆盖 OnModelCreating() 将配置添加到上下文类? modelBuilder.Configurations.Add(new BusinessRuleConfiguration());modelBuilder.Configurations.Add(new QuestionConfigruation());
  • 啊,我傻了,我忘了添加 BusinessRule 配置。谢谢大家解决了一个问题。

标签: c# entity-framework ef-code-first entity-relationship


【解决方案1】:

在我们进入实体框架之前,我认为您的模型不能正确反映您的业务问题,因此无效的关系会导致错误。

您能否提供有关 LinkedBusinessRules 业务问题的更多信息 业务规则

根据您提到的内容,我认为您的问题/业务规则类看起来更像这样。

注意事项, 而不是“一个问题涉及多个业务规则并且该业务规则有很多问题”

现在是“一个问题有一对多的业务规则”。

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<BusinessRule> BusinessRules { get; set; }
}

public class BusinessRule
{
    public int Id { get; set; }
    public string ValidationMessage { get; set; }
    public string ConditionValue { get; set; }
    public int BusinessRuleTypeId { get; set; }
    public virtual string BusinessRuleType { get; set; }
}

如果你仍然有问题并且有良好的数据库设计技能,请使用http://salardbcodegenerator.codeplex.com/之类的工具 然后你可以先创建你的数据库并基于它生成类。

【讨论】:

  • 如果业务规则不需要其他问题值,则您定义的关系很好。简单的业务规则:RuleType = maxlength, ConditionValue = 10;

    但是我有一些规则需要其他问题的值,例如: RuleType = SUM, QuestionIdsToSum = 1, 2, 3 我希望这有助于澄清我的意思。
【解决方案2】:

已解决!

第一个问题是因为我忘记添加 @Slauma 和 @Abhijit 指出的 BusinessRule 配置。

保存新表单的第二个问题实际上与我在问题中没有详细说明的一些问题有关。

【讨论】:

    猜你喜欢
    • 2012-05-18
    • 2011-07-20
    • 2011-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-20
    • 1970-01-01
    相关资源
    最近更新 更多