Skip to content

Instantly share code, notes, and snippets.

@mrstebo
Created June 2, 2020 12:20
Show Gist options
  • Select an option

  • Save mrstebo/b0386c07e2ead801fc88e6fd3f92ff29 to your computer and use it in GitHub Desktop.

Select an option

Save mrstebo/b0386c07e2ead801fc88e6fd3f92ff29 to your computer and use it in GitHub Desktop.
entity-framework-model-mapping-4
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();
}
}
}
@FrenchPupil
Copy link

FrenchPupil commented Jun 2, 2020

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment