我正在嘗試撰寫一個具有物體Food和Order.
public class Order
{
[Key]
public Guid OrderId { get; set; }
public string UserName { get; set; }
public DateTime OrderTime { get; set; }
public ICollection<Food> Foods { get; set; }
public decimal OrderTotal { get; set; }
}
public class Food
{
[Key]
public Guid FoodId { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
public string PictureUrl { get; set; }
public double Rating { get; set; }
public string Comments { get; set; }
}
Order創建新時會出現問題,因為我Id從Foodin傳遞 Guid ICollection<Food> Foods。我使用 MediatR 來處理用戶操作,以及CreateOrderCommand類和Order類本身之間的映射。
捕獲的例外聲稱問題是 Guid fromICollection<Food>已經存在。否則我應該如何映射食物的集合,或者我應該將 Guid 更改為我的 Id 的簡單整數或字串?
“已添加具有相同密鑰的專案。密鑰:36708fa9-71df-4141-95d6-a73a65cd5dce”
這是 postman 在嘗試在 OrderRepository 中添加新訂單時捕獲的例外,其中 Key 是作為引數傳遞的 Food 中的 Guid。
public class CreateOrderCommand : IRequest<Guid>
{
public string UserName { get; set; }
public ICollection<Food> Foods { get; set; }
}
這是從前端應用程式接收的資料
public async Task<Guid> Handle(CreateOrderCommand request, CancellationToken cancellationToken)
{
var order = _mapper.Map<Order>(request);
order.OrderTime = DateTime.Now;
var user = await _userRepository.GetByUserNameAsync(request.UserName);
GeoCoordinate userLocation = new GeoCoordinate(user.Latitude, user.Longitude);
List<Restaurant> restaurants = (List<Restaurant>)await _restaurantRepository.ListAllAsync();
List<double> distances = new List<double>();
double minValue = 0;
int minValuePosition = -1;
//pronadji udaljenosti
for (int i = 0; i < restaurants.Count; i )
{
TimeSpan ts = DateTime.Now - restaurants[i].LastOrdered;
if(ts.TotalMinutes >= 15)
{
GeoCoordinate restaurantCoords = new GeoCoordinate(restaurants[i].Latitude, restaurants[i].Longitude);
distances.Add(userLocation.GetDistanceTo(restaurantCoords));
minValue = distances[i];
minValuePosition = i;
}
else
{
distances[i] = -1;
}
}
//pronadji najmanju udaljenost koja je na listi
for (int i = 0; i < distances.Count; i )
{
if (distances[i] < 0)
continue;
if (distances[i] < minValue)
{
minValue = distances[i];
minValuePosition = i;
}
}
if(minValuePosition == -1)
{
//nema slobodnih restorana
throw new Exception();
}
order.UserName = user.UserName;
order.Foods = request.Foods;
foreach(Food f in request.Foods)
{
order.OrderTotal = f.Price;
}
order = await _orderRepository.AddAsync(order);
return order.OrderId;
}
這是資料的處理程式。
uj5u.com熱心網友回復:
我假設您的 Food 物體代表了一個可用食物的目錄,每個目錄都有自己的 Guid 唯一識別符號。
現在,我認為,您正在嘗試在 Order 背景關系中創建一個訂單,該訂單“使用”在您的 Food 背景關系中標識的食品。
在這種情況下,如果同一種食物在同一個訂單中出現兩次,或者出現在其他訂單中,那么因為您使用食物資源的唯一識別符號作為訂單聚合中某個專案的唯一鍵,您將獲得 PK 約束像上面那樣的錯誤。
我認為您需要兩個獨立的物體:
- 食物
- OrderItem(指的是食物)
OrderItem 應該有自己的唯一鍵,其中有一列包含食物的 id。
public class Order
{
[Key]
public Guid OrderId { get; set; }
public string UserName { get; set; }
public DateTime OrderTime { get; set; }
public ICollection<OrderItem> OrderItems { get; set; }
public decimal OrderTotal { get; set; }
}
public class OrderItem
{
[Key]
public Guid OrderItemId { get; set; }
public Guid FoodId { get; set; }
public int Quantity { get; set; }
public decimal TotalPrice { get; set; }
}
public class Food
{
[Key]
public Guid FoodId { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
public string PictureUrl { get; set; }
public double Rating { get; set; }
public string Comments { get; set; }
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/360891.html
標籤:C# 。网 asp.net核心 .net-5 内存数据库
