Multiplicity constraint violated. DB first EF 4.1

六月ゝ 毕业季﹏ 提交于 2019-12-10 22:25:35

问题


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();

    }

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]


回答1:


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.



来源:https://stackoverflow.com/questions/14088973/multiplicity-constraint-violated-db-first-ef-4-1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!