I have created simple test console application. In it I created separate namespace called CodeFirst where I put all CodeFirst related code. I have decided to test EF6 DbConfiguration, and here's what I got.
CodeFirst code:
Namespace CodeFirst #Region"Database context"<DbConfigurationType(GetType(FLConfiguration))> Class FLContext : Inherits DbContext Property Districts As DbSet(Of District) Property Producers As DbSet(Of Producer) ProtectedOverridesSub OnModelCreating(modelBuilder As DbModelBuilder) With modelBuilder.Configurations .Add(New DistrictConfiguration) .Add(New ProducerConfiguration) EndWithEndSubEndClass#End Region#Region"Entities Configurations"Class ProducerConfiguration : Inherits EntityTypeConfiguration(Of Producer) SubNew() ToTable("Producer") HasKey(Function(p) p.Id) EndSubEndClassClass DistrictConfiguration : Inherits EntityTypeConfiguration(Of District) SubNew() ToTable("District") EndSubEndClass#End Region#Region"Database Configuartion"Class FLConfiguration : Inherits DbConfiguration SubNew() Dim connBuilder = New SqlConnectionStringBuilder With connBuilder .ApplicationName = "FLApp" .DataSource = "FLASH" .InitialCatalog = "FL" .UserID = "RemoteUser" .Password = "rmuser"EndWithMyBase.SetDefaultConnectionFactory(New SqlConnectionFactory(connBuilder.ToString())) EndSubEndClass#End Region#Region"Entities"Class District OverridableProperty Id AsByteOverridableProperty Name AsStringOverridableProperty SpId AsStringEndClassClass Producer OverridableProperty Id AsByteOverridableProperty PId As PEnum EndClass#End RegionEndNamespace
When subclassing DbConfiguration, I used the this guidance. Then I create DbContext and do some manipulations:
Module TestVB Private db As FLContext Sub Main() db = New FLContext() db.Database.Initialize(True) Console.WriteLine("Enumerating all districts:") db.Districts.ForEach(Sub(d) Console.WriteLine("Id: {0}; Name: {1}; SpId: {2}", d.Id, d.Name, d.SpId)) Console.WriteLine() db.Database.ExecuteSqlCommand("delete from dbo.Producer;", {}) With db.Producers .Add(New Producer With {.Id = 1, .PId = PEnum.T1}) .Add(New Producer With {.Id = 2, .PId = PEnum.T2}) EndWith db.SaveChanges() EndSubEndModule
However, error is thrown :
The model backing the 'FLContext' context has changed since the database was created. Consider using Code First Migrations to update the database (http://go.microsoft.com/fwlink/?LinkId=238269).
Then I decided not to use DbConfiguration and passed connection string to DbContext constructor:
Class FLContext : Inherits DbContext ... SubNew(nameOrConnectionString AsString) MyBase.New(nameOrConnectionString) EndSub ... EndClass
And created DbContext:
Module TestVB Private db As FLContext Sub Main() Dim connBuilder = New SqlConnectionStringBuilder With connBuilder .ApplicationName = "FLApp" .DataSource = "FLASH" .InitialCatalog = "FL" .UserID = "RemoteUser" .Password = "rmuser"EndWith db = New FLContext(connBuilder.ToString()) ..... EndSubEndModule
This code works fine. What the problem can be with DbConfiguration?
P.S. I have tested more, and more weird things I have discovered when using DbConfiguration.
I have deleted database and executed SetDatabaseInitializer(New CreateDatabaseIfNotExists(Of FLContext)). It was fully ignored. Database was not created and that same error had been thrown again!