[英]Entity Framework updating one record deletes another
I'm altering a business application to introduce a new entity SalesOrderReason.我正在更改业务应用程序以引入新实体 SalesOrderReason。 This will tie into another entity SalesOrder based on a SalesOrderReasonID field.
这将根据 SalesOrderReasonID 字段绑定到另一个实体 SalesOrder。
What I would like this code to do is to set the SalesOrderReasonID of the sales order (called from a button press - this is a desktop application).我希望这段代码做的是设置销售订单的 SalesOrderReasonID(通过按下按钮调用 - 这是一个桌面应用程序)。
In reality, what is happening is the first time it runs, it sets the SalesOrderReasonID - that's great.实际上,发生的是第一次运行时,它设置了 SalesOrderReasonID - 这很好。 When I set the SalesOrderReasonID for the next SalesOrder it sets it correctly however it deletes a previous SalesOrder from the database which is obviously unwanted behavior.
当我为下一个 SalesOrder 设置 SalesOrderReasonID 时,它会正确设置它,但是它会从数据库中删除以前的 SalesOrder,这显然是不需要的行为。
I've had a similar issue previously where the relationship was configured incorrectly (I had put WithOne instead of WithMany) which gave the same symptoms, because of this I believe it's related to the configuration.我以前遇到过类似的问题,其中关系配置不正确(我使用 WithOne 而不是 WithMany),它给出了相同的症状,因此我认为这与配置有关。
I've tried to configure it from the other perspective as well (SalesOrderReason config) without any success.我也尝试从另一个角度(SalesOrderReason 配置)对其进行配置,但没有成功。 (Here is that attempt, I created an ICollection SalesOrdersWithReason on SalesOrderReason for this)
(这是那个尝试,我为此在 SalesOrderReason 上创建了一个 ICollection SalesOrdersWithReason)
builder.HasMany(e => e.SalesOrdersWithReason)
.WithOne(s => s.SalesOrderReason)
.HasForeignKey(s => s.SalesOrderReasonID);
(Sorry if this is too much code I've tried to strip away what I thought was not relevant.) (对不起,如果这是太多代码,我试图删除我认为不相关的内容。)
This is the SalesOrder entity.这是 SalesOrder 实体。
public class SalesOrder
{
public int ID { get; set; }
public int SalesOrderReasonID { get; set; }
public SalesOrderReason SalesOrderReason { get; set; }
}
This is the SalesOrderReason entity这是 SalesOrderReason 实体
public class SalesOrderReason
{
public int Id { get; set; }
public string Reason { get; set; }
public DateTime DateCreated { get; set; }
}
This is my configuration using fluent API这是我使用 fluent API 的配置
public class SalesOrderConfiguration : IEntityTypeConfiguration<SalesOrder>
{
public void Configure(EntityTypeBuilder<SalesOrder> builder)
{
builder.HasKey(e => e.ID);
builder.Property(e => e.ID)
.ValueGeneratedOnAdd();
builder.HasOne(s => s.SalesOrderReason)
.WithMany()
.HasForeignKey(s => s.SalesOrderReasonID);
}
}
This is the Save method in my Repository这是我存储库中的 Save 方法
public void Save() => _context.SaveChanges();
It all goes wrong here when saving a change to the SalesOrderReasonID保存对 SalesOrderReasonID 的更改时,这里一切都出错了
private static void ChangeSalesOrderReason(SalesOrder salesOrder, int salesOrderReasonID)
{
salesOrder.SalesOrderReasonID = salesOrderReasonID;
SalesOrderRepository.Save();
}
Edit to include UI code as requested The SalesOrder in this use case is an existing object which comes from a Telerik gridview row, it gets selected then the user presses a button to update the SalesOrderReason on the SalesOrder to the one in a DropDownList.编辑以包含请求的UI 代码本用例中的 SalesOrder 是一个来自 Telerik gridview 行的现有对象,它被选中然后用户按下按钮将 SalesOrder 上的 SalesOrderReason 更新为 DropDownList 中的一个。
The UI is in VB.Net (Odd I know but it's part of a legacy project we're rebuilding) the above C# code is in separate C# projects added as reference in the VB project. UI 在 VB.Net 中(我知道很奇怪,但它是我们正在重建的遗留项目的一部分)上述 C# 代码位于单独的 C# 项目中,作为参考添加到 VB 项目中。
Private Sub UpdateReasonButton_Click(sender As Object, e As EventArgs) Handles UpdateReasonButton.Click
For Each salesOrderRow As GridViewRowInfo In SalesOrderGridView.SelectedRows
SalesOrderController.ChangeSalesOrderReason(salesOrderRow.DataBoundItem, ReasonList.SelectedValue)
Next
End Sub
按照 Panagiotis Kanavos 的评论,我调整了我的 DbContext 以使其寿命更短,我没有时间创建适当的“工作单元”模式,而只是在我的代码命中存储库时重新创建上下文似乎可以解决问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.