Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Entity framework 使用BreezeJS添加新实体时为导航属性赋值_Entity Framework_Breeze - Fatal编程技术网

Entity framework 使用BreezeJS添加新实体时为导航属性赋值

Entity framework 使用BreezeJS添加新实体时为导航属性赋值,entity-framework,breeze,Entity Framework,Breeze,我有两种型号: 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 {

我有两种型号:

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);
}
readonly EFContextProvider\u context=new EFContextProvider();
[HttpGet]
公共字符串元数据()
{
返回_context.Metadata();
} 
[HttpGet]
公共IQueryable GetUsers()
{
返回_context.context.Users
。包括(“集团”);
}
[HttpPost]
公共保存结果保存更改(JObject用户)
{
返回_context.SaveChanges(用户);
}

添加新实体时,如何将导航属性指定给预设值,有什么建议吗?

可以显示与EF相关的查询吗?只需将它们附加到上面的帖子中即可。除了推到ICollection时,不应直接设置虚拟属性。您应该在用户上设置GroupID。另外,您有多少个管理员?如果您有多个,那么错误很明显。我尝试在用户的构造函数中设置用户的GroupID,但没有效果。它仍然说INSERT语句由于外键约束而失败。至于管理器,我尝试在一个管理器而不是多个管理器下执行所有操作,但失败了。你能显示与EF相关的查询吗?只需将它们附加到上面的帖子中。你不应该直接设置虚拟属性,除非在推到ICollection时。您应该在用户上设置GroupID。另外,您有多少个管理员?如果您有多个,那么错误很明显。我尝试在用户的构造函数中设置用户的GroupID,但没有效果。它仍然说INSERT语句由于外键约束而失败。至于经理,我试着在一个经理而不是多个经理的领导下做每件事,但失败了。