如何在C#实体框架中动态地将属性强制转换为未知类并对其执行检查?
我试图将一系列函数放在一起以供实体框架使用。其思想是,我希望将所有泛型类传递到一个例程中,然后让例程为我“键入”它并相应地执行操作。我似乎不知道如何将这个类与它的PropertyType结合起来如何在C#实体框架中动态地将属性强制转换为未知类并对其执行检查?,c#,entity-framework-4,system.reflection,C#,Entity Framework 4,System.reflection,我试图将一系列函数放在一起以供实体框架使用。其思想是,我希望将所有泛型类传递到一个例程中,然后让例程为我“键入”它并相应地执行操作。我似乎不知道如何将这个类与它的PropertyType结合起来 public void AddUpdate(string classType, Object o) { //This gets the Type of my Class Object ok. Type mType = Type.GetType(Ge
public void AddUpdate(string classType, Object o)
{
//This gets the Type of my Class Object ok.
Type mType = Type.GetType(GetType().Namespace + "." + classType, true);
var meContext = new ClsContext(_ConnectionString);
//This retrieves the correct primary key for my the Class Object.
string key = FncGetPrimaryKey(meContext, classType + "s");
//I've tried this as a PropertyInfo as well instead of a Var
var keyID = o.GetType().GetProperty(key);
//I've tried this as Var as well as Dynamic
dynamic obj = o;
//Now I'm stuck, because I want to evaluate the property,
//but I get an error "<MyClass> does not contain a reference for 'keyID'
if (obj.keyID == 0) //ERROR ON THIS LINE
{
meContext.Entry(o).State = EntityState.Added;
}
else
{
meContext.Entry(o).State = EntityState.Modified;
}
meContext.SaveChanges();
}
public void AddUpdate(字符串类类型,对象o)
{
//这将获得我的类对象的类型ok。
Type mType=Type.GetType(GetType().Namespace+“+”类类型,true);
var meContext=新的ClsContext(_ConnectionString);
//这将为类对象检索正确的主键。
字符串键=FncGetPrimaryKey(meContext,类类型+“s”);
//我也尝试将其作为PropertyInfo而不是Var
var keyID=o.GetType().GetProperty(键);
//我已经尝试过这个变量和动态变量
动态obj=o;
//现在我被卡住了,因为我想评估财产,
//但我得到一个错误“不包含对'keyID'的引用”
if(obj.keyID==0)//此行出错
{
meContext.Entry(o).State=EntityState.Added;
}
其他的
{
meContext.Entry(o).State=EntityState.Modified;
}
meContext.SaveChanges();
}
虽然我不完全理解您为什么要这样做,但我想,我理解了您试图实现的目标。如果我想添加一些常见对象,我就是这样做的:
public void AddUpdate<T>(T obj) where T : IEntity
{
using(var ctx = new ClsContext(_ConnectionString))
{
if (obj.keyID == 0)
{
ctx.Entry(o).State = EntityState.Added;
}
else
{
ctx.Entry(o).State = EntityState.Modified;
}
ctx.SaveChanges();
}
}
public interface IEntity
{
int keyId {get;set;}
}
public void AddUpdate(T obj),其中T:entity
{
使用(var ctx=new ClsContext(_ConnectionString))
{
如果(obj.keyID==0)
{
ctx.Entry(o).State=EntityState.Added;
}
其他的
{
ctx.Entry(o).State=EntityState.Modified;
}
ctx.SaveChanges();
}
}
公共界面的开放性
{
int keyId{get;set;}
}
虽然我不完全理解您为什么要这样做,但我想,我理解了您试图实现的目标。如果我想添加一些常见对象,我就是这样做的:
public void AddUpdate<T>(T obj) where T : IEntity
{
using(var ctx = new ClsContext(_ConnectionString))
{
if (obj.keyID == 0)
{
ctx.Entry(o).State = EntityState.Added;
}
else
{
ctx.Entry(o).State = EntityState.Modified;
}
ctx.SaveChanges();
}
}
public interface IEntity
{
int keyId {get;set;}
}
public void AddUpdate(T obj),其中T:entity
{
使用(var ctx=new ClsContext(_ConnectionString))
{
如果(obj.keyID==0)
{
ctx.Entry(o).State=EntityState.Added;
}
其他的
{
ctx.Entry(o).State=EntityState.Modified;
}
ctx.SaveChanges();
}
}
公共界面的开放性
{
int keyId{get;set;}
}
试试:
尝试:
非常感谢您花时间将其粘贴。这是一个聪明的答案,但我真的不知道如何将其插入我的代码。我给了您+1并感谢您的响应。非常感谢您花时间将其粘贴。这是一个聪明的答案,但我真的不知道如何将其插入我的代码。我给了您+1并感谢您的响应.