The base class constructor for the context class has the following arguments
1. No parameters
If you do not add a parameter to the base class constructor, it creates the database in the local SQLEXPRESS server named {Namespace}. {Context class name}
namespace schooldatalayer{ publicclass context:dbcontext { public Base() { } }}
The database name created above is:schooldatalayer.context
2. The parameter is: database name
If the base class constructor specifies the name of the database, the local SQLEXPRESS database server is created with the name given to you.
namespace schooldatalayer{ publicclass context:dbcontext { public base("myschooldb") { }} }
The database name above is:myschooldb
3. The parameter is: connection string name
You can create a connection string in app. Config or Web. config, and code first creates a database based on the connection string if the parameters used by the base class constructor have been name=
namespace schooldatalayer{ public class Context:dbcontext { public schooldbcontext (): base (" name= schooldbconnectionstring " ) {}} }
<?xml version="1.0" encoding="utf-8" ?>< configuration> <connectionStrings> <add name="schooldbconnectionstring " connectionString="Data source=.;i Nitial catalog=schooldb-byconnectionstring;integrated security=true" providerName ="System.Data.SqlClient"/> </connectionstrings></ Configuration>
EF Code-first Learning Journey Database initialization