本文介绍了违反多重约束。 DB首先是EF 4.1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下模型:

I have the following model:

我运行以下命令添加新用户:

the i run the following to add a new user:

    using (var context = new CamelotDB())
    {
        Console.WriteLine("Creating Test User");
        DomainEntity userToAdd = new DomainEntity()
        {
            EntityName = "Test User",
            EntityType = DomainEntity.eEntityType.User,
            EntityCreationDate = DateTime.Now,
            EntityLastUpdateDate = DateTime.Now,
            EntityCreatorUserID = 0,
            Creator = context.DomainEntities.Find(0),
            EntityUpdaterUserID = 0,
            Updater = context.DomainEntities.Find(0),
            EntityParentID = null,
            UserDetails = new User()
            {
                Username = "TestUser",
                Password = "123",
                Email = "[email protected]",
                FirstName = "Test",
                LastName = "User",
                Phone = "123456789"
            }
        };

        Console.WriteLine("Adding user to Database...");
        userToAdd = context.DomainEntities.Add(userToAdd);
        context.SaveChanges();

    }

我收到以下错误:
多重约束违反。角色用户的关系CamelotShiftManagement.DAL.DomainEnttyFK1具有多重性1或0..1。![输入图像描述]

And I get the following error:Multiplicity constraint violated. The role 'User' of the relationship 'CamelotShiftManagement.DAL.DomainEnttyFK1' has multiplicity 1 or 0..1.![enter image description here]

推荐答案

问题不是域实体引用的用户实体。
问题是创建者和更新者的自我引用。
他们都不是Nullable ...所以我必须添加一个实际的ID号到属性,并需要将一个实际的用户实体放在创建者和更新器的导航属性。

The problem was not the user entity referenced by the Domain Entity.The problem is the self references for the creator and updater.they are both not Nullable ... so i had to add an actual ID number to the properties and that required to place an actual User entity in the Navigation Properties of Creator and Updater.

这篇关于违反多重约束。 DB首先是EF 4.1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 09:05