private static void Main() { using (var db = new BloggingContext()) { } }
private static void SetupDatabase() { using (var db = new BloggingContext("diego")) { if (db.Database.EnsureCreated()) { db.Blogs.Add( new Blog { Url = "http://sample.com/blogs/fish", Posts = new List <Post> { new Post { Title = "Fish care 101", IsDeleted = false }, new Post { Title = "Caring for tropical fish", IsDeleted = false }, new Post { Title = "Types of ornamental fish", IsDeleted = true } } }); db.Blogs.Add( new Blog { Url = "http://sample.com/blogs/cats", Posts = new List <Post> { new Post { Title = "Cat care 101", IsDeleted = true }, new Post { Title = "Caring for tropical cats", IsDeleted = false }, new Post { Title = "Types of ornamental cats", IsDeleted = false } } }); db.SaveChanges(); using (var andrewDb = new BloggingContext("andrew")) { andrewDb.Blogs.Add( new Blog { Url = "http://sample.com/blogs/catfish", Posts = new List <Post> { new Post { Title = "Catfish care 101", IsDeleted = false }, new Post { Title = "History of the catfish name", IsDeleted = false } } }); andrewDb.SaveChanges(); } // db.Posts // .Where( // p => p.Title == "Caring for tropical fish" // || p.Title == "Cat care 101") // .ToList() // .ForEach(p => db.Posts.Remove(p)); db.SaveChanges(); } } }
private static void SetupDatabase() { using (var db = new BloggingContext("rowan")) { if (db.Database.EnsureCreated()) { db.Blogs.Add(new Blog { Url = "http://sample.com/blogs/fish", Posts = new List <Post> { new Post { Title = "Fish care 101" }, new Post { Title = "Caring for tropical fish" }, new Post { Title = "Types of ornamental fish" } } }); db.Blogs.Add(new Blog { Url = "http://sample.com/blogs/cats", Posts = new List <Post> { new Post { Title = "Cat care 101" }, new Post { Title = "Caring for tropical cats" }, new Post { Title = "Types of ornamental cats" } } }); db.SaveChanges(); using (var jeffDb = new BloggingContext("jeff")) { jeffDb.Blogs.Add(new Blog { Url = "http://sample.com/blogs/catfish", Posts = new List <Post> { new Post { Title = "Catfish care 101" }, new Post { Title = "History of the catfish name" } } }); jeffDb.SaveChanges(); } db.Posts .Where(p => p.Title == "Caring for tropical fish" || p.Title == "Cat care 101") .ToList() .ForEach(p => db.Posts.Remove(p)); db.SaveChanges(); } } }
public static void Main(string[] args) { using (var context = new BloggingContext()) { // Recreate database context.Database.EnsureDeleted(); context.Database.EnsureCreated(); // Add some data... context.Blogs.AddRange( new Blog { BlogId = 1, Name = "ADO.NET", Url = "http://blogs.msdn.com/adonet", Posts = new List <Post> { new Post { PostId = 1, Title = "Welcome to this blog!", Tags = new List <Tag> { new Tag { Name = "Meta" } } }, new Post { PostId = 2, Title = "Getting Started with ADO.NET", Tags = new List <Tag> { new Tag { Name = "Entity Framework Core" }, new Tag { Name = "ADO.NET" } } } } }, new Blog { BlogId = 2, Name = "ASP.NET", Url = "http://blogs.msdn.com/aspnet" }, new Blog { BlogId = 3, Name = ".NET", Url = "http://blogs.msdn.com/dotnet" }); var affected = context.SaveChanges(); Console.WriteLine($"Saved {affected} records to Cosmos DB"); Console.ReadLine(); } using (var context = new BloggingContext()) { var adonetBlog = context.Blogs.Single(b => b.Name == "ADO.NET"); Console.WriteLine($"{adonetBlog.Name} - {adonetBlog.Url}"); foreach (var post in context.Posts.Where(p => p.BlogId == adonetBlog.BlogId)) { Console.WriteLine($" - {post.Title}"); foreach (var tag in post.Tags) { Console.WriteLine($" - {tag.Name}"); } } Console.ReadLine(); } }
public BlogService(BloggingContext db) { _db = db; }