什么是最有效的方法来实现更新行,如果它存在,否则插入新的行逻辑使用实体框架?或者这有什么规律吗?


当前回答

从实体框架4.3开始,在命名空间System.Data.Entity.Migrations中有一个AddOrUpdate方法:

public static void AddOrUpdate<TEntity>(
    this IDbSet<TEntity> set,
    params TEntity[] entities
)
where TEntity : class

医生说:

在调用SaveChanges时按键添加或更新实体。等效 到数据库术语中的“upsert”操作。此方法可以是 在使用迁移播种数据时很有用。


为了回答@Smashing1978的评论,我将从@Colin提供的链接中粘贴相关部分

The job of AddOrUpdate is to ensure that you don’t create duplicates when you seed data during development. First, it will execute a query in your database looking for a record where whatever you supplied as a key (first parameter) matches the mapped column value (or values) supplied in the AddOrUpdate. So this is a little loosey-goosey for matching but perfectly fine for seeding design time data. More importantly, if a match is found then the update will update all and null out any that weren’t in your AddOrUpdate.

也就是说,我有一种情况,我从外部服务中提取数据,并通过主键插入或更新现有值(并且我的消费者本地数据是只读的)-在生产中使用AddOrUpdate已经超过6个月了,到目前为止没有问题。

其他回答

使用Any检查现有行。

public static void insertOrUpdateCustomer(Customer customer)
{
    using (var db = getDb())
    {

        db.Entry(customer).State = !db.Customer.Any(f => f.CustomerId == customer.CustomerId) ? EntityState.Added : EntityState.Modified;
        db.SaveChanges();

    }

}

从实体框架4.3开始,在命名空间System.Data.Entity.Migrations中有一个AddOrUpdate方法:

public static void AddOrUpdate<TEntity>(
    this IDbSet<TEntity> set,
    params TEntity[] entities
)
where TEntity : class

医生说:

在调用SaveChanges时按键添加或更新实体。等效 到数据库术语中的“upsert”操作。此方法可以是 在使用迁移播种数据时很有用。


为了回答@Smashing1978的评论,我将从@Colin提供的链接中粘贴相关部分

The job of AddOrUpdate is to ensure that you don’t create duplicates when you seed data during development. First, it will execute a query in your database looking for a record where whatever you supplied as a key (first parameter) matches the mapped column value (or values) supplied in the AddOrUpdate. So this is a little loosey-goosey for matching but perfectly fine for seeding design time data. More importantly, if a match is found then the update will update all and null out any that weren’t in your AddOrUpdate.

也就是说,我有一种情况,我从外部服务中提取数据,并通过主键插入或更新现有值(并且我的消费者本地数据是只读的)-在生产中使用AddOrUpdate已经超过6个月了,到目前为止没有问题。

如果你知道你正在使用相同的上下文并且没有分离任何实体,你可以像这样创建一个通用版本:

public void InsertOrUpdate<T>(T entity, DbContext db) where T : class
{
    if (db.Entry(entity).State == EntityState.Detached)
        db.Set<T>().Add(entity);

    // If an immediate save is needed, can be slow though
    // if iterating through many entities:
    db.SaveChanges(); 
}

Db当然可以是一个类字段,或者方法可以是静态的和一个扩展,但这是基本的。

神奇的是在调用SaveChanges()时发生的,这取决于当前的EntityState。如果实体有一个EntityState。如果它有一个EntityState,那么它将被添加到数据库中。修改后,将在数据库中更新。所以你可以实现InsertOrUpdate()方法,如下所示:

public void InsertOrUpdate(Blog blog) 
{ 
    using (var context = new BloggingContext()) 
    { 
        context.Entry(blog).State = blog.BlogId == 0 ? 
                                   EntityState.Added : 
                                   EntityState.Modified; 

        context.SaveChanges(); 
    } 
}

更多关于EntityState的信息

如果你不能检查Id = 0来确定它是否是一个新实体,请检查Ladislav Mrnka的答案。

插入else更新

public void InsertUpdateData()
{
//Here TestEntities is the class which is given from "Save entity connection setting in web.config"
TestEntities context = new TestEntities();

var query = from data in context.Employee
            orderby data.name
            select data;

foreach (Employee details in query)
{
    if (details.id == 1)
    {
        //Assign the new values to name whose id is 1
        details.name = "Sanjay";
        details. Surname="Desai";
        details.address=" Desiwadi";
    }
    else if(query==null)
    {
        details.name="Sharad";
        details.surname=" Chougale ";
        details.address=" Gargoti";
    }
}

//Save the changes back to database.
context.SaveChanges();
}