问题
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