C# 如何在DbContext中以编程方式查询DbSet?
我正在尝试构建一个控制器,为所有查找表的编辑请求提供服务。我的DbContext上有几个DbSet变量,它们来自C# 如何在DbContext中以编程方式查询DbSet?,c#,.net,entity-framework,ef-code-first,C#,.net,Entity Framework,Ef Code First,我正在尝试构建一个控制器,为所有查找表的编辑请求提供服务。我的DbContext上有几个DbSet变量,它们来自IdNamePairBase,例如: public DbSet<Country> Countries { get; set; } // Country derives from IdNamePairBase 然后,对于奖励积分,我需要从列表中获得IEnumerable如果表的名称与类型对应,您可以在DbContext上使用集合(类型) public IEnumerabl
IdNamePairBase
,例如:
public DbSet<Country> Countries { get; set; } // Country derives from IdNamePairBase
然后,对于奖励积分,我需要从
列表中获得IEnumerable
如果表的名称与类型对应,您可以在DbContext上使用集合(类型)
public IEnumerable<IdNamePairBase> GetNamedDbSet(string dbSetName)
{
var property = Type.GetType(dbSetName);
if (property == null || !property.CanRead)
{
throw new ArgumentException("DbSet named " + dbSetName + " does not exist." );
}
// at this point you might want to check that the property is an enumerable type
// and that the generic definition is convertible to IdNamePairBase by
// inspecting the property type. If you're sure that these hold, you could
// omit the check.
return Set(type).Cast<IdNamePairBase>();
}
public IEnumerable GetNamedDbSet(字符串dbSetName)
{
var属性=Type.GetType(dbSetName);
if(property==null | |!property.CanRead)
{
抛出新的ArgumentException(“名为“+dbSetName+”的DbSet不存在”);
}
//此时,您可能需要检查属性是否为可枚举类型
//并且泛型定义可以通过
//正在检查属性类型。如果确定这些属性有效,则可以
//省略支票。
返回集(type).Cast();
}
原始答案
如果集合的名称与属性名称匹配,则可以使用反射
public IEnumerable<IdNamePairBase> GetNamedDbSet( string dbSetName )
{
var property = this.GetType().GetProperty( dbSetName );
if (property == null || !property.CanRead)
{
throw new ArgumentException("DbSet named " + dbSetName + " does not exist." );
}
// at this point you might want to check that the property is an enumerable type
// and that the generic definition is convertible to IdNamePairBase by
// inspecting the property type. If you're sure that these hold, you could
// omit the check.
var result = new List<IdNamePairBase>();
foreach (var item in (IEnumerable)property.GetValue( this, null))
{
result.Add( (IdNamePairBase)item );
}
return result;
}
public IEnumerable GetNamedDbSet(字符串dbSetName)
{
var property=this.GetType().GetProperty(dbSetName);
if(property==null | |!property.CanRead)
{
抛出新的ArgumentException(“名为“+dbSetName+”的DbSet不存在”);
}
//此时,您可能需要检查属性是否为可枚举类型
//并且泛型定义可以通过
//正在检查属性类型。如果确定这些属性有效,则可以
//省略支票。
var result=新列表();
foreach(在(IEnumerable)property.GetValue(this,null)中的var项)
{
结果.添加((IdNamePairBase)项);
}
返回结果;
}
正如您所指出的,我从反射中获得了属性值,谢谢,但我希望先将动态类型转换为正确的DbSet类型。我将很快更新这个问题。@ProfK-这比我想象的要复杂,因为要获得正确的DbSet的强类型(泛型)方法,您必须知道类型。如果您知道类型,那么您应该能够简单地使用适当的属性。我怀疑一个非泛型的DbSet(可以很容易地完成)是否有效。GetNamedDbSet正是我所需要的,但visual studio找不到IdNamePairBase定义。我正在尝试为它创建DbContext扩展类!怎么办?帮助。@Add080bbA使用您自己的类名。这个答案中的一个是针对这个问题的。如果没有一个是确定的呢!假设我想要一个名为“雇员”或“公司”的数据库集的关键属性集合。我想我不明白解决办法
public IEnumerable<IdNamePairBase> GetNamedDbSet( string dbSetName )
{
var property = this.GetType().GetProperty( dbSetName );
if (property == null || !property.CanRead)
{
throw new ArgumentException("DbSet named " + dbSetName + " does not exist." );
}
// at this point you might want to check that the property is an enumerable type
// and that the generic definition is convertible to IdNamePairBase by
// inspecting the property type. If you're sure that these hold, you could
// omit the check.
var result = new List<IdNamePairBase>();
foreach (var item in (IEnumerable)property.GetValue( this, null))
{
result.Add( (IdNamePairBase)item );
}
return result;
}