[英]Entity Framework Core 6 - many-to-many update
I'm trying to create a update for EF Core 6 many-to-many on SQL Server but I am really confused.我正在尝试在 SQL 服务器上为 EF Core 6 多对多创建更新,但我真的很困惑。 I have stock.cs
class and location.cs
class我有stock.cs
class 和location.cs
class
public class Stock : BaseModel
{
public Stock()
{
this.Locations = new List<Location>();
}
[Key]
public int Id { get; set; }
public string Name { get; set; } = string.Empty;
public string Guid { get; set; } = string.Empty;
public string RackBarNumber { get; set; } = string.Empty;
public string ShelveNumber { get; set; } = string.Empty;
public string ShelveName { get; set; } = string.Empty;
public virtual List<Location>? Locations { get; set; }
}
public class Location : BaseModel
{
public Location()
{
this.Stocks = new List<Stock>();
}
[Key]
public int Id { get; set; }
public string Name { get; set; } = string.Empty;
public string? Description { get; set; }
public virtual List<Stock>? Stocks { get; set; }
}
I use this as my DTO for getting all my current locations我用它作为我的 DTO 来获取我当前的所有位置
public class StockLocations
{
public Stock Stock { get; set; }
public virtual ICollection<Location> currentLocations { get; set; }
}
Now the StockController
is the piece of code which updates the fields, I am able to create and delete in the StockLocation
table that EF Core creates.现在StockController
是更新字段的一段代码,我可以在 EF Core 创建的StockLocation
表中创建和删除。 But when I try many updates at once it just goes haywire.但是当我一次尝试许多更新时,它就会变得混乱。
This is my last attempt:这是我最后一次尝试:
[HttpPut("{id}")]
public async Task<IActionResult> PutStock(int id, StockLocations stockLocation)
{
await _userService.ConfirmUser(User);
stockLocation.Stock.UpdatedAt = DateTime.Now;
List<Location> removedLocations = new List<Location>();
if (id != stockLocation.Stock.Id)
{
return BadRequest();
}
_context.Entry(stockLocation.Stock).State = EntityState.Modified;
try
{
await _context.SaveChangesAsync();
// Add new items to the database
foreach (var item in stockLocation.Stock.Locations)
{
if (!stockLocation.currentLocations.Any(x => x.Id == item.Id))
{
_context.Entry(item).State = EntityState.Modified;
await _context.SaveChangesAsync();
}
}
// Create a list of removed locations to be removed from the database
foreach (Location location in stockLocation.currentLocations)
{
if (!stockLocation.Stock.Locations.Any(x => x.Id == location.Id))
{
removedLocations.Add(location);
}
}
foreach (var item in removedLocations)
{
/*
Stock stock = _context.Stocks.Include(x => x.Locations).Single(x => x.Id == id);
Location locationToDelete = stock.Locations.Find(x => x.Id == item.Id);
stock.Locations.Remove(locationToDelete);
await _context.SaveChangesAsync();
*/
}
}
catch (DbUpdateConcurrencyException)
{
return NoContent();
}
return NoContent();
}
Anyone who is willing to tell me how I can approach this properly?任何愿意告诉我如何正确处理这个问题的人?
Since you need to update StockLocations I will recommend that tyou just pull the record from the database such as:由于您需要更新 StockLocations,我建议您只从数据库中提取记录,例如:
var record = await _context.StockLocations
.Include(a=>a.Location).Include(a=>a.Stock)
.FirstOrDefaultAsync(a=>a.id == id);
if(record == null) {
thrown new NotFoundException();
}
stockLocation.Stock.UpdatedAt = DateTime.Now;
await _context.SaveChangesAsync();
// other code
// add new location to the db if they don't exist
var locations = stockLocation.Location;
foreach(var loc in locations) {
var findLocation =
_context.Locations.FirstOrDefault(a=>a.Name.ToLower() ==
loc.Name.ToLower()) ;
if(findLocation == null){
// does not exist and can be added
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.