Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/270.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么通过实体框架保存更改会在通过dbContext添加一条记录时影响更多记录_C#_Wpf_Entity Framework_Sqlite - Fatal编程技术网

C# 为什么通过实体框架保存更改会在通过dbContext添加一条记录时影响更多记录

C# 为什么通过实体框架保存更改会在通过dbContext添加一条记录时影响更多记录,c#,wpf,entity-framework,sqlite,C#,Wpf,Entity Framework,Sqlite,我在ViewModelLocator中创建了如下服务: var context = new DbEntities(); SimpleIoc.Default.Register<IPaymentLogService>(() => new PaymentLogService(context)); public PaymentLogService(installmentsEntities context) { _context = context;

我在ViewModelLocator中创建了如下服务:

 var context = new DbEntities();
 SimpleIoc.Default.Register<IPaymentLogService>(() => new PaymentLogService(context));


public PaymentLogService(installmentsEntities context)
    {
        _context = context;
        _paymentLogRepository = new PaymentLogRepository(context);
    }
上述函数用于删除记录。affectedRows=2。PaymentLogs被引用到另一个对象,首先不添加新对象,而且被引用的LoanAgreement行被从数据库中删除

为什么?

准备添加的新记录如下所示:

public int Add(PaymentLog entity)
{
    _context.PaymentLogs.Add(entity);
    var affectedRows = _context.SaveChanges();

    return numberOfAddedItems;
}
 var result = new PaymentLog() { PayedAmount = newPayment.PaymentAmout, LoanAgreementID = newPayment.LoanID };
PaymentLog对象具有对LoanAgreement的引用。如上所述,在将其传递给函数之前,我只设置了引用的FK ID。我希望添加新记录。没有添加任何记录。一个已删除,saveChang返回的值为2

CREATE TABLE "PaymentLogs" ("ID" INTEGER PRIMARY KEY  AUTOINCREMENT  NOT NULL,"LoanAgreementID" INTEGER NOT NULL,
"PayedAmount" DECIMAL NOT NULL,

 FOREIGN KEY ([LoanAgreementID]) REFERENCES [LoanAgreements] ([ID]) 
        ON DELETE NO ACTION ON UPDATE NO ACTION)

感谢您的评论,我添加了日志上下文信息:

_context.Database.Log = s => Debug.WriteLine(s);
执行了以下更新

UPDATE [LoanAgreements] SET [LoanTypeID] = @p0 WHERE ([ID] = @p1);
-@p0:'0'类型=Int64 -@p1:'7'类型=Int64

以上更新是不必要的

当用户从WPF UI添加新的LoanAgreement时,如果成功,我将通过绑定如下方式重置UI:

private ProductTypeDto_productType; 公共产品类型到产品类型 { 获取{return\u productType;} 设置 { _产品类型=价值; //这导致不必要的更新: _loanAgreement.LoanTypeID=\u productType.ID; } }

然后,接下来的每一个动作都试图执行在上述设定器中所做的动作

直接绑定到上下文已知的db对象是个坏主意。 也许另一个问题与上下文本身有关,因为它只创建一次,并且一直存在。
这里没有足够的信息来回答这个问题。请发表一些我们可以复制的东西。试着在外面看这个问题。。。在调用Add方法之前,我不理解上下文是否存在。如果在调用Add之前有其他挂起的更改,则在调用SaveChanges时将执行这些更改。在调用Add之前,您使用_context做什么?因为_context是一个字段,所以它可以在其他地方使用。如果没有此类所有方法的代码,我们实际上无法修复此问题。您不断发布一些不完整的代码,这些代码无法提供完整的信息。这没用。阅读如何发布一些有用的代码。或者,在代码中的某个地方添加此项以跟踪EF生成的SQL,如果您无法理解它,请使用以下日志编辑您的帖子:_context.Database.Log=s=>Debug.WriteLines;