Developed using the Entity Framework 6.x Code-first method in Oracle
Last year, I wrote a simple learning note for EF, when EF did not support Oracle's Code-first development model, and today has no intention of looking at Oracle's official website, and found that ef6.x has been supported, and has given two tutorials (English version):
1.Using NuGet to Install and Configure Oracle Data Provider for. NET
2.Entity Framework Code First and code first migrations for Oracle Database
Basically do it, in order to facilitate the reluctant to chew English friends, the main steps "free translation" under:
Environment: Visual Studio +. Net Framework 4.5.2
1. Install, configure odp.net with NuGet
A) for reference, create a console Application project
Project name whatever it is, NuGet is in the picture.
This is the main program entrance.
b) Open Solution view
References reference node of the project right-click, select Manage NuGet Packages ...
Reference, search for Oracle, two items in the installation diagram:
During the installation, the License dialog box pops up, click I Accept
After the installation is successful, these two items should be automatically marked with a green tick.
After the installation is complete, the Readme.txt will be opened automatically, the Earth people generally do not look at this thing
Check the references of the project below, and you should see that 4 new DLL references were added automatically
And look at the app.
The following is automatically added:
These two places are used to configure the connection string, remember to modify
OK, ODP. NET Installation configuration complete
2. Using Code First Mode development
A) First refer to, modify the connection string (this article uses the HR example user, you can modify according to the actual situation)
Open Program.cs This file
Replace the following code:
View Code
The following is the main calling code that demonstrates the insert record
The following is the definition of the entity class, which is completely a Pojo object, either with tools or pure handwriting.
Here is the DbContext section, which is equivalent to the DAL layer. Note: onmodelcreating, this means that when creating a table from the model, the default will be created to the HR database schema
Run results
Open the Server Explorer panel
Connect to Oracle
You can see that two tables are automatically generated according to the model definition (note the following table name and automatically add the plural)
can view data directly
As you can see, 2 data has been inserted successfully
b) Model and database migration merge
The class definition of the data entity model tends to change as demand changes, and if attributes are increased or decreased, EF can automatically generate the corresponding DB script to modify the table structure synchronously
Refer First, enter PM console
Input enable-migrations enable the database migration feature
Then the employee class definition, the original commented out the Location property row, remove the comment (ie: added the Location property)
Back to PM console, enter add-migration first to generate the corresponding DB modification script
Last Input update-database Update table structure
Open the Server Explorer view to view the following Employees table
You can see that the new field location has been added
Using the entity Framework 6.x Code-first in Oracle