Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/267.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# 如果它存在,.Remove()它,如果不存在。Add()它_C#_Linq_Entity Framework Core - Fatal编程技术网

C# 如果它存在,.Remove()它,如果不存在。Add()它

C# 如果它存在,.Remove()它,如果不存在。Add()它,c#,linq,entity-framework-core,C#,Linq,Entity Framework Core,我有一个动作方法,它检查一个项目是否存在,如果存在,它将被删除。如果它不存在,就要添加它。它就像一个特定项目的开关: [HttpPost] [ValidateAntiForgeryToken] public async Task<IActionResult> FrontPageProduct(ViewModelFrontPageProduct frontPageProduct) { var fpp = new FrontPageProduct() {

我有一个动作方法,它检查一个项目是否存在,如果存在,它将被删除。如果它不存在,就要添加它。它就像一个特定项目的开关:

[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> FrontPageProduct(ViewModelFrontPageProduct frontPageProduct)
{
    var fpp = new FrontPageProduct()
    {
        ProductCategoryId = frontPageProduct.ProductCategoryId,
        ProductId = frontPageProduct.ProductId,
        SortOrder = 0
    };
    bool exists = _context.FrontPageProducts
        .Any(x => x.ProductCategoryId == frontPageProduct.ProductCategoryId 
        && x.ProductId == frontPageProduct.ProductId);
    if (exists)
    {
        var delete = (from d in _context.FrontPageProducts
                         where (d.ProductCategoryId == frontPageProduct.ProductCategoryId && 
                         d.ProductId == frontPageProduct.ProductId)
                         select d).FirstOrDefault();
        _context.Remove(delete);
    }
    else
    {
        _context.Add(fpp);
    }
    await _context.SaveChangesAsync();
    return RedirectToAction(nameof(Index), new { id = fpp.ProductCategoryId, tab = 2 });
}
[HttpPost]
[ValidateAntiForgeryToken]
公共异步任务FrontPageProduct(ViewModelFrontPageProduct FrontPageProduct)
{
var fpp=新的FrontPageProduct()
{
ProductCategoryId=frontPageProduct.ProductCategoryId,
ProductId=frontPageProduct.ProductId,
排序器=0
};
bool exists=\u context.FrontPageProducts
.Any(x=>x.ProductCategoryId==frontPageProduct.ProductCategoryId
&&x.ProductId==frontPageProduct.ProductId);
如果(存在)
{
var delete=(来自_context.FrontPageProducts中的d)
其中(d.ProductCategoryId==frontPageProduct.ProductCategoryId&&
d、 ProductId==frontPageProduct.ProductId)
选择d).FirstOrDefault();
_上下文。删除(删除);
}
其他的
{
_添加上下文(fpp);
}
wait_context.SaveChangesAsync();
返回RedirectToAction(nameof(Index),new{id=fpp.ProductCategoryId,tab=2});
}

现在,我觉得这有点冗长。是否有一种更短但仍然可读的方法来执行此操作?

您不必使用
Any
来确定它是否存在。基本上使用
FirstOrDefault
加载它(我使用了async,因为我看到您在save中使用async,您也可以在
FirstOrDefault
中使用它)。如果发现该实例,则您有一个实例,可以删除该实例而无需额外加载:

var fpp = new FrontPageProduct()
{
    ProductCategoryId = frontPageProduct.ProductCategoryId,
    ProductId = frontPageProduct.ProductId,
    SortOrder = 0
};

var fppDB = await _context.FrontPageProducts
    .FirstOrDefaultAsync(x => x.ProductCategoryId == frontPageProduct.ProductCategoryId && x.ProductId == frontPageProduct.ProductId);

if (fppDB != null)
{
    _context.Remove(fppDB);
}
else
{
    _context.Add(fpp);
}

await _context.SaveChangesAsync();

否则,您也可以使用SQL存储过程并从EF调用此存储过程。它将更加有效。

您不必使用任何
来确定它是否存在。基本上使用
FirstOrDefault
加载它(我使用了async,因为我看到您在save中使用async,您也可以在
FirstOrDefault
中使用它)。如果发现该实例,则您有一个实例,可以删除该实例而无需额外加载:

var fpp = new FrontPageProduct()
{
    ProductCategoryId = frontPageProduct.ProductCategoryId,
    ProductId = frontPageProduct.ProductId,
    SortOrder = 0
};

var fppDB = await _context.FrontPageProducts
    .FirstOrDefaultAsync(x => x.ProductCategoryId == frontPageProduct.ProductCategoryId && x.ProductId == frontPageProduct.ProductId);

if (fppDB != null)
{
    _context.Remove(fppDB);
}
else
{
    _context.Add(fpp);
}

await _context.SaveChangesAsync();

否则,您也可以使用SQL存储过程并从EF调用此存储过程。这样会更有效率。

这听起来像是一个非常糟糕的设计。你应该有一个删除和一个创建操作。这听起来像是一个非常糟糕的设计。您应该有一个删除和一个创建操作。