如何传递lambda表达式的参数用于Generic方法来检查MVC EF 4.0中的重复项 [英] How to pass parameters for lambda Expression for Generic method to check duplicates in MVC EF 4.0

查看:127
本文介绍了如何传递lambda表达式的参数用于Generic方法来检查MVC EF 4.0中的重复项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序中有10个设置屏幕。在每个人中,我必须检查重复的数据。
例如这里是一个示例代码。

  private void CheckDuplication(AIRLINE航空公司)
{
var AIRLINE = context.AIRLINEs.Where(f => f.ABBREVATION == airline.ABBREVATION).FirstOrDefault();
if(AIRLINE == null)
{
context.AIRLINEs.Add(airline);
}
else
{
ModelState.AddModelError(ABBREVATION,缩写已经存在);
}
}

每个项目使用几乎相同的代码建立。
我想让这个方法泛型,所以我可以调用它,我需要检查重复。
为此,我必须将参数传递给将设置Lambda Expression的通用函数。
我已经做了一个静态类从我应用程序的任何地方访问它。
问题是如何将参数传递给这样的类(包含泛型方法),它将为不同的表和列一般设置lambda表达式,并显示通用错误消息。
感谢Advance。

解决方案

实体框架已经提供了一种机制来检查数据库中是否存在实体,由 AddOrUpdate 方法。您可以使用这样的方法:

 使用System.Data.Entity.Migrations; 
...

public static string AddWhenNew< T>(DbContext context,
Expression< Func< T,object>> identifierExpression,T item)其中T:class
{
var error = string.Empty;
context.Set&T;()。AddOrUpdate(identifierExpression,item);
if(context.Entry(item).State!= System.Data.Entity.EntityState.Added)
{
error = string.Format({0}{1}已经存在,
item.GetType()。Name,
identifierExpression.Compile()(item));
}
返回错误;
}

在一些静态实用程序类中。



用法:

  var error = Util.AddWhenNew(context,a => a.ABBREVATION,airline) ; 
if(!string.IsNullOrEmpty(error))
{

等。



我故意检查 State!= EntityState.Added ,因为实体的状态可能是附加修改此处(修改为相同的 ABBREVATION 但另一个属性不同)。实际上,当实体不是添加时,它的状态被错误地报告为 Detached ,因为 bug AddOrUpdate



所以你必须确保在添加对象时只调用 SaveChanges ,然后处理上下文,否则可能会无意中保存其他修改。


I have more then 10 setup screens in my application. In each of them I have to check duplicates data. For instance here is one sample code.

         private void CheckDuplication(AIRLINE airline)
        {
            var AIRLINE = context.AIRLINEs.Where(f => f.ABBREVATION == airline.ABBREVATION).FirstOrDefault();
            if (AIRLINE == null)
            {
                context.AIRLINEs.Add(airline);
            }
            else
            {
            ModelState.AddModelError("ABBREVATION", "Abbreviation already exists.");
            }
        }

Almost same code is used through out the project for each setup. I want to make this method generic so that I can just call it any where I need to check Duplicates. To do so, I have to pass parameters to the generic function that will set "Lambda Expression". I have made a static class to access it from any where in my application. The Question is how to pass parameters to such class (containing generic method) that will set lambda expression generically for different Tables and Columns and Show generic Error Message.. Thanks in Advance.

解决方案

Entity Framework already provides a mechanism to check if an entity exists in the database, by the AddOrUpdate method. You could use this in a method like this:

using System.Data.Entity.Migrations;
...

public static string AddWhenNew<T>(DbContext context,
    Expression<Func<T, object>> identifierExpression, T item) where T : class
{
    var error = string.Empty;
    context.Set<T>().AddOrUpdate(identifierExpression, item);
    if (context.Entry(item).State != System.Data.Entity.EntityState.Added)
    {
        error = string.Format("{0} '{1}' already exists",
                              item.GetType().Name,
                              identifierExpression.Compile()(item));
    }
    return error;
}

in some static utility class.

Usage:

var error = Util.AddWhenNew(context, a => a.ABBREVATION, airline);
if (!string.IsNullOrEmpty(error))
{

etc.

I deliberately check State != EntityState.Added because the entity's state could be Attached or Modified here (modified when it's got the same ABBREVATION but another property differs). In reality though, when the entity is not Added, its state is incorrectly reported as Detached because of a bug in AddOrUpdate.

So you have to make sure to only call SaveChanges when the object is added, and then dispose the context, otherwise you may inadvertently save other modifications.

这篇关于如何传递lambda表达式的参数用于Generic方法来检查MVC EF 4.0中的重复项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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