public List <User> GetUsers() { using (CodefirstContext db = new CodefirstContext()) { return(db.Users.ProjectTo <User>(Mapper.Configuration).ToList()); } }
public void Can_Initialise_Database_And_Product_Table() { //arrange //act using (CodefirstContext db = new CodefirstContext()) { var products = db.Products; //assert Assert.IsTrue(products.Count() > 0); } }
public void Can_Initialise_Database_And_Category_Table() { //arrange //act using (CodefirstContext db = new CodefirstContext()) { var categories = db.Categories; //assert Assert.IsTrue(categories.Count() > 0); } }
public User UpdateUser(User user) { using (CodefirstContext db = new CodefirstContext()) { var userEntity = db.Users.FirstOrDefault(u => u.ID == user.ID); if (userEntity != null) { userEntity.FirstName = user.FirstName; userEntity.LastName = user.LastName; userEntity.Email = user.Email; userEntity.IsActive = user.IsActive; if (db.SaveChanges() > 0) { return(Mapper.Map <User>(userEntity)); } } return(null); } }
public void Initialise_Database_ConnectionString_WithoutSeed() { //arrange //act. This will create database EFCodeFirst.TDD.NUnit in.\sqlexpress or local db based on the local system. using (CodefirstContext db = new CodefirstContext()) { CategoryEntity category = new CategoryEntity { ID = 1, Name = "Test1", IsActive = true }; db.Categories.Add(category); db.SaveChanges(); //asert Assert.IsTrue(db.Database.Connection.Database == "EFCodeFirst"); } }
public User AddUser(User user) { //convert domain model to entity if (user.ID == 0) { var userEntity = Mapper.Map <UserEntity>(user); using (CodefirstContext db = new CodefirstContext()) { db.Users.Add(userEntity); if (db.SaveChanges() > 0) { return(Mapper.Map <User>(userEntity)); } } return(null); } else { return(UpdateUser(user)); } }
public void Initialise_Database_NoConnectionString_WithoutSeed() { //arrange // ************Important :- commented connection string from the app.config file *******/ //act. Thsi will create database EFCodeFirst.TDD.NUnit in.\sqlexpress or local db based on the local system. using (CodefirstContext db = new CodefirstContext()) { CategoryEntity category = new CategoryEntity { ID = 1, Name = "Test", IsActive = true }; db.Categories.Add(category); //asert db.Categories.Add(category); db.SaveChanges(); //asert Assert.IsTrue(db.Database.Connection.Database == "EFCodeFirst.TDD.NUnit"); } }