vb.net 获取特定 linq 的名称到 sql 表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14886725/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting name of specific linq to sql table
提问by yu_ominae
I am using Linq to SQL to manipulate and MS Access database.
我正在使用 Linq to SQL 来操作 MS Access 数据库。
To speed up batch modifications I found it more efficient to directly execute queries using the datacontext, like so context.ExecutCommand("DELETE FROM [MyTable];"). For the sake of efficiency I'd like to make this an extension method, but I don't know how to retrieve the table name from the context...
为了加快批量修改,我发现使用数据上下文直接执行查询更有效,就像这样context.ExecutCommand("DELETE FROM [MyTable];")。为了效率起见,我想将此作为扩展方法,但我不知道如何从上下文中检索表名...
I know I could just pass the table name as a hardcoded string, something like:
我知道我可以将表名作为硬编码字符串传递,例如:
public static void DeleteAll(this Table<TEntity> MyTable)
{
string tableName = // retrieve MyTable's name
MyTable.Context.ExecuteCommand(string.Format("DELETE FROM [{0}];", tableName));
}
I got some way towards getting the table name, but need some help to get thsi working. So far I have:
我得到了一些获取表名的方法,但需要一些帮助才能使该表正常工作。到目前为止,我有:
var tableName = dc.MyTables.Context.GetTable(typeof(MyTable)).ElementType.Name;
But can't figure out how to retrieve the type of the entities in MyTablesso as not have to hardcode the argument of .GetTable()and make this usable for any table I pass in.
但是无法弄清楚如何检索实体的类型,MyTables以便不必硬编码 的参数.GetTable()并使它可用于我传入的任何表。
Any answer in C# or VB is fine. Thanks.
C# 或 VB 中的任何答案都很好。谢谢。
EDIT
编辑
To summarise what I am looking for is a way to get the entity type of a table, from the table itself. Something like Context.MyTable.GetEntityType()... if only it were that easy.
总结一下我正在寻找的是一种从表格本身获取表格实体类型的方法。类似的东西Context.MyTable.GetEntityType()……要是这么简单就好了。
回答by horgh
I am not sure if this works for EF, but I use this approach in Linq to Sql.
我不确定这是否适用于 EF,但我在 Linq to Sql 中使用这种方法。
You'll have to use attributes from System.Data.Linq.Mappingnamespace. If you open the *.designer.csfile, containing the definition of any Linq to Sql entity, you'll find a line like this above the declaration of the class:
您必须使用System.Data.Linq.Mapping命名空间中的属性。如果您打开*.designer.cs包含任何 Linq to Sql 实体定义的文件,您会在类的声明上方找到如下一行:
[global::System.Data.Linq.Mapping.TableAttribute(Name="YourTableName")]
So each entity class in Linq to Sql is marked with the TableAttributeattribute and it's Nameproperty contains the name you need. We may use this:
因此,Linq to Sql 中的每个实体类都标有TableAttribute属性,其Name属性包含您需要的名称。我们可以这样使用:
public static string GetTableName<TEntity>(this Table<TEntity> MyTable)
where TEntity : class
{
Type type = typeof(TEntity);
object[] temp = type.GetCustomAttributes(
typeof(System.Data.Linq.Mapping.TableAttribute),
true);
if (temp.Length == 0)
return null;
else
return (temp[0] as System.Data.Linq.Mapping.TableAttribute).Name;
}
回答by Rolf Kristensen
This should also work:
这也应该有效:
MyTable.Context.Mapping.GetTable(typeof(TEntity)).TableName
回答by yu_ominae
Since I do not have attributes on my tables, I need to get the entity name, which is what Linq to SQL will use in this case, so based on Konstantin Vasilicov's answer, the extension method becomes:
由于我的表上没有属性,我需要获取实体名称,这是 Linq to SQL 在这种情况下将使用的名称,因此根据 Konstantin Vasilicov 的回答,扩展方法变为:
public static string GetTableName<TEntity>(this Table<TEntity> MyTable) where TEntity : class
{
string name = string.Empty;
Type type;
object[] attributes;
type = typeof(TEntity);
attributes = type.GetCustomAttributes(typeof(TableAttribute), true);
if (attributes.Length > 0)
name = ((TableAttribute)attributes[0]).Name;
if (!string.IsNullOrEmpty(name))
return name;
return type.Name;
}

