By Chris Scott
Presentation code on github
Created using reveal.js
DbContext
public class ServicesContext : DbContext
PM> Enable-Migrations
Configuration.cs
public Configuration()
{
AutomaticMigrationsEnabled = true;
}
public Contact ServiceAgentContact { get; set; }
Bad:
public int ServiceAgentContactId { get; set; }
DbContext
to be picked up on migration
public DbSet<Contact> Contacts { get; set; }
PM> Update-Database
...
public int? OptionalValue { get; set; }
public DateTime? OptionalDate { get; set; }
...
Use Attributes to enhance relationships
...
//Make Partner required for this model.
[Required]
public Partner Partner { get; set; }
public string CompanyName { get; set; }
...
Modify relationships during migration run using Fluent API
public class ServicesContext : DbContext
{
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Partner>()
.HasOptional(p => p.Logo)
.WithOptionalDependent()
.Map(k => k.MapKey("Logo_Id"));
...
}
}
Will set Partner.Logo
to be 1:1, but still optional