我写了以下方法。
public T GetByID(int id)
{
var dbcontext = DB;
var table = dbcontext.GetTable<T>();
return table.ToList().SingleOrDefault(e => Convert.ToInt16(e.GetType().GetProperties().First().GetValue(e, null)) == id);
}
基本上,它是通用类中的一个方法,其中T
是 DataContext 中的一个类。
该方法从 T ( GetTable
) 的类型中获取表,并检查输入参数的第一个属性(始终是 ID)。
这样做的问题是我必须先将元素表转换为列表才能GetType
对属性执行 a ,但这不是很方便,因为表的所有元素都必须枚举并转换为 a List
。
如何重构此方法以避免ToList
在整个表上出现 a ?
[更新]
我不能Where
直接在桌子上执行的原因是因为我收到了这个异常:
方法 'System.Reflection.PropertyInfo[] GetProperties()' 不支持对 SQL 的转换。
因为GetProperties
不能翻译成SQL。
[更新]
有人建议为T使用接口,但问题是T
参数将是在[DataContextName].designer.cs中自动生成的类,因此我无法使其实现接口(而且实现LINQ 的所有这些“数据库类”的接口;而且,一旦我将新表添加到 DataContext,文件将重新生成,从而丢失所有写入的数据)。
所以,必须有更好的方法来做到这一点......
[更新]
我现在已经像Neil Williams的建议那样实现了我的代码,但我仍然遇到问题。以下是代码的摘录:
界面:
public interface IHasID
{
int ID { get; set; }
}
DataContext [查看代码]:
namespace MusicRepo_DataContext
{
partial class Artist : IHasID
{
public int ID
{
get { return ArtistID; }
set { throw new System.NotImplementedException(); }
}
}
}
通用方法:
public class DBAccess<T> where T : class, IHasID,new()
{
public T GetByID(int id)
{
var dbcontext = DB;
var table = dbcontext.GetTable<T>();
return table.SingleOrDefault(e => e.ID.Equals(id));
}
}
在这一行抛出return table.SingleOrDefault(e => e.ID.Equals(id));
异常:异常是:
System.NotSupportedException: The member 'MusicRepo_DataContext.IHasID.ID' has no supported translation to SQL.
[更新] 解决方法:
在Denis Troller发布的答案和Code Rant 博客上的帖子链接的帮助下,我终于设法找到了解决方案:
public static PropertyInfo GetPrimaryKey(this Type entityType)
{
foreach (PropertyInfo property in entityType.GetProperties())
{
ColumnAttribute[] attributes = (ColumnAttribute[])property.GetCustomAttributes(typeof(ColumnAttribute), true);
if (attributes.Length == 1)
{
ColumnAttribute columnAttribute = attributes[0];
if (columnAttribute.IsPrimaryKey)
{
if (property.PropertyType != typeof(int))
{
throw new ApplicationException(string.Format("Primary key, '{0}', of type '{1}' is not int",
property.Name, entityType));
}
return property;
}
}
}
throw new ApplicationException(string.Format("No primary key defined for type {0}", entityType.Name));
}
public T GetByID(int id)
{
var dbcontext = DB;
var itemParameter = Expression.Parameter(typeof (T), "item");
var whereExpression = Expression.Lambda<Func<T, bool>>
(
Expression.Equal(
Expression.Property(
itemParameter,
typeof (T).GetPrimaryKey().Name
),
Expression.Constant(id)
),
new[] {itemParameter}
);
return dbcontext.GetTable<T>().Where(whereExpression).Single();
}