简体   繁体   中英

Static method to get object from database

I'm using Entity Framework to get data from the database. Now I have a hard time wrapping my head around static methods. I have an Users class and have this method defined in this User class:

public static User Get(int userId)
{
    using (DbContext ctx = new DbContext())
    return ctx.Users.FirstOrDefault(x => x.UserId == userId);
}

Then I will use it like User currentUser = User.Get(1) and make changes to it before I save it again. Will this cause problems if there are more than 1 request virtually at the same time?

Potentially it will cause issues, especially when you attempt to update. There is not enough information here to give you a specific answer. Review locking for the database product you are using and EF documentation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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