Entity framework EF 5.0的通用存储库不工作
我有一个用于我的实体的通用存储库,我的所有实体(由代码生成项生成)都有一个实现IID接口的个性化部分,此时,我的所有实体都必须具有Int32 Id属性 所以,我的问题是更新,这是我的代码Entity framework EF 5.0的通用存储库不工作,entity-framework,repository,entity-framework-5,Entity Framework,Repository,Entity Framework 5,我有一个用于我的实体的通用存储库,我的所有实体(由代码生成项生成)都有一个实现IID接口的个性化部分,此时,我的所有实体都必须具有Int32 Id属性 所以,我的问题是更新,这是我的代码 public class RepositorioPersistencia<T> where T : class { public static bool Update(T entity) { try { using (var c
public class RepositorioPersistencia<T> where T : class
{
public static bool Update(T entity)
{
try
{
using (var ctx = new FisioKinectEntities())
{
// here a get the Entity from the actual context
var currentEntity = ctx.Set<T>().Find(((BLL.Interfaces.IID)entity).Id);
var propertiesFromNewEntity = entity.GetType().GetProperties();
var propertiesFromCurrentEntity = currentEntity.GetType().GetProperties();
for (int i = 0; i < propertiesFromCurrentEntity.Length; i++)
{
//I'am trying to update my current entity with the values of the new entity
//but this code causes an exception
propertiesFromCurrentEntity[i].SetValue(currentEntity, propertiesFromNewEntity[i].GetValue(entity, null), null);
}
ctx.SaveChanges();
return true;
}
}
catch
{
return false;
}
}
}
public class RepositorioPersistencia其中T:class
{
公共静态布尔更新(T实体)
{
尝试
{
使用(var ctx=new FisioKinectEntities())
{
//这里是一个从实际上下文获取实体的示例
var currentEntity=ctx.Set().Find((BLL.Interfaces.IID)entity.Id);
var propertiesFromNewEntity=entity.GetType().GetProperties();
var propertiesFromCurrentEntity=currentEntity.GetType().GetProperties();
对于(int i=0;i
有人能帮我吗?这让我抓狂。您可以使用EF API更新实体的值,如下所示
public static bool Update(T entity)
{
try
{
using (var ctx = new FisioKinectEntities())
{
var currentEntity = ctx.Set<T>().Find(((BLL.Interfaces.IID)entity).Id);
var entry = ctx.Entry(currentEntity);
entry.CurrentValues.SetValues(entity);
ctx.SaveChanges();
return true;
}
}
catch
{
return false;
}
}
公共静态bool更新(T实体)
{
尝试
{
使用(var ctx=new FisioKinectEntities())
{
var currentEntity=ctx.Set().Find((BLL.Interfaces.IID)entity.Id);
var分录=ctx分录(当前实体);
entry.CurrentValues.SetValues(实体);
ctx.SaveChanges();
返回true;
}
}
抓住
{
返回false;
}
}