在 Azure 移动服务中创建一个引用另一个实体的实体
Creating an entity with a reference to an other entity in azure mobile service
我创建了一个基本上由 2 个实体和 2 个 TableController 组成的 azure 移动服务。这两个实体都具有 1:1 关系。
public class Entity1 : EntityData
{
public int Value { get; set; }
public DateTime Date { get; set; }
public string Name { get; set; }
public virtual Entity2 Reference { get; set; }
}
public class Entity2 : EntityData
{
public string Name { get; set; }
}
控制器是标准脚手架生成的控制器。当我尝试插入一个引用已存在的实体 2 的实体 1 实例时,我收到以下消息:
{"$id":"1","message":"The operation failed due to a conflict: 'Violation of
PRIMARY KEY constraint 'PK_Service.Entity2'. Cannot insert
duplicate key in object 'Service.Entity2'. The duplicate key
value is (32aec44a282e42b7bc51096052335dad).\r\nThe statement has been
terminated.'."}
我在请求正文中使用了以下 JSON:
{
"value": 1,
"date": "2015-04-27T06:51:47.641Z",
"name": "name",
"project": {
"id": "32aec44a282e42b7bc51096052335dad",
}
}
是否可以在 .NET 代码中使用已经存在的实体作为参考 First/Azure 移动服务?我不太确定这是一个与 EF CodeFirst 或 azure 移动服务相关的问题。
谢谢。
默认行为是 Entity1 的插入也尝试插入嵌套的引用。如果已插入该值,则可能会发生此类冲突。
一个选项是修改插入代码以将嵌套项目视为已插入,如 https://msdn.microsoft.com/en-us/data/jj592676.aspx
的 "Attaching an existing entity to the context" 部分
有关更多信息,此博客 post 可能会有帮助:
http://blogs.msdn.com/b/azuremobile/archive/2014/06/18/insert-update-related-data-with-1-n-relationship-using-net-backend-azure-mobile-services.aspx
我们试图做同样的事情,但并不关心更新相关实体本身。如果您想更新包含对相关实体的引用的实体,您只需将它们附加到上下文,您就不会收到任何 PK 违规错误:
我创建了一个基本上由 2 个实体和 2 个 TableController 组成的 azure 移动服务。这两个实体都具有 1:1 关系。
public class Entity1 : EntityData
{
public int Value { get; set; }
public DateTime Date { get; set; }
public string Name { get; set; }
public virtual Entity2 Reference { get; set; }
}
public class Entity2 : EntityData
{
public string Name { get; set; }
}
控制器是标准脚手架生成的控制器。当我尝试插入一个引用已存在的实体 2 的实体 1 实例时,我收到以下消息:
{"$id":"1","message":"The operation failed due to a conflict: 'Violation of
PRIMARY KEY constraint 'PK_Service.Entity2'. Cannot insert
duplicate key in object 'Service.Entity2'. The duplicate key
value is (32aec44a282e42b7bc51096052335dad).\r\nThe statement has been
terminated.'."}
我在请求正文中使用了以下 JSON:
{
"value": 1,
"date": "2015-04-27T06:51:47.641Z",
"name": "name",
"project": {
"id": "32aec44a282e42b7bc51096052335dad",
}
}
是否可以在 .NET 代码中使用已经存在的实体作为参考 First/Azure 移动服务?我不太确定这是一个与 EF CodeFirst 或 azure 移动服务相关的问题。
谢谢。
默认行为是 Entity1 的插入也尝试插入嵌套的引用。如果已插入该值,则可能会发生此类冲突。
一个选项是修改插入代码以将嵌套项目视为已插入,如 https://msdn.microsoft.com/en-us/data/jj592676.aspx
的 "Attaching an existing entity to the context" 部分有关更多信息,此博客 post 可能会有帮助: http://blogs.msdn.com/b/azuremobile/archive/2014/06/18/insert-update-related-data-with-1-n-relationship-using-net-backend-azure-mobile-services.aspx
我们试图做同样的事情,但并不关心更新相关实体本身。如果您想更新包含对相关实体的引用的实体,您只需将它们附加到上下文,您就不会收到任何 PK 违规错误: