Use CodeFirst for dynamic database creation
In the project ITOO3.0, a technical point is to dynamically create databases and tables. After a simple explanation, we will dynamically create a complete set of ITOO backend databases for a cloud platform system registered with a university. Well, the background of the project is clear. The following is the specific implementation process.
I. Business Analysis
The following describes how to register a target at ordinary times:
Figure 1 current target login process
For more information, see:
Step 1: registration page. Enter your mobile phone number or email address, and click OK to go to the basic information page.
Step 2: basic information. Complete basic user information and click Register to complete registration. In this step, we want to create databases and tables.
Step 3: complete the information. After the user completes the registration, a prompt box is displayed to inform the user of the enterprise number, account, and password allocated.
Ii. Code Design
First, we use the. net mvc + EF architecture to create three interfaces.
Figure 2 create three interfaces
Second, CodeFirst code is generated based on the database. If we want to write CodeFirst code on our own, it will be very troublesome and the connection between classes is not easy to grasp. Microsoft provides a good plug-in for generating CodeFirst code from the database: Entity Framework Power Tools Bata 4, open VS ==> tool ==> extension and update, search online.
Figure 3 Entity Framework Power Tools Bata 4 plugin installation
After installation, create a new class library or right-click on the original class library. An EntityFramework option is added (VS is not restarted), and select the Reverse Engineer Code First option, the familiar pop-up box for database connection will pop up. Clicking OK will automatically create CodeFirst code based on the database, which is very convenient.
Figure 4-1 generate the CodeFirst code
Figure 4-2 Database Connection Properties
Figure 4-3 generate CodeFirst code from the database
Finally, the database is automatically created in the code, that is, we click the register button to dynamically generate a complete set of database operations. Here we only show you the key code for dynamic database creation.
There are two things to do in this step:
1. Change the EF context constructor to the database connection character bed, because it is read from the configuration file by default.
Namespace DbFristModel. Models {public partial class ModelTestContext: DbContext {static ModelTestContext () {Database. SetInitializer
(Null) ;}// modify the context default constructor public ModelTestContext (string connectionString): base (connectionString) {// determine whether there is a database in the connection string in the context. If there is a connection, this is automatically created if it does not exist. database. createIfNotExists ();} public DbSet
Classes {get; set;} public DbSet
Students {get; set;} protected override void OnModelCreating (DbModelBuilder modelBuilder) {modelBuilder. events. add (new ClassMap (); modelBuilder. events. add (new StudentMap ());}}}
2. dynamically create a database when you click the register button. The code in Controller:
// Connection string connectionString = "Data Source = .; user = sa; Password = 123456; Initial Catalog = "+ db. dbName + "; Integrated Security = True"; ModelTestContext newDB = new ModelTestContext (connectionString );
So far, the key code for dynamic database creation has been implemented. It should be noted that the database name is automatically generated by the database ID of the permission system + the school code, so we can generate different databases every time we register.
Effect display:
Figure 5 dynamically created database