Created
September 11, 2017 20:46
-
-
Save scottsauber/e832d237f3752d27ee10c03b809778b3 to your computer and use it in GitHub Desktop.
EF Core 1.x Customizations
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 ApplicationDbContext : IdentityDbContext<ApplicationUser> | |
{ | |
public DbSet<OrderItem> Orders { get; set; } | |
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options) { } | |
protected override void OnModelCreating(ModelBuilder builder) | |
{ | |
base.OnModelCreating(builder); | |
// Customizations must go after base.OnModelCreating(builder) | |
builder.Entity<ApplicationUser>(config => | |
{ | |
// Override nvarchar(max) with nvarchar(15) | |
config.Property(u => u.PhoneNumber).HasMaxLength(15); | |
// Make the default table name of AspNetUsers to Users | |
config.ToTable("Users"); | |
}); | |
builder.Entity<OrderItem>(config => | |
{ | |
// Make the default column type datetime over datetime2 (for some reason). | |
config.Property(o => o.DateTimeOrdered).HasColumnType("datetime"); | |
// Make the default value 1 for the Quantity property | |
config.Property(o => o.Quantity).HasDefaultValue(1); | |
// Make the Primary Key associated with the property UniqueKey | |
config.HasKey(o => o.UniqueKey); | |
}); | |
// Imagine a ton more customizations | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment