Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# EF 6使用通用方法插入、更新和删除_C#_Entity Framework_Generics - Fatal编程技术网

C# EF 6使用通用方法插入、更新和删除

C# EF 6使用通用方法插入、更新和删除,c#,entity-framework,generics,C#,Entity Framework,Generics,我创建了web服务asp API宽度EF 6以连接sql server。。我创建了执行插入、更新和删除的通用方法。。方法是 protected enum ExecuteActions { Insert, Update, Delete } protected ResponseResult Execute<T>(T entity,ExecuteActions exAction) where T:class {

我创建了web服务asp API宽度EF 6以连接sql server。。我创建了执行插入、更新和删除的通用方法。。方法是

    protected enum ExecuteActions
    {
        Insert,
        Update,
        Delete
    }
protected ResponseResult Execute<T>(T entity,ExecuteActions exAction) where T:class
{
      var model = db.Set<T>();
      switch (exAction)
                {
                    case ExecuteActions.Insert:
                        model.Add(entity);
                        break;
                    case ExecuteActions.Update:
                        model.Attach(entity);
                        db.Entry(entity).State = System.Data.Entity.EntityState.Modified;
                        break;
                    case ExecuteActions.Delete:
                        model.Attach(entity);
                        db.Entry(entity).State = System.Data.Entity.EntityState.Deleted;
                        break;

                    default:
                        break;
                }
                  db.SaveChanges();    
}
受保护的枚举执行
{
插入,
更新,
删除
}
受保护的响应结果执行(T实体,ExecuteActions exAction),其中T:class
{
var model=db.Set();
开关(精密)
{
案例执行。插入:
模型。添加(实体);
打破
案例执行。更新:
模型。附加(实体);
db.Entry(entity.State=System.Data.entity.EntityState.Modified;
打破
案例执行。删除:
模型。附加(实体);
db.Entry(entity.State=System.Data.entity.EntityState.Deleted;
打破
违约:
打破
}
db.SaveChanges();
}
插入工作正常,但问题在于更新情况。。与某些实体和其他实体一起工作的不可理解的东西会抛出错误

附加类型为“XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX”的实体失败,因为相同类型的另一个实体已具有相同的主键值。如果图形中的任何实体具有冲突的键值,则在使用“Attach”方法或将实体状态设置为“Unchanged”或“Modified”时可能会发生这种情况。这可能是因为某些实体是新的,尚未收到数据库生成的键值。在这种情况下,使用“添加”方法或“添加”实体状态跟踪图形,然后根据需要将非新实体的状态设置为“未更改”或“已修改”

我找到了一些解决方案,解释了我必须使用(查找)来获取实体,而不是更新它。。但这与我的工作不相符。。因为我不知道方法中泛型类型的ID

我也试过了


在另一个项目中,它是通过类似这种方法工作的。。所以我认为实体框架元素(edmx)的问题。。因此,我删除了它并重新创建了它,但什么也没有发生

为您的实体类型创建一个接口,例如:

public interface IKeyed{
     int KeyValue{get;}
}
然后让您的实体实现接口,例如:

public partial class Foo : IKeyed{
     public int KeyValue {get {return this.Id;}}
     public int Id {get; set;}
}
然后更新泛型方法签名,将T定义为IKEY

protected ResponseResult Execute<T>(T entity,ExecuteActions exAction) 
                                    where T : class, IKeyed
当使用实体框架的“Find”方法时

entity.KeyValue