多重约束侵犯。 DB第一EF 4.1 [英] Multiplicity constraint violated. DB first EF 4.1

查看:188
本文介绍了多重约束侵犯。 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 = "TestUser@Test.org",
                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]

推荐答案

这个问题是不是由域实体引用的用户实体。
问题是创建者和更新的自引用。
它们都是不可为空...所以我不得不添加一个实际的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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆