public User Create(User entity) { UnitOfWork.Users.Add(entity); UnitOfWork.SaveChanges(); // return created entity from database return UnitOfWork.Users.Get(entity.Id); }
public User Update(User entity) { UnitOfWork.Users.Update(entity); UnitOfWork.SaveChanges(); // return updated entity from database return UnitOfWork.Users.Get(entity.Id); }
// Instruction: // 1. Create database in, for example, Management studio // 2. Install Entity Framework NuGet Package to VS-project // 3. Add to project -> New Item -> Data -> ADO.NET Entity Data Model // 4. Transfer Database to project and use by ORM-technology // 5. Edit database by code and create migrations + database updates // Migrations commands: // Enable-Migration (first calling only) // Add-Migration {Migration Name} // Update-Database public static void Main() { using (var db = new BlogContext()) { // entities Blog blog = new Blog(); Post post = new Post(); User user = new User(); UserLogInAttributes attributes = new UserLogInAttributes(); blog.Name = "Menaver's Blog"; blog.Url = "www.someblog.com/blog/1"; blog.Posts = new Post[1] { post }; blog.Users = new User[1] { user }; post.Title = "Awesome C#"; post.Content = "C# is awesome!"; user.Name = "Dmitry"; user.UserLogInAttributes = attributes; user.Blogs = new Blog[1] { blog }; attributes.Login = "******"; attributes.Password = "******"; db.Blogs.Add(blog); db.SaveChanges(); // LinqToSql example var query = from b in db.Blogs orderby b.Name select b; } }
public void Delete(User entity) { UnitOfWork.Users.Remove(entity); UnitOfWork.SaveChanges(); }