private BloggingContextFactory CreateDbFactory(string dbName) { var dbFactory = new BloggingContextFactory(dbName); dbFactories.Add(dbFactory); return(dbFactory); }
public EntityFrameworkQueryIntegrationTest() { _dbFactory = new BloggingContextFactory("EntitiFrameworkQueryTest" + Guid.NewGuid()); _dbFactory.OnDbInit += (sender, context) => { context.Blog.AddRange(_defaultData); context.SaveChanges(); }; }
static void Main(string[] args) { var db = new BloggingContextFactory().CreateDbContext(new string[0]); Console.WriteLine("Deleting database..."); db.Database.EnsureDeleted(); //doesn't run the migration to build the UDF //Console.WriteLine("Creating database"); //db.Database.EnsureCreated(); //creates the database and runs all migrations Console.WriteLine("Creating database and running migrations"); db.Database.Migrate(); }
static void Main(string[] args) { var blog = new Blog { Url = "my link" }; var posts = new List <Post> { new Post { Content = "content 1", Title = "Title1" }, new Post { Content = "content 2", Title = "Title2" }, new Post { Content = "content 3", Title = "Title3" }, new Post { Content = "content 4", Title = "Title4" }, new Post { Content = "content 5", Title = "Title5" }, }; blog.Posts.AddRange(posts); var db = new BloggingContextFactory().CreateDbContext(new string[0]); Console.WriteLine("Deleting database..."); db.Database.EnsureDeleted(); //doesn't run the migration to build the UDF //Console.WriteLine("Creating database"); //db.Database.EnsureCreated(); //creates the database and runs all migrations Console.WriteLine("Creating database and running migrations"); db.Database.Migrate(); db.Blogs.Add(blog); db.SaveChanges(); var foo = "foo"; }
static void Main(string[] args) { Console.WriteLine("Welcome to EF Core Learning session"); //case 1 DataAccess.DataContext objContext = new BloggingContextFactory().CreateDbContext(null); // objContext.Database.Migrate(); // objContext.Users.Add(new DataAccess.Entities.User { Firstname = "jkd", Password = "******" }); // objContext.Roles.Add(new DataAccess.Entities.Role { Name = "SalesHead" }); objContext.UserRoles.Add(new DataAccess.Entities.UserRole { UserID = 1, RoleID = 1 }); objContext.SaveChanges(); var rls = objContext.Roles.AsNoTracking(); Console.ReadLine(); }
public void ReadFromOutsideOfUnitWOrkButUpdateInUnitOfWOrkCreatedLater() { using (var dbFactory = new BloggingContextFactory("ReadFromOutsideOfUnitWOrkButUpdateInUnitOfWOrkCreatedLater")) { var uowFactory = new EntityFrameworkUnitOfWorkFactory <BloggingContext>(dbFactory, new UnitOfWorkRegistry()); var repository = new BlogRepository(uowFactory, dbFactory); string defaultBlogName = "Default name"; string newBlogName = "UpdatedBlogName"; //Create record using (var uow = uowFactory.Create()) { var context = uow.Context; Assert.AreEqual(0, context.Blog.Count()); context.Blog.Add(new Blog(defaultBlogName)); uow.Commit(); Assert.AreEqual(1, context.Blog.Count()); } //Read outside from UoW var blog = repository.GetByName(defaultBlogName); Assert.IsNotNull(blog); //Update by another UoW using (var uow = uowFactory.Create()) { blog.Name = newBlogName; repository.Update(blog); uow.Commit(); } //Read from outside UoW and get updated value var updatedBlog = repository.GetById(blog.Id); Assert.IsNotNull(updatedBlog); Assert.AreEqual(newBlogName, updatedBlog.Name); } }
public void InsertEntityWithKeyToBeNotEmptyInteger() { using (var dbFactory = new BloggingContextFactory("ReadFromOutsideOfUnitWOrkButUpdateInUnitOfWOrkCreatedLater")) { var uowFactory = new EntityFrameworkUnitOfWorkFactory <BloggingContext>(dbFactory, new UnitOfWorkRegistry()); var repository = new BlogRepository(uowFactory, dbFactory); var blog = new Blog("Default name") { Id = 100 }; //Create record using (var uow = uowFactory.Create()) { var context = uow.Context; Assert.AreEqual(0, context.Blog.Count()); repository.Insert(blog); uow.Commit(); Assert.AreEqual(1, context.Blog.Count()); Assert.AreEqual(1, context.Blog.First().Id); } } }
public void HandlerEvent(object sender, EventArgs e) { // Or make 'Create' method static using (var context = new BloggingContextFactory().Create()) {