How do I upsert a record in ADO.NET EF 4.1?

后端 未结 5 1978
走了就别回头了
走了就别回头了 2021-01-01 23:22

I\'m trying to accomplish something really simple and I can\'t find how to do it using Entity Framework 4.1.

I want a controller method that accepts an object and

5条回答
  •  抹茶落季
    2021-01-02 00:04

    In most cases you do not need to explicitly set the EntityState.Modified unless you've disabled change tracking.

    The solution we took was to check the value of the entity identifier:

    if (entity.Id == default(int)) {
        // transient entity so insert
    } else {
        // update
    }
    

提交回复
热议问题