Add()方法为Code-First Entity Framework中的链接模型添加重复的行 [英] Add() method adding duplicate rows for linked models in Code-First Entity Framework

查看:44
本文介绍了Add()方法为Code-First Entity Framework中的链接模型添加重复的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是将贷款请求添加到数据库的操作:

Following is the action that is adding a Loan request to the database:

[HttpPost]
public ActionResult Add(Models.ViewModels.Loans.LoanEditorViewModel loanEditorViewModel)
{
    if (!ModelState.IsValid)
        return View(loanEditorViewModel);

    var loanViewModel = loanEditorViewModel.LoanViewModel;

    loanViewModel.LoanProduct = LoanProductService.GetLoanProductById(loanViewModel.LoanProductId); // <-- don't want to add to this table in database
    loanViewModel.Borrower = BorrowerService.GetBorrowerById(loanViewModel.BorrowerId); //<-- don't want to add to this table in database

    Models.Loans.Loan loan = AutoMapper.Mapper.Map<Models.Loans.Loan>(loanEditorViewModel.LoanViewModel);
    loanService.AddNewLoan(loan);
    return RedirectToAction("Index");
}

以下是 AddNewLoan()方法:

public int AddNewLoan(Models.Loans.Loan loan)
{
    loan.LoanStatus = Models.Loans.LoanStatus.PENDING;
    _LoanService.Insert(loan);

    return 0;
}

这是 Insert()

public virtual void Insert(TEntity entity)
{
    if (entity == null)
        throw new ArgumentNullException(nameof(entity));

    try
    {
        entity.DateCreated = entity.DateUpdated = DateTime.Now;
        entity.CreatedBy = entity.UpdatedBy = GetCurrentUser();

        Entities.Add(entity);
        context.SaveChanges();
    }
    catch (DbUpdateException exception)
    {
        throw new Exception(GetFullErrorTextAndRollbackEntityChanges(exception), exception);
    }
}

它已成功在 Loans 表中添加了一行,但也正在向 LoanProduct Borrower 表中添加行,如我在第一个代码中所示评论.

It is adding one row successfully in Loans table but it is also adding rows to LoanProduct and Borrower table as I showed in first code comments.

我检查了多次调用此操作和 Insert 方法的可能性,但是它们被调用了一次.

I checked the possibility of multiple calls to this action and Insert method but they are called once.

更新

我在这里遇到类似的问题,但在功能上却相反:实体未更新使用代码优先"方法

I am facing similar problem but opposite in functioning problem here: Entity not updating using Code-First approach

我认为这两个原因具有相同的变更跟踪.但是一个正在添加另一个并没有更新.

I think these two have same reason of Change Tracking. But one is adding other is not updating.

推荐答案

最后在@GertArnold共享的链接的帮助下重复的DataType在每个产品创建中都会创建

Finally with the help of the link shared by @GertArnold Duplicate DataType is being created on every Product Creation

由于我的所有模型都继承了 BaseModel 类,因此我修改了 Insert 方法,如下所示:

Since all my models inherit a BaseModel class, I modified my Insert method like this:

public virtual void Insert(TEntity entity, params BaseModel[] unchangedModels)
{
    if (entity == null)
        throw new ArgumentNullException(nameof(entity));

    try
    {
        entity.DateCreated = entity.DateUpdated = DateTime.Now;
        entity.CreatedBy = entity.UpdatedBy = GetCurrentUser();

        Entities.Add(entity);

        if (unchangedModels != null)
        {
            foreach (var model in unchangedModels)
            {
                _context.Entry(model).State = EntityState.Unchanged;
            }
        }

        _context.SaveChanges();
    }
    catch (DbUpdateException exception)
    {
        throw new Exception(GetFullErrorTextAndRollbackEntityChanges(exception), exception);
    }
}

并这样称呼它:

_LoanService.Insert(loan, loan.LoanProduct, loan.Borrower);

这篇关于Add()方法为Code-First Entity Framework中的链接模型添加重复的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆