我尝试将一个类序列化为 xml。序列化后,我想更新 statusid。我能够找到第一个订单并更改 statusId ,当转到第二个 orderid 时,这是我遇到模棱两可的匹配发现错误的地方。
这是主要方法:
using (var Context = new Context())
{
var orderRepo = new OrderRepository(Context);
foreach (var orderId in orderIds)
{
var order = orderRepo.Find(orderId);
order.orderStatusID = 5;
}
orderRepo.Save();
}
在 OrderRepository.cs
public Order Find(int id)
{
return _context.Orders.Find(id);
}
public void Save()
{
try
{
_context.SaveChanges();
}
catch (Exception ex)
{
_logger.Error(ex);
}
}
订单.cs:
[XmlRoot("Orders")]
[NotMapped]
public class OrderCollection
{
public OrderCollection() { Orders = new List<Order>(); }
[XmlElement("Order")]
[NotMapped]
public List<Order> Orders { get; set; }
}
[Serializable()]
public class Order
{
public int id { get; set; }
[XmlElement("date")]
public DateTime createdDate
{
get
{
return (_createdDate == default(DateTime))
? DateTime.Now
: _createdDate;
}
set { _createdDate = value; }
}
private DateTime _createdDate = default(DateTime);
public string firstName { get; set; }
public string lastName { get; set; }
[XmlIgnore]
public int orderStatusID { get; set; }
}