-
-
Save christiannagel/add698a7bf60c25a86b258abce1eeb3f to your computer and use it in GitHub Desktop.
Seeding data with EF Core 2.1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class BooksContext : DbContext | |
{ | |
private const string ConnectionString = @"server=(localdb)\MSSQLLocalDb;database=Books1;trusted_connection=true"; | |
public DbSet<Book> Books { get; set; } | |
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) | |
{ | |
base.OnConfiguring(optionsBuilder); | |
optionsBuilder.UseSqlServer(ConnectionString) | |
.ConfigureWarnings(warnings => warnings.Throw(RelationalEventId.QueryClientEvaluationWarning)); | |
} | |
protected override void OnModelCreating(ModelBuilder modelBuilder) | |
{ | |
base.OnModelCreating(modelBuilder); | |
modelBuilder.Entity<Book>().Property(b => b.Title) | |
.IsRequired() | |
.HasMaxLength(50); | |
modelBuilder.Entity<Book>().Property(b => b.Publisher) | |
.IsRequired(false) | |
.HasMaxLength(30); | |
modelBuilder.Entity<Book>().HasData( | |
new Book { BookId = 1, Title = "Professional C# 6", Publisher = "Wrox Press"}, | |
new Book { BookId = 2, Title = "Professional C# 7", Publisher = "Wrox Press"}, | |
new Book { BookId = 3, Title = "Professional C# 8", Publisher = "Wrox Press"} | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment