private static Genre[] CreateTestGenres(int numberOfGenres, int numberOfAlbums, DbContext dbContext) { var albums = Enumerable.Range(1, numberOfAlbums * numberOfGenres).Select(n => new Album { AlbumId = n, Artist = new Artist { ArtistId = n, Name = "Artist " + n } }).ToList(); var genres = Enumerable.Range(1, numberOfGenres).Select(n => new Genre { Albums = albums.Where(i => i.AlbumId % numberOfGenres == n - 1).ToList(), GenreId = n, Name = "Genre " + n }); dbContext.AddRange(albums); dbContext.AddRange(genres); dbContext.SaveChanges(); return(genres.ToArray()); }
public static void Run(DbContext db) { if (!db.Set<Profile>().Any()) db.Add(new Profile { Name = "Alexandre Machado", Email = "*****@*****.**", Login = "******" }); if (!db.Set<Skill>().Any()) db.AddRange( new Skill { SkillName = "ASP.NET" }, new Skill { SkillName = "Ruby" }, new Skill { SkillName = "JavaScript" } ); if (!db.Set<Mastery>().Any()) db.AddRange( new Mastery { Code = 10, Name = "Iniciante", Description = "recordação não-situacional, reconhecimento decomposto, decisão analítica, consciência monitorada" }, new Mastery { Code = 20, Name = "Competente", Description = "recordação situacional, reconhecimento decomposto, decisão analítica, consciência monitorada" }, new Mastery { Code = 30, Name = "Proeficiente", Description = "recordação situacional, reconhecimento holítico, decisão analítica, consciência monitorada" }, new Mastery { Code = 40, Name = "Experiente", Description = "recordação situacional, reconhecimento holítico, decisão intuitiva, consciência monitorada" }, new Mastery { Code = 50, Name = "Mestre", Description = "recordação situacional, reconhecimento holítico, decisão intuitiva, consciência absorvida" }); db.SaveChanges(); }
public static void InitializeDbForTests(DbContext context) { context.AddRange(new Domain.Models.User[] { new Domain.Models.User { Id = 1, FirstName = "first-1", LastName = "last-1", Email = "*****@*****.**", Password = "******" }, new Domain.Models.User { Id = 2, FirstName = "first-2", LastName = "last-2", Email = "*****@*****.**", Password = "******" }, new Domain.Models.User { Id = 3, FirstName = "first-3", LastName = "last-3", Email = "*****@*****.**", Password = "******" }, new Domain.Models.User { Id = 4, FirstName = "first-4", LastName = "last-4", Email = "*****@*****.**", Password = "******" }, new Domain.Models.User { Id = 5, FirstName = "first-5", LastName = "last-5", Email = "*****@*****.**", Password = "******" }, }); context.AddRange(new Domain.Models.Phonebook[] { new Domain.Models.Phonebook { Id = 1, Name = "name-1", UserId = 1 }, new Domain.Models.Phonebook { Id = 2, Name = "name-2", UserId = 1 }, new Domain.Models.Phonebook { Id = 3, Name = "name-3", UserId = 1 }, new Domain.Models.Phonebook { Id = 4, Name = "name-4", UserId = 1 }, new Domain.Models.Phonebook { Id = 5, Name = "name-5", UserId = 1 }, }); context.AddRange(new Domain.Models.PhoneEntry[] { new Domain.Models.PhoneEntry { Id = 01, FirstName = "first-01", LastName = "last-01", CountryCode = "+91", Phone = "9875642301", OrganizationName = "org-name-01", Address = "address-01", PhonebookId = 1 }, new Domain.Models.PhoneEntry { Id = 02, FirstName = "first-02", LastName = "last-02", CountryCode = "+91", Phone = "9875642302", OrganizationName = "org-name-02", Address = "address-02", PhonebookId = 1 }, new Domain.Models.PhoneEntry { Id = 03, FirstName = "first-03", LastName = "last-03", CountryCode = "+91", Phone = "9875642303", OrganizationName = "org-name-03", Address = "address-03", PhonebookId = 1 }, new Domain.Models.PhoneEntry { Id = 04, FirstName = "first-04", LastName = "last-04", CountryCode = "+91", Phone = "9875642304", OrganizationName = "org-name-04", Address = "address-04", PhonebookId = 1 }, new Domain.Models.PhoneEntry { Id = 05, FirstName = "first-05", LastName = "last-05", CountryCode = "+91", Phone = "9875642305", OrganizationName = "org-name-05", Address = "address-05", PhonebookId = 1 }, }); context.SaveChanges(); }
/// <summary> /// Insert entity entries /// </summary> /// <param name="entities">Entity entries</param> /// <param name="publishEvent">Whether to publish event notification</param> public virtual async Task InsertAsync(IList <TEntity> entities) { if (entities == null) { throw new ArgumentNullException(nameof(entities)); } _context.AddRange(entities); }
public static void SeedDatabase(this DbContext dbContext) { dbContext.Database.EnsureCreated(); dbContext.Database.EnsureDeleted(); dbContext.Database.EnsureCreated(); dbContext.AddRange(PersonSeed); dbContext.AddRange(CommentSeed); dbContext.SaveChanges(); }
protected virtual void Seed(DbContext context) { context.Add(CreateBlogAndPosts <BlogAuto, PostAuto>()); context.AddRange(CreatePostsAndBlog <BlogAuto, PostAuto>()); context.Add(CreateBlogAndPosts <BlogFull, PostFull>()); context.AddRange(CreatePostsAndBlog <BlogFull, PostFull>()); context.Add(CreateBlogAndPosts <BlogFullExplicit, PostFullExplicit>()); context.AddRange(CreatePostsAndBlog <BlogFullExplicit, PostFullExplicit>()); if (context.Model.GetPropertyAccessMode() != PropertyAccessMode.Property) { context.Add(CreateBlogAndPosts <BlogReadOnly, PostReadOnly>()); context.AddRange(CreatePostsAndBlog <BlogReadOnly, PostReadOnly>()); context.Add(CreateBlogAndPosts <BlogReadOnlyExplicit, PostReadOnlyExplicit>()); context.AddRange(CreatePostsAndBlog <BlogReadOnlyExplicit, PostReadOnlyExplicit>()); context.Add(CreateBlogAndPosts <BlogWriteOnly, PostWriteOnly>()); context.AddRange(CreatePostsAndBlog <BlogWriteOnly, PostWriteOnly>()); context.Add(CreateBlogAndPosts <BlogWriteOnlyExplicit, PostWriteOnlyExplicit>()); context.AddRange(CreatePostsAndBlog <BlogWriteOnlyExplicit, PostWriteOnlyExplicit>()); context.Add(CreateBlogAndPosts <BlogFields, PostFields>()); context.AddRange(CreatePostsAndBlog <BlogFields, PostFields>()); } context.SaveChanges(); }
protected virtual void Seed(DbContext context) { context.AddRange( new IntKey { Id = 77, Foo = "Smokey" }, new StringKey { Id = "Cat", Foo = "Alice" }, new CompositeKey { Id1 = 77, Id2 = "Dog", Foo = "Olive" }, new BaseType { Id = 77, Foo = "Baxter" }, new DerivedType { Id = 78, Foo = "Strawberry", Boo = "Cheesecake" }); var entry = context.Entry(new ShadowKey { Foo = "Clippy" }); entry.Property("Id").CurrentValue = 77; entry.State = EntityState.Added; context.SaveChanges(); }
/// <summary> /// Method to seed beer entities to the context /// </summary> /// <param name="context">Brewdogger database context</param> private static void AddBeers(DbContext context) { // Seed beers var beer1 = new Beer() { BeerId = 1, Abv = 7.2, Ibu = 80, BeerName = "Hexagenia", BeerStyle = BeerStyle.Ipa, BreweryId = 1 }; var beer2 = new Beer() { BeerId = 2, Abv = 9.2, Ibu = 120, BeerName = "Widowmaker", BeerStyle = BeerStyle.DoubleIpa, BreweryId = 1 }; var beer3 = new Beer() { BeerId = 3, Abv = 5.5, Ibu = 75, BeerName = "Sierra Nevada Pale Ale", BeerStyle = BeerStyle.PaleAle, BreweryId = 2 }; // Seed entities context.AddRange(beer1, beer2, beer3); }
public void Populate() { _db.AddRange(new List <BusinessRuleRecord> { new BusinessRuleRecord { DelayedDaysStart = 1, DelayedDaysEnd = 3, Penalty = 0.02, InterestPerDay = 0.001, Id = "BR-1" }, new BusinessRuleRecord { DelayedDaysStart = 3, DelayedDaysEnd = 5, Penalty = 0.03, InterestPerDay = 0.002, Id = "BR-2" }, new BusinessRuleRecord { DelayedDaysStart = 5, DelayedDaysEnd = int.MaxValue, Penalty = 0.05, InterestPerDay = 0.003, Id = "BR-3" }, }); _db.SaveChanges(); }
public async Task GetAllSkipTakeAsyncTest_GoodFlow( [ProjectDataSource(100)] List <Project> projects, [UserDataSource(100)] List <User> users) { // Set project - user relation // Set some properties to be able to search // And seed database projects = SetStaticTestData(projects, users); DbContext.AddRange(projects); await DbContext.SaveChangesAsync(); List <Project> retrieved; // Tests retrieved = await Repository.GetAllWithUsersAsync(0, 1); Assert.AreEqual(1, retrieved.Count, "Get all with skip take failed"); retrieved = await Repository.GetAllWithUsersAsync(0, 10); Assert.AreEqual(10, retrieved.Count, "Get all with skip take failed"); retrieved = await Repository.GetAllWithUsersAsync(10, 10); Assert.AreEqual(10, retrieved.Count, "Get all with skip take failed"); }
public async Task FindWithUserAndCollaboratorsAsyncTest_BadFlow_NoUsers( [ProjectDataSource(100)] List <Project> projects, [CollaboratorDataSource(400)] List <Collaborator> collaborators) { // Seeding projects = SetStaticTestData(projects); int i = 0; foreach (Project project in projects) { project.Collaborators = new List <Collaborator>(); for (int a = i; a < collaborators.Count; a++) { project.Collaborators.Add(collaborators[a]); i = a; } } DbContext.AddRange(projects); await DbContext.SaveChangesAsync(); // Testing Project retrieved = await Repository.FindWithUserAndCollaboratorsAsync(1); // Retrieved is null because there are no users and users is required Assert.IsNull(retrieved); }
public TheaterProgramBindingModel Edit(TheaterProgramBindingModel model) { var programDb = DbContext.TheaterProgram.Find(model.Id); if (programDb == null) { model.SetError("Program not found in database"); return(model); } Mapper.Map(model, programDb); var moviesInTheaterToDelete = DbContext.MoviesInTheaters .Where(mt => mt.TheaterProgramId == model.Id); var moviesInTheaterToKeep = model.SelectedMoviesIds .Select(movieId => new MoviesInTheater { MovieId = movieId, TheaterProgramId = model.Id.Value }).ToList(); DbContext.RemoveRange(moviesInTheaterToDelete); DbContext.AddRange(moviesInTheaterToKeep); DbContext.TheaterProgram.Update(programDb); DbContext.SaveChanges(); Mapper.Map(programDb, model); return(model); }
public void AddRange(object[] objs) { try { _poshContext.AddRange(objs); } catch (InvalidCastException) { List <object> NewObjectList = new List <object>(); foreach (var instance in objs) { NewObjectList.Add(ConvertType(instance)); } _poshContext.AddRange(NewObjectList); } }
public TransactionDetails Update(TransactionDetails transaction) { var entity = Transactions.Single(x => x.Id == transaction.Id); entity.Amount = transaction.Amount; entity.AccountNumber = transaction.AccountNumber; entity.TransactionIdentifier = transaction.TransactionIdentifier; entity.Date = transaction.Date; entity.Partner = transaction.Partner is null ? null: Partners.SingleOrDefault(x => x.Id == transaction.Partner.Id); entity.Condominium = transaction.Condominium is null ? null: Condominiums.SingleOrDefault(x => x.Id == transaction.Condominium.Id); DbContext.RemoveRange(DbContext.TransactionTags.Where(x => x.Transaction == entity)); entity.Tags = transaction.Tags .Select(x => new TransactionTag() { Label = x.Label, Ratio = x.Rate }) .ToList(); DbContext.AddRange(entity.Tags); DbContext.SaveChanges(); return(entity.ToModelDetails()); }
public async Task FindWithUserAndCollaboratorsAsyncTest_GoodFlow( [ProjectDataSource(100)] List <Project> projects, [UserDataSource(100)] List <User> users, [CollaboratorDataSource(400)] List <Collaborator> collaborators) { // Seeding projects = SetStaticTestData(projects, users); int i = 0; foreach (Project project in projects) { project.Collaborators = new List <Collaborator>(); for (int a = i; a < collaborators.Count; a++) { project.Collaborators.Add(collaborators[a]); i = a; } } DbContext.AddRange(projects); await DbContext.SaveChangesAsync(); // Testing Project retrieved = await Repository.FindWithUserAndCollaboratorsAsync(1); Assert.AreEqual(projects[0].Id, retrieved.Id); Assert.AreEqual(projects[0].Name, retrieved.Name); Assert.AreEqual(projects[0].ShortDescription, retrieved.ShortDescription); Assert.AreEqual(projects[0].Description, retrieved.Description); Assert.AreEqual(projects[0].Uri, retrieved.Uri); Assert.AreEqual(projects[0].User.Name, retrieved.User.Name); Assert.AreEqual(projects[0].Collaborators[0].FullName, retrieved.Collaborators[0].FullName); }
private static void SeedProducts(DbContext context) { if (!context.Set <Product>().Any()) { var id = Guid.NewGuid(); var products = new List <Product> { new Product { Description = "Best phone OMG!", DisplayName = "Samsung S9", Price = 850.00M }, new Product { Description = "Best laptop OMFG!", DisplayName = "Asus Aspire V Nitro", Price = 1199.99M }, new Product { Description = "Battlestation gaming pc", DisplayName = "RtB created battlestation", Price = 2000.00M }, new Product { Description = "Smatphone killer!", DisplayName = "OnePlus 6", Price = 645.50M } }; context.AddRange(products); context.SaveChanges(); } }
//private static void SeedUsers(EFContext context) //{ // if (!context.Users.Any()) // { // var users = new List<User> // { // new User(){ Email = "test", Password = "******", // RoleId = context.Roles.FirstOrDefault(x=>x.RoleName == "user").Id }, // new User(){ Email = "admin", Password = "******", // RoleId = context.Roles.FirstOrDefault(x=>x.RoleName == "administrator").Id } // }; // context.AddRange(users); // context.SaveChanges(); // } //} private static void SeedCategories(DbContext context) { if (!context.Set <Category>().Any()) { var id = Guid.NewGuid(); var categories = new List <Category> { new Category() { Name = "Computers", Id = id }, new Category() { Name = "Phones" }, new Category() { Name = "Notebooks", ParentCategoryId = id }, new Category() { Name = "Stacionary computers", ParentCategoryId = id } }; context.AddRange(categories); context.SaveChanges(); } }
public TransactionDetails Create(TransactionDetails transaction) { var entity = new Entities.Transaction(); if (transaction.Partner != null) { entity.Partner = Partners .Single(x => x.Id == transaction.Partner.Id); } if (transaction.Condominium != null) { entity.Condominium = Condominiums .Single(x => x.Id == transaction.Condominium.Id); } entity.Tags = transaction.Tags .Select(x => new TransactionTag() { Label = x.Label, Ratio = x.Rate }) .ToList(); DbContext.AddRange(entity.Tags); DbContext.Add(transaction); DbContext.SaveChanges(); return(entity.ToModelDetails()); }
/// <summary> /// Adds the data from this data source to the given <see cref="DbContext"/>. /// </summary> /// <returns>The entities that have been added to the <paramref name="context"/>.</returns> public IEnumerable <object> AddToContext(DbContext context) { IEnumerable <object> entities = GetData(amount); context.AddRange(entities); return(entities); }
/// <summary> /// Entities ekle /// </summary> /// <param name="entities">Eklenen Entity</param> /// <returns>İşlem başaarılı ise true değilse false</returns> public bool Insert(List <TEntity> entities) { DbContext.AddRange(entities); SaveChanges(); return(true); }
public void AddRange(IEnumerable <T> data, bool save = true) { _context.AddRange(data); if (save) { Save(); } }
public override void Given() { var tlSpecialisms = new TlSpecialismBuilder().BuildList(_awardingOrganisation); DbContext.AddRange(tlSpecialisms); DbContext.SaveChanges(); _data = tlSpecialisms.FirstOrDefault(); }
public IEnumerable <Book> Insert(IEnumerable <Book> entities) { _dbContext.AddRange(entities); _dbContext.SaveChanges(); return(entities); }
public Task Persist(Envelope[] envelopes) { var outgoing = envelopes.Select(x => new OutgoingEnvelope(x)); _db.AddRange(outgoing); return(Task.CompletedTask); }
public void AddRange(IEnumerable <TEntity> entities, bool saveNow = true) { DbContext.AddRange(entities); if (saveNow) { DbContext.SaveChanges(); } }
public async Task GetAllAsync_multiple([RoleDataSource(10)] List <Role> roles) { DbContext.AddRange(roles); await DbContext.SaveChangesAsync(); List <Role> retrievedRoles = await Repository.GetAllAsync(); Assert.AreEqual(roles, retrievedRoles); }
public void InsertMany(IList <TModel> models) { using (var transaction = _database.Database.BeginTransaction()) { _database.AddRange(models); transaction.Commit(); } }
public async Task GetAllWithUsersAsyncTest_NoProjects([UserDataSource(100)] List <User> users) { DbContext.AddRange(users); await DbContext.SaveChangesAsync(); // Test List <Project> retrieved = await Repository.GetAllWithUsersAsync(); Assert.AreEqual(0, retrieved.Count); }
public void Init() { List <StudentDAO> studentDAOs = new List <StudentDAO>(); StudentDAO hocsinh1 = new StudentDAO { Id = CreateGuid("Hùng Vi Mạnh"), Name = "Hùng Vi Mạnh", Dob = new DateTime(1999, 06, 05), Gender = true, Identify = "091955291", Email = "*****@*****.**", Status = 0 }; studentDAOs.Add(hocsinh1); StudentDAO hocsinh2 = new StudentDAO { Id = CreateGuid("Vi Mạnh Hùng"), Name = "Vi Mạnh Hùng", Dob = new DateTime(1999, 06, 05), Gender = true, Identify = "091955291", Email = "*****@*****.**", Status = 0 }; studentDAOs.Add(hocsinh2); StudentDAO hocsinh3 = new StudentDAO { Id = CreateGuid("Khang ĐM"), Name = "Khang ĐM", Dob = new DateTime(1999, 08, 08), Gender = true, Identify = "456456456", Email = "*****@*****.**", Status = 0 }; studentDAOs.Add(hocsinh3); StudentDAO hocsinh4 = new StudentDAO { Id = CreateGuid("Linh ML"), Name = "Linh ML", Dob = new DateTime(1999, 08, 09), Gender = false, Identify = "45634536", Email = "*****@*****.**", Status = 0 }; studentDAOs.Add(hocsinh4); DbContext.AddRange(studentDAOs); }
private void SeedDatabase() { DbContext.ClearPlayersDatabase(); Players = _fixture .Build <Player>() .Without(x => x.Id) .CreateMany(10); DbContext.AddRange(Players); DbContext.SaveChanges(); }
static internal protected void addSet <T>(IEnumerable <T> set, DbContext db) where T : PeriodData { try { db?.AddRange(set); db?.SaveChanges(); } catch (Exception e) { Log.Exception(e); rollBack(db); addItems(set, db); } }
private static Genre[] CreateTestGenres(int numberOfGenres, int numberOfAlbums, DbContext dbContext) { var albums = Enumerable.Range(1, numberOfAlbums * numberOfGenres).Select(n => new Album() { AlbumId = n, }).ToList(); var generes = Enumerable.Range(1, numberOfGenres).Select(n => new Genre() { Albums = albums.Where(i => i.AlbumId % numberOfGenres == n - 1).ToList(), GenreId = n, Name = "Genre " + n, }); dbContext.AddRange(albums); dbContext.AddRange(generes); dbContext.SaveChanges(); return generes.ToArray(); }