DB Initialization (Database Initialization) [EF Code-First series], ef updates the model from the database
In the previous example, we have seen an example of Code-First automatically creating a database for us.
What we will learn here is how Code-First decides the Database Name and service during initialization ???
The figure below explains all this !!!
The figure explains that the database initialization process is based on the parameters passed in the constructor in the context class.
In the preceding figure, you can enter the following parameters in the base constructor of the context class:
- No Parameter)
- Database Name)
- Connection String Name)
No Parameter
If you do not specify any parameters in the base constructor, Code-First will use your local server name, that is, {Namespace }. {Context class name }. for example, in the following code: code-First will create a database named EF1.DbContextClass.
using System;using System.Collections.Generic;using System.Data.Entity;using System.Linq;using System.Text;using System.Threading.Tasks;namespace EF1{ public class DbContextClass:DbContext { public DbContextClass() : base() { } public DbSet<Student> Studnets { get; set; } public DbSet<Standard> Standards { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder) { Database.SetInitializer(new DropCreateDatabaseIfModelChanges<DbContextClass>()); base.OnModelCreating(modelBuilder); } }}
Database Name
You can also specify the database name in the base constructor, for example:
using System;using System.Collections.Generic;using System.Data.Entity;using System.Linq;using System.Text;using System.Threading.Tasks;namespace EF1{ public class DbContextClass:DbContext { public DbContextClass() : base("MYDBHAHA") { } public DbSet<Student> Studnets { get; set; } public DbSet<Standard> Standards { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder) { Database.SetInitializer(new DropCreateDatabaseIfModelChanges<DbContextClass>()); base.OnModelCreating(modelBuilder); } }}
The generated database is:
Connection string
using System;using System.Collections.Generic;using System.Data.Entity;using System.Linq;using System.Text;using System.Threading.Tasks;namespace EF1{ public class DbContextClass:DbContext { public DbContextClass() : base("ConnectionString") { } public DbSet<Student> Studnets { get; set; } public DbSet<Standard> Standards { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder) { Database.SetInitializer(new DropCreateDatabaseIfModelChanges<DbContextClass>()); base.OnModelCreating(modelBuilder); } }}
Or write as follows:
using System;using System.Collections.Generic;using System.Data.Entity;using System.Linq;using System.Text;using System.Threading.Tasks;namespace EF1{ public class DbContextClass:DbContext { public DbContextClass() : base("name=ConnectionString") { } public DbSet<Student> Studnets { get; set; } public DbSet<Standard> Standards { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder) { Database.SetInitializer(new DropCreateDatabaseIfModelChanges<DbContextClass>()); base.OnModelCreating(modelBuilder); } }}
You can choose one of the above methods. After the modification, add the following nodes in the configuration file:
<connectionStrings> <add name="ConnectionString" connectionString="server=.;database=EFCodeFirstDB;uid=sa;pwd=Password_1" providerName="System.Data.SqlClient"/> </connectionStrings>
I usually add this node to this position:
<?xml version="1.0" encoding="utf-8"?><configuration> <configSections> <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --> <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false"/> </configSections> <connectionStrings> <add name="ConnectionString" connectionString="server=.;database=EFCodeFirstDB;uid=sa;pwd=Password_1" providerName="System.Data.SqlClient"/> </connectionStrings> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/> </startup> <entityFramework> <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework"> <parameters> <parameter value="v11.0"/> </parameters> </defaultConnectionFactory> <providers> <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer"/> </providers> </entityFramework></configuration>
After the modification, we run the program and generated the desired database in the database management tool:
There are three methods to initialize the database.
The following is a directory to facilitate Viewing:
- What is Code First?
- Simple Code First example
- Code-First Convention
- DB Initialization (Database Initialization)
- Inheritance Strategy (Inheritance Policy)
- Configure Domain Classes (configuration Domain class)
- DataAnnotations (Data annotation)
- Fluent API
- Configure One-to-One (Configure a One-to-One relationship)
- Configure One-to-multiple (Configure One-to-multiple relationships)
- Configure allow-to-Configure (Configure Many-to-Many relationships)
- Move deployments (data migration)
- DB Initialization Strategy (Database Initialization Policy)
- ... To be continued .....