Skip to content

Instantly share code, notes, and snippets.

@scottsauber
Created September 11, 2017 20:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save scottsauber/e832d237f3752d27ee10c03b809778b3 to your computer and use it in GitHub Desktop.
Save scottsauber/e832d237f3752d27ee10c03b809778b3 to your computer and use it in GitHub Desktop.
EF Core 1.x Customizations
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