3

エンティティの汎用リポジトリがあり、すべてのエンティティ(コード生成アイテムによって生成される)には、IIDインターフェイスを実装するパーソナライズされたパーシャルがあります。この時点で、すべてのエンティティにInt32Idプロパティが必要です。

だから、私の問題はアップデートにあります、ここに私のコードがあります

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;
        }
    }
 }

誰かが私を助けることができますか?これは私を夢中にさせます。

4

1 に答える 1

2

次のように、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;
    }
}
于 2012-08-17T04:42:16.780 に答える