Created
June 2, 2020 12:20
-
-
Save mrstebo/b0386c07e2ead801fc88e6fd3f92ff29 to your computer and use it in GitHub Desktop.
entity-framework-model-mapping-4
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System.Collections.Generic; | |
| using EntityFrameworkExamples.Data; | |
| using EntityFrameworkExamples.Models; | |
| namespace EntityFrameworkExamples.Services | |
| { | |
| public class UserService | |
| { | |
| private readonly ApplicationContext _context; | |
| public UserService(ApplicationContext context) | |
| { | |
| _context = context; | |
| } | |
| public IEnumerable GetUsers() | |
| { | |
| return _context.Users; | |
| } | |
| public User GetUser(long id) | |
| { | |
| return _context.Users.Find(id); | |
| } | |
| public User AddUser(User user) | |
| { | |
| var result = _context.Users.Add(user); | |
| _context.SaveChanges(); | |
| return result; | |
| } | |
| public int UpdateUser(long id, User user) | |
| { | |
| var original = GetUser(id); | |
| if (original == null) | |
| return 0; | |
| var entry = _context.Entry(original); | |
| entry.CurrentValues.SetValues(user); | |
| return _context.SaveChanges(); | |
| } | |
| public int RemoveUser(long id) | |
| { | |
| var user = GetUser(id); | |
| if (user == null) | |
| return 0; | |
| _context.Users.Remove(user); | |
| return _context.SaveChanges(); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi :) Maybe something interesting for you https://codenpaste.com
would love to chat with you : drop me a line if you have time (using the link in the website for security reason). Thanks