所有表[实体框架]中同一列的检查值

问题描述 投票:1回答:1

我在数据库中有100多个表,其中60多个表包含名为ShortCode nvarchar(12)的列,它们代表该记录的全局唯一代码。

现在有任何方法可以找到ShortCode值,例如AST_SHIP_FIRE存在于数据库的任何表中。

NoteShortCode是用户定义的。

当前我正在尝试下面的代码,它可以工作,但是我必须为所有表编写代码。

if (entities.Table1.Any(x =>  x.ShortCode.Trim().ToLower() == a.ShortCode.Trim().ToLower()) 
{return false;}
else if(entities.Table2.Any(x => x.ShortCode.Trim().ToLower() == a.ShortCode.Trim().ToLower()))
{return false;}
else if( entities.Talble3.Any(x => x.ShortCode.Trim().ToLower() == a.ShortCode.Trim().ToLower()))
{return false;}
.
.
.
else
{
//insert code
}

我认为可能有更有效的方法。

c# entity-framework linq-to-entities
1个回答
2
投票

好吧,也许不是很简单,但是让我们做吧!

首先为ShortCode属性定义一个接口,并由拥有它的任何实体来实现它:

public interface ITableWithShortCode
{
    public string ShortCode { get; set; }
}

public class Table1 : ITableWithShortCode
{
    public long Id { get; set; }
    public string ShortCode { get; set; }
}

public class Table2 : ITableWithShortCode
{
    public long Id { get; set; }
    public string ShortCode { get; set; }
}

现在使用Reflection的功率,您可以编写这样的方法:

public bool IsExistShortCode(string shortCode)
{
    using (var context = new AppDbContext())
    {
        /* 
           find all tables that are defined in your DbContext and are implemented ITableWithShortCode like:
           public DbSet<Table1> Table1 { get; set; }
           public DbSet<Table2> Table2 { get; set; }
           ...
        */
        var properties = typeof(AppDbContext).GetProperties()
            .Where(p => p.PropertyType.IsGenericType
                && typeof(ITableWithShortCode).IsAssignableFrom(p.PropertyType.GenericTypeArguments[0]));

        foreach (var property in properties)
        {
            var contextProp = (IQueryable<ITableWithShortCode>)property.GetValue(context);
            bool isExist = contextProp.Any(p => p.ShortCode == shortCode);
            if (isExist)
                return true;
        }

        return false;
    }
}

注意:您可以对此代码进行一些优化,我更喜欢将其保持在最简单的状态以显示该想法。但是在生产中,例如,您可以在启动时轻松缓存DbContext属性,然后再使用它

© www.soinside.com 2019 - 2024. All rights reserved.