English 中文(简体)
实体框架代码一号至多图一号框架代码被忽略,也没有保存
原标题:Entity Framework Code First Many to Many Mapping is Being Ignored and also not saving

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方面经验较多的人的帮助。 我开始后悔首先选择代码,但我不知道是代码第一位的问题,还是我对它的理解。

谢谢!

问题回答

在我们进入实体框架之前 我相信 你们的模型没有正确反映你的商业问题 结果无效的关系 造成了错误

Can you please provide more information regarding the business problem around LinkedBusinessRules BusinessRules

基于你刚才提到的,我看到你的问题/商业规则课 更像这个样子。

Things to note, Instead of having one question has on to many business rule AND that business rule has many 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<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; }
}

If you still have problems and have good db design skills, use a tool such as http://salardbcodegenerator.codeplex.com/ Then you can create your db first and generate classes based on that.

解决了!

第一个问题是因为我忘记加上《业务规则》的配置,

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





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签