Skip to content

Commit

Permalink
Quicker, easier, more seductive... (Change DbContext.OnModelCreating …
Browse files Browse the repository at this point in the history
…to get a ConventionModelBuilder)

The relationship fluent API patterns we have introduced cannot work without some conventions. However, we feel that there is still value in having a ModelBuilder API that is very close to the metadata model and never uses any conventions. Therefore, the plan is to move the new relationship API into the ConventionModelBuilder. Since we want this API to be the one available in OnModelCreating this means that the parameter in OnModelCreating should now be ConventionModelBuilder.

Also did the rename of the parameter per issue #441.
  • Loading branch information
ajcvickers committed Aug 15, 2014
1 parent 1866fc5 commit 415a8e8
Show file tree
Hide file tree
Showing 16 changed files with 104 additions and 104 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Microsoft.Data.Entity.AzureTableStorage.Metadata
{
public class AtsModelBuilderFactory : IModelBuilderFactory
{
public virtual ModelBuilder CreateConventionBuilder([NotNull] Model model)
public virtual ConventionModelBuilder CreateConventionBuilder([NotNull] Model model)
{
Check.NotNull(model, "model");

Expand Down
2 changes: 1 addition & 1 deletion src/EntityFramework/DbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ protected internal virtual void OnConfiguring([NotNull] DbContextOptions options
{
}

protected internal virtual void OnModelCreating([NotNull] ModelBuilder builder)
protected internal virtual void OnModelCreating([NotNull] ConventionModelBuilder modelBuilder)
{
}

Expand Down
2 changes: 1 addition & 1 deletion src/EntityFramework/Metadata/IModelBuilderFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ namespace Microsoft.Data.Entity.Metadata
{
public interface IModelBuilderFactory
{
ModelBuilder CreateConventionBuilder([NotNull] Model model);
ConventionModelBuilder CreateConventionBuilder([NotNull] Model model);
}
}
2 changes: 1 addition & 1 deletion src/EntityFramework/Metadata/ModelBuilderFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace Microsoft.Data.Entity.Metadata
{
public class ModelBuilderFactory : IModelBuilderFactory
{
public virtual ModelBuilder CreateConventionBuilder([NotNull] Model model)
public virtual ConventionModelBuilder CreateConventionBuilder([NotNull] Model model)
{
Check.NotNull(model, "model");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,9 @@ protected override void OnConfiguring(DbContextOptions options)
options.UseInMemoryStore(persist: false);
}

protected override void OnModelCreating(ModelBuilder builder)
protected override void OnModelCreating(ConventionModelBuilder modelBuilder)
{
builder.Entity<Blog>().Key(b => b.Url);
modelBuilder.Entity<Blog>().Key(b => b.Url);
}
}
}
Expand Down
10 changes: 5 additions & 5 deletions test/EntityFramework.FunctionalTests/FixupTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -239,13 +239,13 @@ private class SpecialOffer

private class FixupContext : DbContext
{
protected override void OnModelCreating(ModelBuilder builder)
protected override void OnModelCreating(ConventionModelBuilder modelBuilder)
{
var model = builder.Model;
var model = modelBuilder.Model;

builder.Entity<Product>().OneToMany(e => e.SpecialOffers, e => e.Product);
builder.Entity<Category>().OneToMany(e => e.Products, e => e.Category);
builder.Entity<SpecialOffer>();
modelBuilder.Entity<Product>().OneToMany(e => e.SpecialOffers, e => e.Product);
modelBuilder.Entity<Category>().OneToMany(e => e.Products, e => e.Category);
modelBuilder.Entity<SpecialOffer>();

model.GetEntityType(typeof(Category)).GetProperty("Id").ValueGenerationOnAdd = ValueGenerationOnAdd.None;
model.GetEntityType(typeof(Product)).GetProperty("Id").ValueGenerationOnAdd = ValueGenerationOnAdd.None;
Expand Down
78 changes: 39 additions & 39 deletions test/EntityFramework.FunctionalTests/TestModels/MonsterContext`.cs
Original file line number Diff line number Diff line change
Expand Up @@ -211,22 +211,22 @@ public override IQueryable<ILicense> Licenses
get { return Set<TLicense>(); }
}

protected override void OnModelCreating(ModelBuilder builder)
protected override void OnModelCreating(ConventionModelBuilder modelBuilder)
{
builder.Entity<TBarcodeDetail>().Key(e => e.Code);
builder.Entity<TResolution>();
builder.Entity<TSuspiciousActivity>();
builder.Entity<TLastLogin>().Key(e => e.Username);
builder.Entity<TMessage>().Key(e => new { e.MessageId, e.FromUsername });
builder.Entity<TOrderNote>().Key(e => e.NoteId);
builder.Entity<TProductDetail>().Key(e => e.ProductId);
builder.Entity<TProductWebFeature>().Key(e => e.ProductId);
builder.Entity<TSupplierLogo>().Key(e => e.SupplierId);
builder.Entity<TCustomerInfo>();
builder.Entity<TComputerDetail>();
builder.Entity<TLicense>().Key(e => e.Name);

builder.Entity<TAnOrder>(b =>
modelBuilder.Entity<TBarcodeDetail>().Key(e => e.Code);
modelBuilder.Entity<TResolution>();
modelBuilder.Entity<TSuspiciousActivity>();
modelBuilder.Entity<TLastLogin>().Key(e => e.Username);
modelBuilder.Entity<TMessage>().Key(e => new { e.MessageId, e.FromUsername });
modelBuilder.Entity<TOrderNote>().Key(e => e.NoteId);
modelBuilder.Entity<TProductDetail>().Key(e => e.ProductId);
modelBuilder.Entity<TProductWebFeature>().Key(e => e.ProductId);
modelBuilder.Entity<TSupplierLogo>().Key(e => e.SupplierId);
modelBuilder.Entity<TCustomerInfo>();
modelBuilder.Entity<TComputerDetail>();
modelBuilder.Entity<TLicense>().Key(e => e.Name);

modelBuilder.Entity<TAnOrder>(b =>
{
b.OneToMany(e => (IEnumerable<TOrderLine>)e.OrderLines, e => (TAnOrder)e.Order);
Expand All @@ -242,7 +242,7 @@ protected override void OnModelCreating(ModelBuilder builder)
}
});

builder.Entity<TOrderQualityCheck>(b =>
modelBuilder.Entity<TOrderQualityCheck>(b =>
{
b.Key(e => e.OrderId);
Expand All @@ -260,7 +260,7 @@ protected override void OnModelCreating(ModelBuilder builder)
}
});

builder.Entity<TProduct>(b =>
modelBuilder.Entity<TProduct>(b =>
{
b.OneToMany(e => (IEnumerable<TProductReview>)e.Reviews, e => (TProduct)e.Product);
b.OneToMany(e => (IEnumerable<TBarcode>)e.Barcodes, e => (TProduct)e.Product);
Expand All @@ -269,15 +269,15 @@ protected override void OnModelCreating(ModelBuilder builder)
b.OneToOne<TProductWebFeature>();
});

builder.Entity<TOrderLine>(b =>
modelBuilder.Entity<TOrderLine>(b =>
{
b.Key(e => new { e.OrderId, e.ProductId });
b.ManyToOne(e => (TProduct)e.Product);
});

builder.Entity<TSupplier>().OneToOne(e => (TSupplierLogo)e.Logo);
modelBuilder.Entity<TSupplier>().OneToOne(e => (TSupplierLogo)e.Logo);

builder.Entity<TCustomer>(b =>
modelBuilder.Entity<TCustomer>(b =>
{
b.OneToMany(e => (IEnumerable<TAnOrder>)e.Orders, e => (TCustomer)e.Customer);
b.OneToMany(e => (IEnumerable<TLogin>)e.Logins, e => (TCustomer)e.Customer);
Expand All @@ -287,7 +287,7 @@ protected override void OnModelCreating(ModelBuilder builder)
.ForeignKey<TCustomer>(e => e.HusbandId);
});

builder.Entity<TComplaint>(b =>
modelBuilder.Entity<TComplaint>(b =>
{
b.ManyToOne(e => (TCustomer)e.Customer)
.ForeignKey(e => e.CustomerId);
Expand All @@ -304,7 +304,7 @@ protected override void OnModelCreating(ModelBuilder builder)
}
});

builder.Entity<TProductPhoto>(b =>
modelBuilder.Entity<TProductPhoto>(b =>
{
b.Key(e => new { e.ProductId, e.PhotoId });
Expand All @@ -313,15 +313,15 @@ protected override void OnModelCreating(ModelBuilder builder)
.ReferencedKey(e => new { e.ProductId, e.PhotoId });
});

builder.Entity<TProductReview>(b =>
modelBuilder.Entity<TProductReview>(b =>
{
b.Key(e => new { e.ProductId, e.ReviewId });
b.OneToMany(e => (IEnumerable<TProductWebFeature>)e.Features, e => (TProductReview)e.Review)
.ForeignKey(e => new { e.ProductId, e.ReviewId });
});

builder.Entity<TLogin>(b =>
modelBuilder.Entity<TLogin>(b =>
{
b.Key(e => e.Username);
Expand All @@ -340,7 +340,7 @@ protected override void OnModelCreating(ModelBuilder builder)
b.OneToOne(e => (TLastLogin)e.LastLogin, e => (TLogin)e.Login);
});

builder.Entity<TPasswordReset>(b =>
modelBuilder.Entity<TPasswordReset>(b =>
{
b.Key(e => new { e.ResetNo, e.Username });
Expand All @@ -358,11 +358,11 @@ protected override void OnModelCreating(ModelBuilder builder)
}
});

builder.Entity<TPageView>()
modelBuilder.Entity<TPageView>()
.ManyToOne(e => (TLogin)e.Login)
.ForeignKey(e => e.Username);

builder.Entity<TBarcode>(b =>
modelBuilder.Entity<TBarcode>(b =>
{
b.Key(e => e.Code);
Expand All @@ -372,21 +372,21 @@ protected override void OnModelCreating(ModelBuilder builder)
b.OneToOne(e => (TBarcodeDetail)e.Detail);
});

builder.Entity<TIncorrectScan>()
modelBuilder.Entity<TIncorrectScan>()
.ManyToOne(e => (TBarcode)e.ActualBarcode)
.ForeignKey(e => e.ActualCode);

builder.Entity<TSupplierInfo>().ManyToOne(e => (TSupplier)e.Supplier);
modelBuilder.Entity<TSupplierInfo>().ManyToOne(e => (TSupplier)e.Supplier);

builder.Entity<TComputer>().OneToOne(e => (TComputerDetail)e.ComputerDetail, e => (TComputer)e.Computer);
modelBuilder.Entity<TComputer>().OneToOne(e => (TComputerDetail)e.ComputerDetail, e => (TComputer)e.Computer);

builder.Entity<TDriver>(b =>
modelBuilder.Entity<TDriver>(b =>
{
b.Key(e => e.Name);
b.OneToOne(e => (TLicense)e.License, e => (TDriver)e.Driver);
});

builder.Entity<TSmartCard>(b =>
modelBuilder.Entity<TSmartCard>(b =>
{
b.Key(e => e.Username);
Expand All @@ -397,22 +397,22 @@ protected override void OnModelCreating(ModelBuilder builder)
.ForeignKey<TLastLogin>(e => e.SmartcardUsername);
});

builder.Entity<TRsaToken>(b =>
modelBuilder.Entity<TRsaToken>(b =>
{
b.Key(e => e.Serial);
b.OneToOne(e => (TLogin)e.Login)
.ForeignKey<TRsaToken>(e => e.Username);
});

// TODO: Many-to-many
//builder.Entity<TSupplier>().ForeignKeys(fk => fk.ForeignKey<TProduct>(e => e.SupplierId));
//modelBuilder.Entity<TSupplier>().ForeignKeys(fk => fk.ForeignKey<TProduct>(e => e.SupplierId));

// TODO: Inheritance
//builder.Entity<TBackOrderLine>().ForeignKeys(fk => fk.ForeignKey<TSupplier>(e => e.SupplierId));
//builder.Entity<TDiscontinuedProduct>().ForeignKeys(fk => fk.ForeignKey<TProduct>(e => e.ReplacementProductId));
//builder.Entity<TProductPageView>().ForeignKeys(fk => fk.ForeignKey<TProduct>(e => e.ProductId));
//modelBuilder.Entity<TBackOrderLine>().ForeignKeys(fk => fk.ForeignKey<TSupplier>(e => e.SupplierId));
//modelBuilder.Entity<TDiscontinuedProduct>().ForeignKeys(fk => fk.ForeignKey<TProduct>(e => e.ReplacementProductId));
//modelBuilder.Entity<TProductPageView>().ForeignKeys(fk => fk.ForeignKey<TProduct>(e => e.ProductId));

var model = builder.Model;
var model = modelBuilder.Model;

// TODO: Key should get by-convention value generation even if key is not discovered by convention
var noteId = model.GetEntityType(typeof(TOrderNote)).GetProperty("NoteId");
Expand Down Expand Up @@ -447,7 +447,7 @@ protected override void OnModelCreating(ModelBuilder builder)

if (_onModelCreating != null)
{
_onModelCreating(builder);
_onModelCreating(modelBuilder);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,13 +183,13 @@ public BronieContext(IServiceProvider serviceProvider)
public DbSet<Unicorn> Unicorns { get; set; }
public DbSet<EarthPony> EarthPonies { get; set; }

protected override void OnModelCreating(ModelBuilder builder)
protected override void OnModelCreating(ConventionModelBuilder modelBuilder)
{
builder.Entity<Pegasus>().Key(e => new { e.Id1, e.Id2 });
builder.Entity<Unicorn>().Key(e => new { e.Id1, e.Id2, e.Id3 });
builder.Entity<EarthPony>().Key(e => new { e.Id1, e.Id2 });
modelBuilder.Entity<Pegasus>().Key(e => new { e.Id1, e.Id2 });
modelBuilder.Entity<Unicorn>().Key(e => new { e.Id1, e.Id2, e.Id3 });
modelBuilder.Entity<EarthPony>().Key(e => new { e.Id1, e.Id2 });

var unicornType = builder.Model.GetEntityType(typeof(Unicorn));
var unicornType = modelBuilder.Model.GetEntityType(typeof(Unicorn));

var id1 = unicornType.GetProperty("Id1");
id1.ValueGenerationOnAdd = ValueGenerationOnAdd.Client;
Expand All @@ -198,7 +198,7 @@ protected override void OnModelCreating(ModelBuilder builder)
var id3 = unicornType.GetProperty("Id3");
id3.ValueGenerationOnAdd = ValueGenerationOnAdd.Client;

var earthType = builder.Model.GetEntityType(typeof(EarthPony));
var earthType = modelBuilder.Model.GetEntityType(typeof(EarthPony));

var id = earthType.GetProperty("Id1");
id.ValueGenerationOnAdd = ValueGenerationOnAdd.Client;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,9 @@ protected override void OnConfiguring(DbContextOptions options)
options.UseInMemoryStore();
}

protected override void OnModelCreating(ModelBuilder builder)
protected override void OnModelCreating(ConventionModelBuilder modelBuilder)
{
builder.Entity<Artist>().Key(a => a.ArtistId);
modelBuilder.Entity<Artist>().Key(a => a.ArtistId);
}

public class Artist : ArtistBase<string>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,13 +199,13 @@ protected override void OnConfiguring(DbContextOptions builder)
builder.UseSqlServer(SqlServerTestDatabase.CreateConnectionString(_databaseName));
}

protected override void OnModelCreating(ModelBuilder builder)
protected override void OnModelCreating(ConventionModelBuilder modelBuilder)
{
builder.Entity<Pegasus>().Key(e => new { e.Id1, e.Id2 });
builder.Entity<Unicorn>().Key(e => new { e.Id1, e.Id2, e.Id3 });
builder.Entity<EarthPony>().Key(e => new { e.Id1, e.Id2 });
modelBuilder.Entity<Pegasus>().Key(e => new { e.Id1, e.Id2 });
modelBuilder.Entity<Unicorn>().Key(e => new { e.Id1, e.Id2, e.Id3 });
modelBuilder.Entity<EarthPony>().Key(e => new { e.Id1, e.Id2 });

var unicornType = builder.Model.GetEntityType(typeof(Unicorn));
var unicornType = modelBuilder.Model.GetEntityType(typeof(Unicorn));

var id1 = unicornType.GetProperty("Id1");
id1.ValueGenerationOnAdd = ValueGenerationOnAdd.Client;
Expand All @@ -214,7 +214,7 @@ protected override void OnModelCreating(ModelBuilder builder)
var id3 = unicornType.GetProperty("Id3");
id3.ValueGenerationOnAdd = ValueGenerationOnAdd.Client;

var earthType = builder.Model.GetEntityType(typeof(EarthPony));
var earthType = modelBuilder.Model.GetEntityType(typeof(EarthPony));

var id = earthType.GetProperty("Id1");
id.ValueGenerationOnAdd = ValueGenerationOnAdd.Client;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,9 @@ protected override void OnConfiguring(DbContextOptions options)
options.UseSqlServer(_nameOrConnectionString);
}

protected override void OnModelCreating(ModelBuilder builder)
protected override void OnModelCreating(ConventionModelBuilder modelBuilder)
{
ConfigureModel(builder);
ConfigureModel(modelBuilder);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,9 @@ protected override void OnConfiguring(DbContextOptions options)
options.UseSqlServer(_connection);
}

protected override void OnModelCreating(ModelBuilder builder)
protected override void OnModelCreating(ConventionModelBuilder modelBuilder)
{
builder.Entity<Customer>(b =>
modelBuilder.Entity<Customer>(b =>
{
b.Key(c => c.CustomerID);
b.ToTable("Customers");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,9 +208,9 @@ protected override void OnConfiguring(DbContextOptions options)
options.UseSqlServer(SqlServerTestDatabase.CreateConnectionString(_databaseName));
}

protected override void OnModelCreating(ModelBuilder builder)
protected override void OnModelCreating(ConventionModelBuilder modelBuilder)
{
builder.Entity<Pegasus>(b =>
modelBuilder.Entity<Pegasus>(b =>
{
b.Key(e => e.Identifier);
b.Property(e => e.Identifier).UseStoreSequence("PegasusSequence", 11);
Expand Down
Loading

0 comments on commit 415a8e8

Please sign in to comment.