using System.Collections.Generic; using System.Linq; using Microsoft.EntityFrameworkCore; using ProjectGrid.Models; namespace ProjectGrid.Data { public class DataAccessContext : DbContext, IUserRepository { public DataAccessContext(DbContextOptions options) : base(options) { } public DbSet Users { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity().ToTable("USER"); } public IEnumerable GetAll() { return Users.Select(data => data.ToModel()); } public UserModel Add(UserModel user) { // TODO: check if exist Users.Add(new UserData(user)); SaveChanges(); return user; } } public class UserData { public int? Id { get; set; } public string Name1 { get; set; } public string Name2 { get; set; } public string Email { get; set; } public UserData() { } public UserData(UserModel model) { Name1 = model.firstName; Name2 = model.lastName; Email = model.email; } public UserModel ToModel() { return new UserModel { firstName = Name1, lastName = Name2, email = Email }; } } }