开发者

LINQ2SQL Entities - Updating only the fields that have changed

开发者 https://www.devze.com 2023-04-04 18:27 出处:网络
I was hoping there was an easier way to do this in my MVC 3 project.In my database, I have a Customer table that is mapped in my application via LINQ2SQL.There is also a partial customer class where I

I was hoping there was an easier way to do this in my MVC 3 project. In my database, I have a Customer table that is mapped in my application via LINQ2SQL. There is also a partial customer class where I perform updates, look-up etc - which where I have an update method like this:

public static void Update(Customer customer)
{
    if (customer == 开发者_高级运维null)
        return;

    using(var db = new DataBaseContext)
    {
        var newCustomer = db.Customers.Where(c => c.customer_id = customer.customer_id).SingleOrDefault();

        if(newCustomer == null) 
            return;

        newCustomer.first_nm = customer.first_nm;
        // ...
        // ...  Lot's of fields to update
        // ...
        newCustomer.phone_num = customer.phone_nm;

        db.SubmitChanges();
    }
}

What I was hoping to find was a less-cumbersome method to update the fields in newCustomer with the corresponding fields in customer that are different.

Any suggestions? Thanks.


I think you can implement IEqualityComparer:

public class Customer
{
    public string first_nm { get; set; }
    public int phone_num { get; set; }
}        
class CustomerComparer : IEqualityComparer<Customer>
{
    public bool Equals(Customer x, Customer y)
    {
        //Check whether the compared objects reference the same data.
        if (Object.ReferenceEquals(x, y)) return true;

        //Check whether any of the compared objects is null.
        if (Object.ReferenceEquals(x, null) || Object.ReferenceEquals(y, null))
            return false;

        //Check whether the customer' properties are equal.
        return x.first_nm  == y.first_nm && x.phone_num == y.phone_num ;
    }
}

and do it as follows:

if (newCustomer != customer)
{
    myDbContext.Customers.Attach(customer,true); // true means modified.
}  

Or implement ICloneable and set newCustomer to customer.Clone(). then there's no need to attach customer since newCustomer is already attached.

in EF(4.1), I think You just have to attach the entity as modified:

  myDbContext.Customers.AttachAsModified(customer, this.ChangeSet.GetOriginal(customer), myContext);

UPDATE:
Well, it seems like L2S needs original values of the entity. In reply to your comment, you have a couple choices: Using a timestamp column, returning a subset of entities, or having the original entity in your hand. In your scenario, you have the original entity already:

// This is your original entity
var newCustomer = db.Customers.Where(c => c.customer_id = customer.customer_id).SingleOrDefault();  

So you will most probably can do:

if (customer != newCustomer)
{
     myDbContext.Customers.Attach(customer, newCustomer);
}  

Note: I'd rename newCustomer to originalCustomer if I were you since it's more related to the entity's state.

The problem with this approach is that you have an extra trip to database to get your original customer (newCustomer in your code). Take a look at here, here and definitely here to see how you can use TimeStamp columns to prevent the extra database trip.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号