我想用实体框架在Users表中添加用户。
我添加了所有的参考资料,即。
this.UserRolesReference.EntityKey = new System.Data.EntityKey("KEntities.UserRoles", "UserRoles", roleID);
this.OfficeMasterReference.EntityKey = new System.Data.EntityKey("KEntities.OfficeMaster", "SROCode", SROCode);
this.UserDesignationsReference.EntityKey = new System.Data.EntityKey("KEntities.UserDesignations", "UserDesignations", designationId);当我这么做的时候
context.AddObject(this.GetType().Name.ToString(), this);[ this is object of Users]它给了我一个错误
无法添加或附加对象,因为它的EntityReference具有与此对象的EntityKey不匹配的EntityKey属性值。
Users表仅与UserRoles、UserDesignations和OfficeMaster有关联。
仍然在导航属性的KModel.edmx表下的Users文件中,它显示了CustomPermissions,UserLog
CustomPermissions和UserLog与Users有关联,但我不会在它们中插入任何值。
提前谢谢你
发布于 2011-07-11 17:37:42
在实体框架中,不应该手动更改或设置RoleID,而是必须设置导航属性,RoleID将被正确更新。
Role adminRole = GetTheAdminRole(); // get instance of Role
User newUser =new User();
context.Users.Add(newUser);
newUser.Role = adminRole;
context.SaveChanges();https://stackoverflow.com/questions/6646224
复制相似问题