LINQ2SQL 实体 - 仅更新已更改的字段

发布于 2024-12-04 14:36:47 字数 738 浏览 0 评论 0原文

我希望在我的 MVC 3 项目中有一种更简单的方法来做到这一点。在我的数据库中,我有一个通过 LINQ2SQL 映射到我的应用程序中的 Customer 表。还有一个部分客户类,我在其中执行更新、查找等 - 其中我有一个像这样的更新方法:

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

我希望找到一种不太麻烦的方法,用中的相应字段更新 newCustomer 中的字段不同的客户。

有什么建议吗?谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

白日梦 2024-12-11 14:36:47

我认为您可以实现 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 ;
    }
}

,并按照以下操作:

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

或实现iClonable并设置newcustomer to customer.clone()。然后,无需附加customer,因为newcustomer已经连接。

在EF(4.1)中,我认为您只需要修改的实体:

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

更新:
好吧,似乎L2S需要实体的原始价值。在回复您的评论时,您有几个选择:使用时间戳列,返回一部分实体或将原始实体放在手中。在您的情况下,您已经拥有原始实体:

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

因此,您很可能可以做:

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

注意:我将重命名newcustomer Original -Customer如果我是您,因为它更相关到实体的状态。

这种方法的问题在于,您需要额外的数据库来获取原始客户(newCustomer在代码中)。看看在这里//geekswithblogs.net/michelotti/archive/2007/12/17/117791.aspx“ rel =“ nofollow”>在这里肯定此处查看如何使用时间戳列来防止额外的数据库跳闸。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文