我有3个表(A - AB - B),它们是使用Code-First" Azure Mobile Service" 在移动服务和第三个表(AB)中使用模型创建的两个表(A和B)是由框架创建的,它将两个表连接为N:N关系问题是当我创建A的新实例时有一个引用另一个表B," InsertAsync(item);"尝试在另一个表中插入新实例导致冲突错误,因为实体已经存在,我想要的是仅插入关系表(AB)。我目前的解决方案是使用sql-string手动执行sql命令,我认为这不是一个好的解决方案。有没有办法可以通知框架,如果对象存在,只需插入关系表。谢谢你们的帮助。
答案 0 :(得分:1)
非常感谢您的回复,我仍然遇到同样的问题,请参阅下面的解决方案。 1-从天蓝色方面
//User Model
public class User
{
[JsonProperty(PropertyName = "Id")]
public string Id {get;set;}
[JsonProperty(PropertyName = "Companies")]
public IEnumerable<Company> Companies {get;set;}
}
//Company Model
public class Company
{
[JsonProperty(PropertyName = "Id")]
public string Id {get;set;}
[JsonProperty(PropertyName = "Users")]
public IEnumerable<User> Users {get;set;}
}
//Save Part
user.Companies.Add(ExistingCompany);
await UserTable.InsertAsync(newuser);
2-从Azure端
//The User Model
public class User : EntityData
{
//public virtual string CompanyId { get; set; } (Not working)
//....
public virtual ICollection<Company> Companies { get; set; }
//...
}
//User
public class User : EntityData
{
//public virtual string UserId { get; set; } (Not Working)
public virtual ICollection<User> Users { get; set; }
}
//dbContext, As you mentioned
modelBuilder.Entity<User>()
.HasMany(x => x.Companies)
.WithMany(x => x.Users)
.Map(c =>
{
c.MapLeftKey("UserId");
c.MapRightKey("CompanyId");
c.ToTable("UserCompanies");
});
modelBuilder.Entity<Company>()
.HasMany(x => x.Users)
.WithMany(x => x.Companies)
.Map(c =>
{
c.MapLeftKey("CompanyId");
c.MapRightKey("UserId");
c.ToTable("UserCompanies");
});
用户控制器类
// Not working
public async Task<IHttpActionResult> PostUser(User item)
{
MyLogger("PostCustomer", item);
User current = await InsertAsync(item);
return CreatedAtRoute("Tables", new { id = current.Id }, item);
}
// POST Working
public async Task<IHttpActionResult> PostUser(User item)
{
using ( mobileaContext context = new mobileaContext())
{
List<Company> attachedCompanies = new List<Company>();
for (int i =0; i< item.Companies.Count; i++)
{
Company company = new Company { Id = item.Companies.ElementAt<Company>(i).Id };
context.Companies.Attach(company);
attachedCompanies.Add(company);
}
item.Companies = attachedCompanies;
context.Users.Add(item);
int id = await context.SaveChangesAsync();
//User current = await InsertAsync(item);
return CreatedAtRoute("Tables", new { id = item.Id }, item);
}
}
不工作时出现错误消息: 消息=&#39; UserMessage =&#39;操作因冲突而失败:&#39;违反PRIMARY KEY约束&#39; PK_xyz.Companies&#39;。无法在对象&#39; xyz.Companies&#39;中插入重复的密钥。重复键值为(8842ff9e-fa29-4271-a6e1-8de18987f452)。
答案 1 :(得分:-1)
这是新的Tag类:
public class Tag
{
public int TagId{ get; set; }
public string Name { get; set; }
public ICollection<Post> Posts { get; set; }
}
&#13;
这是添加到Post类的新属性:
public ICollection<Tag> Tags { get; set; }
&#13;
您需要在dbContext中的OnModelCreating中定义
modelBuilder.Entity<Post>()
.HasMany(p => p.Tags)
.WithMany(t => t.Posts)
.Map(mc =>
{
mc.ToTable("PostJoinTag");
mc.MapLeftKey("PostId");
mc.MapRightKey("TagId");
});
&#13;
当您在数据库中插入包含B和B列表的A时,您需要添加
newA.ListOfB.Add(YourBFromDB)
&#13;
其中YourBFromDB是表中的对象。这样,您就有了两个解决方案:
向后端发送右边的
在后端,您将获得基于应用发送的ID的B