C# 我们可以通过传递表值来调用实体框架查询吗?

C# 我们可以通过传递表值来调用实体框架查询吗?,c#,sql-server,entity-framework,asp.net-mvc-4,C#,Sql Server,Entity Framework,Asp.net Mvc 4,是将表名作为参数传递并使用make-generalized函数从db获取记录的任何方法 string table = "tbl_Category"; int Id = Class.getLastId(table); Class.aspx public static int getLastId(string table) { int lastID = 0; using (HatnEntities context = new HatnEntities(

是将表名作为参数传递并使用make-generalized函数从db获取记录的任何方法

string table = "tbl_Category";
int Id  = Class.getLastId(table);
Class.aspx

 public static int getLastId(string table)
    {

        int lastID = 0;
        using (HatnEntities context = new HatnEntities())
        {
            // Fetch Id of last record from table
                var result = (from c in context.tbl_Category.OrderByDescending(u => u.Id) select new { Id = c.Id }).FirstOrDefault();
                                                  ^
//any way to use table name from parameter value"+table+"


                if (result != null)
                {
                    lastID = Convert.ToInt32(result.Id);
                }
                obj.Id = lastID + 1;
                context.tbl_Category.Add(obj);
                context.SaveChanges();

        }
        return status;
    }
请告诉我是否可以使用Set(),这将要求传递表字符串参数时,必须从程序集中获取类型

context.Set<TypeFromTableStringParameter>() ...
context.Set()。。。
您可以使用Set(),这要求通过表字符串参数,您必须从程序集中获取类型

context.Set<TypeFromTableStringParameter>() ...
context.Set()。。。

如果您只想访问EF中的任何表格,则必须执行以下操作:

public static int getLastId<T>()
    where T : PrimaryKey
{
    using (HatnEntities context = new HatnEntities())
    {
        // Fetch Id of last record from table
        var result = (from c in context.Set<T>().OrderByDescending(u => u.Id) select new { Id = c.Id }).FirstOrDefault();

        var lastID = 0;
        if (result != null)
        {
            lastID = Convert.ToInt32(result.Id);
        }
        obj.Id = lastID + 1;
        context.Set<T>().Add(obj);
        context.SaveChanges();
    }

    // not sure where this comes from?
    return status;
}

public abstract class PrimaryKey
{
    public int Id { get; set; }
}
public static int getLastId()
其中T:PrimaryKey
{
使用(hatnetities context=new hatneties())
{
//从表中获取最后一条记录的Id
var result=(从context.Set()中的c开始。OrderByDescending(u=>u.Id)选择新的{Id=c.Id});
var-lastID=0;
如果(结果!=null)
{
lastID=Convert.ToInt32(result.Id);
}
对象Id=lastID+1;
context.Set().Add(obj);
SaveChanges();
}
//不知道这是从哪里来的?
返回状态;
}
公共抽象类主密钥
{
公共int Id{get;set;}
}

所有实体都需要继承(扩展)
PrimaryKey
,以便基于属性
Id

选择
Where
FirstOrDefault>(等),如果您只想访问EF中的任何表,则必须这样做:

public static int getLastId<T>()
    where T : PrimaryKey
{
    using (HatnEntities context = new HatnEntities())
    {
        // Fetch Id of last record from table
        var result = (from c in context.Set<T>().OrderByDescending(u => u.Id) select new { Id = c.Id }).FirstOrDefault();

        var lastID = 0;
        if (result != null)
        {
            lastID = Convert.ToInt32(result.Id);
        }
        obj.Id = lastID + 1;
        context.Set<T>().Add(obj);
        context.SaveChanges();
    }

    // not sure where this comes from?
    return status;
}

public abstract class PrimaryKey
{
    public int Id { get; set; }
}
public static int getLastId()
其中T:PrimaryKey
{
使用(hatnetities context=new hatneties())
{
//从表中获取最后一条记录的Id
var result=(从context.Set()中的c开始。OrderByDescending(u=>u.Id)选择新的{Id=c.Id});
var-lastID=0;
如果(结果!=null)
{
lastID=Convert.ToInt32(result.Id);
}
对象Id=lastID+1;
context.Set().Add(obj);
SaveChanges();
}
//不知道这是从哪里来的?
返回状态;
}
公共抽象类主密钥
{
公共int Id{get;set;}
}
所有实体都需要继承(扩展)
PrimaryKey
,以便基于属性
Id
选择
Where
FirstOrDefault
(等)