Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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