This is the default behavior of Code First in the standard flow of creating a model based on DbContext. The following code demonstrates by showing that a store model convention is applied twice:
```
using System;
using System.Data.Entity;
using System.Data.Entity.Core.Metadata.Edm;
using System.Data.Entity.ModelConfiguration.Conventions;
namespace PerfectRehearsal
{
class Program
{
static void Main(string[] args)
{
using(var db = new TownContext())
{
db.Database.Delete();
db.Database.Initialize(force: false);
Console.WriteLine(
"Number of times convention was dispatched {0}:",
StoreModelDispatchCounter.Count);
}
}
}
public class Person
{
public int Id { get; set; }
public string Name { get; set; }
}
public class TownContext : DbContext
{
public DbSet<Person> People { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Add(new StoreModelDispatchCounter());
}
}
public class StoreModelDispatchCounter: IStoreModelConvention<EdmModel>
{
public static int Count = 0;
public void Apply(EdmModel item, System.Data.Entity.Infrastructure.DbModel model)
{
Count++;
}
}
}
```
```
using System;
using System.Data.Entity;
using System.Data.Entity.Core.Metadata.Edm;
using System.Data.Entity.ModelConfiguration.Conventions;
namespace PerfectRehearsal
{
class Program
{
static void Main(string[] args)
{
using(var db = new TownContext())
{
db.Database.Delete();
db.Database.Initialize(force: false);
Console.WriteLine(
"Number of times convention was dispatched {0}:",
StoreModelDispatchCounter.Count);
}
}
}
public class Person
{
public int Id { get; set; }
public string Name { get; set; }
}
public class TownContext : DbContext
{
public DbSet<Person> People { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Add(new StoreModelDispatchCounter());
}
}
public class StoreModelDispatchCounter: IStoreModelConvention<EdmModel>
{
public static int Count = 0;
public void Apply(EdmModel item, System.Data.Entity.Infrastructure.DbModel model)
{
Count++;
}
}
}
```