Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the problems that are easy to be ignored when updating data by Linq to SQL?

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Xiaobian to share with you Linq to SQL update data easy to ignore what problems, I hope you have something to gain after reading this article, let us discuss it together!

Sometimes, we still use Linq to Sql to update LINQ data. The steps to perform are: Get a record-> Update fields-> submitChanges()

Encountered a problem, the process is not wrong, but LINQ update data has not been updated to the database,

The approximate pseudocode is as follows:

public void UpdateUser(User user) { DataContext context = new DataContext("conn"); User existsUser = GetUser(user.ID); existsUser.Name = user.Name; //............. context.SubmitChanges(); }

Simple code, probably means to get a record, and then update the field, and then submitChanges, generally see no wrong, but!!! We have not found that our context is a private variable, and our GetUser although also obtained from the context, but it uses its own context, that is to say, for the program, it is two objects, so we here in the submitChanges, no matter how you change it is no effect, the database will always not change, My God, perhaps you will think this who does not know ah, but often we really ignore this point, remember the previous exam, Often difficult topics are basically all right, but the simpler the easier the topic, but often make mistakes, I hope these can enlighten you.

Well, now that we know why it went wrong, it's easy to fix it. There are two ways:

LINQ Update Data Method 1:

public void UpdateUser(User user) { DataContext context = new DataContext("conn"); //from the current context User existsUser = context.Users.SingleOrDefault(e => e.ID.Equals(user.ID); existsUser.Name = user.Name; //............. context.SubmitChanges(); }

LINQ Update Data Method 2://Set context to context common DataContext context = new DataContext("conn"); public void UpdateUser(User user) { User existsUser = GetUser(user.ID); existsUser.Name = user.Name; //............. context.SubmitChanges(); } After reading this article, I believe you have a certain understanding of "what problems are easy to ignore when Linq to SQL updates data". If you want to know more about relevant knowledge, please pay attention to the industry information channel. Thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report