我有一个客户模型:
public class Client
{
[Key]
public int id { get; set; }
[Required]
public string? Hostname { get; set; }
public ICollection<Software>? Softwares { get; set; }
}与软件模型:
public class Software
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int id { get; set; }
public string Name { get; set; }
public ICollection<Client>? Clients { get; set; }
},这应该是n到n的连接。如何将软件添加到客户端?
我尝试过的:
public async void add(Software software)
{
using (var repo = new ClientRepository(contextFactory.CreateDbContext()))
{
client.Softwares.Add(software);
await repo.Save(client);
}仓库:
public async Task Save(Client client)
{
_context.Clients.Update(client);
_context.SaveChanges();
}
}这适用于我添加的第一个软件,但如果我尝试添加第二个软件,则会出现以下错误:
SqlException:违反主键约束'PK_ClientSoftware‘。无法在对象'dbo.ClientSoftware‘中插入重复键。重复键值为(7003,5002).
。
发布于 2022-11-10 10:06:01
问:你每次都用新的()做一个新的吗?答:不,这是一个已经存在的软件。
那就是错误。
第一个SaveChanges()将开始跟踪它是否存在。您可以检查: Id为!= 0。
然后,当您稍后将其添加到同一个客户端时,您将得到重复的错误。
所以在你的代码里:
await add(software); // existing
software = new Software(); // add this 相关的,变化:
public async void add(Software software)至
public async Task Add(Software software)始终避免使用async void。
发布于 2022-11-08 14:42:12
似乎您正在添加多个相同的关系。在向客户端添加软件之前,请确保尚未存在关系,然后可以继续向客户端添加软件。
此外,你也可以改进你的实体。对主键使用更好的命名,并将[Key]属性添加到Software类中。
Client
public class Client
{
public Client()
{
Softwares = new HashSet<Software>();
}
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int ClientId { get; set; }
[Required]
public string Hostname { get; set; }
public ICollection<Software> Softwares { get; set; }
}Software
public class Software
{
public Software()
{
Clients = new HashSet<Client>();
}
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int SoftwareId { get; set; }
public string Name { get; set; }
public ICollection<Client> Clients { get; set; }
}检查关系是否已经存在:
// Get the client including its related softwares
var client = dbContext.Clients.Include(x => x.Softwares)
.FirstOrDefault(x => x.Hostname == "Ibrahim");
if (client != null)
{
// Check with unique property such as name of software
// If it does not exist in this current client, then add software
if (!client.Softwares.Any(x => x.Name == software.Name))
client.Softwares.Add(software);
dbContext.SaveChanges();
}注意:如果更新实体,请记住创建新的迁移和数据库。
https://stackoverflow.com/questions/74361667
复制相似问题