using System; using Dinah.EntityFrameworkCore; using Microsoft.EntityFrameworkCore; namespace _scratch_pad { ////// to use this as a console, open properties and change from class library => console //// DON'T FORGET TO REVERT IT //public class Program //{ // public static void Main(string[] args) // { // var user = new Student() { Name = "Dinah Cheshire" }; // var udi = new UserDef { UserDefId = 1, TagsRaw = "my,tags" }; // using (var context = new MyTestContextDesignTimeDbContextFactory().Create()) // { // context.Add(user); // //context.Add(udi); // context.Update(udi); // context.SaveChanges(); // } // Console.WriteLine($"Student was saved in the database with id: {user.Id}"); // } //} public class MyTestContextDesignTimeDbContextFactory : DesignTimeDbContextFactoryBase { protected override MyTestContext CreateNewInstance(DbContextOptions options) => new MyTestContext(options); protected override void UseDatabaseEngine(DbContextOptionsBuilder optionsBuilder, string connectionString) => optionsBuilder.UseSqlite(connectionString); } public class MyTestContext : DbContext { // see DesignTimeDbContextFactoryBase for info about ctors and connection strings/OnConfiguring() public MyTestContext(DbContextOptions options) : base(options) { } #region classes for OnModelCreating() seed example class Blog { public int BlogId { get; set; } public string Url { get; set; } public System.Collections.Generic.ICollection Posts { get; set; } } class Post { public int PostId { get; set; } public string Content { get; set; } public string Title { get; set; } public int BlogId { get; set; } public Blog Blog { get; set; } public Name AuthorName { get; set; } } class Name { public string First { get; set; } public string Last { get; set; } } #endregion protected override void OnModelCreating(ModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); // config modelBuilder.Entity(entity => entity.Property(e => e.Url).IsRequired()); modelBuilder.Entity().OwnsOne(p => p.OrderDetails, cb => { cb.OwnsOne(c => c.BillingAddress); cb.OwnsOne(c => c.ShippingAddress); }); modelBuilder.Entity(entity => entity .HasOne(d => d.Blog) .WithMany(p => p.Posts) .HasForeignKey("BlogId")); // BlogSeed modelBuilder.Entity().HasData(new Blog { BlogId = 1, Url = "http://sample.com" }); // PostSeed modelBuilder.Entity().HasData(new Post() { BlogId = 1, PostId = 1, Title = "First post", Content = "Test 1" }); // AnonymousPostSeed modelBuilder.Entity().HasData(new { BlogId = 1, PostId = 2, Title = "Second post", Content = "Test 2" }); // OwnedTypeSeed modelBuilder.Entity().OwnsOne(p => p.AuthorName).HasData( new { PostId = 1, First = "Andriy", Last = "Svyryd" }, new { PostId = 2, First = "Diego", Last = "Vega" }); } public DbSet Students { get; set; } public DbSet UserDefs { get; set; } public DbSet Orders { get; set; } } public class Student { public int Id { get; set; } public string Name { get; set; } } public class UserDef { public int UserDefId { get; set; } public string TagsRaw { get; set; } } public class Order { public int Id { get; set; } public OrderDetails OrderDetails { get; set; } } public class OrderDetails { public StreetAddress BillingAddress { get; set; } public StreetAddress ShippingAddress { get; set; } } public class StreetAddress { public string Street { get; set; } public string City { get; set; } } }