我有两种模式:
public partial class User
{
public int UserID { get; set; }
public string FullName { get; set;}
public int GroupID { get; set; }
public virtual Group Group {get; set;}
}
public partial class Group
{
public int GroupID { get; set;}
public string GroupName { get; set;}
}
每当我在UI中创建新用户时,我也会通过下拉列表将该组分配给用户。我将从下拉列表中的选定值中检索组实体,并在创建用户实体时将其分配给导航属性。
groupEntity = { GroupID: 1, GroupName: ABCD }; // group entity retrieved from dropdown
manager.createEntity('User' {
FullName: 'Boon',
Group: groupEntity
});
以下内容给我一个错误。
An Entity cannot be attached to an entity in another EntityManager. One of the two entities must be detached first.
所以我将实体与检索它的breeze管理器分离,并在创建时返回另一个错误。
Uncaught Error: Cannot attach this entity because the EntityType (Group:#Data) and MetadataStore associated with this entity does not match this EntityManager's MetadataStore.
实体框架代码
readonly EFContextProvider<EFEntities> _context = new EFContextProvider<EFEntities>();
[HttpGet]
public string Metadata()
{
return _context.Metadata();
}
[HttpGet]
public IQueryable<Data.User> GetUsers()
{
return _context.Context.Users
.Include("Group");
}
[HttpPost]
public SaveResult SaveChanges(JObject user)
{
return _context.SaveChanges(user);
}
有关如何在添加新实体时将导航属性指定给预设值的任何建议吗?