Previous Period review: Entity Framework Learning Note (1)
The main thing about the Entity framework is the class that you create and inherit from DbContext:
<summary>// context equivalent to a database/// </summary> public class Musiccontext:dbcontext { //base ("LocalDB") indicates that the connection string named "Lcoaldb" in the config file is to be used public musiccontext (): Base ("LocalDB") { } //dbset equivalent to data table public dbset<album> Albums {get; set;} }
Of course, our data table definition classes are also very simple, as follows:
<summary>////album/// </summary> public class Album {public int albumID {get; set;} Public string Title {get; set;} Public decimal price {get; set;} }
The corresponding actual data table is defined as:
It is easy to see that both int and decimal are normal, and string is translated to nvarchar (MAX)
Many times we do not need to have a plain text field defined to nvarchar (MAX), most of which is an actual value, such as nvarchar (50).
To implement this in the Entity Framework, you have to add the attribute to the field (Attribute):
<summary> ////album (table indicates that you want to manually set the name of the tables for the Created data table)/// </summary> [Table (Name: "MyTable")] public class Album {public int albumID {get; set;} <summary> //stringlength can set maximum length ///</summary> [stringlength] public String Title {get; set;} [Required] Public decimal price {get; set;} }
Then if we run the program, we will get an error:
It means: The table definition in your program is inconsistent with the table definition in the database (definitely inconsistent, we have all changed the definition)
At this point, we need to do a set of actions to synchronize the table definitions in the program to the database:
- Open the package Manager Console
- Enter enable-migrations to turn on database migration
(if there are multiple DbContext objects in the project, this will be an error, the workaround is to specify the full name of the DbContext class including the namespace: Enable-migrations–contexttypename full name) (if you want to have the entity The framework helps you to automatically implement the changes to the target database, you can add another option –enableautomaticmigrations, so this example is better written as: enable-migrations– ContextTypeName normaltest.musiccontext–enableautomaticmigrations)
- The content in the diagram indicates that the database migration component has been created, and the Entity framework has added some files to the project that you can see in the Solution Explorer
- At this point, we can continue to enter the instruction Update-database to update the target database (here you can use the UPDATE-DATABASE–SCIRPT option to show which changes are running). But then ... and an error.
This means that we do not have the option to turn on –enableautomaticmigrations. Here we have two methods to open, one is the direct input enable-migrations-enableautomaticmigrations-force, the other is to open the entity The framework for the Configuration.cs file we just created
Change the value of automaticmigrationsenabled = False to True.
- Open this option, we re-enter Update-database, or error
The mistake is to say that the changes you are going to make will have a loss on your current data, so we don't do it without your authorization! Well, since you're not authorized, we'll give you authorization!
There are two ways to authorize, one is to use Update-database–force to force the modification The other is to add another sentence base.automaticmigrationdatalossallowed = True in the Configuration.cs file just now;
In the main method, if you call Musiccontext to create an instance, the Entity Framework will find the corresponding database based on the connection string.
- If the database does not exist, the database is created according to the data table description class
- If the table definition in the created database is inconsistent with the corresponding class definition in the program, delete the corresponding table, and then recreate the data table according to the class definition in the program
The Entity framework is based on the fact that the class definition and the actual database must be consistent, or the exception will be thrown.
Entity Framework Learning Note (2)