Quantcast
Channel: Entity Framework
Viewing all articles
Browse latest Browse all 10318

Edited Issue: Code First models still get built more than once in EF6 [1454]

$
0
0
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++;
}
}
}

```

Viewing all articles
Browse latest Browse all 10318

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>