简体   繁体   English

LINQ To SQL C#更新类

[英]LINQ To SQL C# Update With Class

I have users table with generated LINQ-class with followed structure: 我的users表具有生成的LINQ类,其结构如下:

class User {
    int Id;
    string Login;
    string Password;
    string Mail;
    ...

Now I need to update specified columns (for ex. only Login and Password) and because I don't want to overwrite other fields, my code looks like this: 现在,我需要更新指定的列(例如,仅用于登录名和密码),并且由于我不想覆盖其他字段,因此我的代码如下所示:

public User UpdateUser(int userId, User newUser)
{
    User user = (from u in _context.Users
                where u.Id == userId
                select u).FirstOrDefault();

    if (newUser.Login != default(string)) user.Login = newUser.Login;
    if (newUser.Mail != default(string)) user.Mail = newUser.Mail;
    if (newUser.Password != default(string)) user.Password = newUser.Password;
    ...

    _context.SubmitChanges();
    return user;
}

And call it like this: 并这样称呼它:

var user = new User { Password = "123" };
UpdateUser(123, user);

For each field I need to write IF statement and I thinking that I doing something wrong. 对于每个字段,我需要编写IF语句,并且认为自己做错了什么。 Also because I am using comparsion with default(string) I cannot set empty values to rows. 另外,因为我使用default(string)所以无法将空值设置为行。 Please, tell me, what is right way to do this? 请告诉我,这样做的正确方法是什么?

PS: Please, sorry for my bad English. PS:拜托,抱歉我的英语不好。

You are misusing LINQ 2 SQL. 您正在滥用LINQ 2 SQL。 You shouldn't even have a generic UpdateUser method because you don't need it. 您甚至不需要通用的UpdateUser方法,因为您不需要它。 If you want to write a certain field of an entity, just do it: 如果要编写实体的某个字段,请执行以下操作:

var user = GetUser(userId);
user.Password = "123";

And you're done. 这样就完成了。 When you have made all changes to the object model, call SubmitChanges at the end. 对对象模型进行所有更改后,最后调用SubmitChanges It is not necessary to call it after each mutation. 每次突变后都不必调用它。

You are using LINQ 2 SQL as a CRUD repository but it is not meant to be one. 您正在使用LINQ 2 SQL作为CRUD存储库,但并不意味着它是一个。 It is meant to give you a live object model that you can treat like normal C# objects. 它旨在为您提供一个可以像对待普通C#对象一样对待的活动对象模型。 In the end you synchronize with the database by calling SubmitChanges . 最后,您通过调用SubmitChanges与数据库同步。

This is possible just with SubmitChanges: 仅通过SubmitChanges就可以实现:

This gets the user:
    var user=context.User.Where(m=>m.id == "xyz").FirstOrDefault();

This updates the above user:
user.Password = "xyz";
context.User.SubmitChanges();

I think you are looking into the wrong way for optimization. 我认为您正在寻找错误的优化方式。 An update command on single column isn't much different than on every other column than PK. 单列上的update命令与除PK以外的其他所有列都没有太大不同。 Your validation logics might take more time to process than your optimized update command. 验证逻辑可能比优化的update命令花费更多时间。

However if it is always the password that needs to be updated, you can do it this way : 但是,如果始终需要更新密码,则可以通过以下方式进行操作:

public User ChangePassword(int userId, string password)
{
  var user = new User() { Id = userId };
  _context.Users.Attach(user); 
  user.Password = password;
  _context.SaveChanges();

  return user;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM