. NET Entity Framework (with Oracle odp.net)-code First

Source: Internet
Author: User
Tags class definition connectionstrings oracle developer

The previous article introduced the. NET Entity Framework and demonstrated the model first pattern, and this article will continue to discuss the implementation of Code first mode.  

I. Summary

1. Target

This document verifies the use of the entity Framework (EF) Code first through Oracle Data Provider for. NET (odp.net). Created first. NET classes, which create tables for these classes in the database through the framework, modify these classes, and migrate and map changes to the corresponding tables through code first.

2. Preface

The logical schema of the Entity Framework abstract relational database is mapped to. NET application, it provides developers with the ability to map object relationships. Code first enables the developer-developed class to be transformed into a database entity table. Code first migrations the changes to the class to accurately map to the database's corresponding table.

Since Oracle Data Access components (ODAC) 12c Release 3 (12.1.0.2.1) This version begins, the ODP. NET supports code first and code first migrations, ODP. NET provides the database storage, fetching and updating capabilities of the EF model.

This article builds EF programs, generates "Employee" and "Department" classes, and adds object data for those classes. When the program runs, it maps the class to a database table and stores the object as a table record row. New properties are then added to the class, and these properties are added to the table's column information through code first migrations.

3. Environmental conditions

Before you begin, you should meet the following criteria:

    • Visual Studio 2013 above,. Net Framework 4.5 or higher
    • Oracle 11g Release 2 or more
    • Download these files to your working directory (the downloaded file is Programecs.txt)
    • Please read "Configuring Odp.net with NuGet Installation", this article will show you how to install and configure the Entity Framework 6 and odp.net, and verify related usage through a console program.
    • If you do not understand what the ODT is, this article may be slightly. Oracle Developer Tools (ODT) can be integrated into the Visual Studio environment to assist with Oracle development, but this is not required for code first. This article can be accessed by Server Explorer to explore changes to Oracle database objects to verify code first changes to the database. If you want to use the ODT, please go to Oracle official website to download.

This article will also take the example of "HR" in Oracle, but note that data table objects are named to avoid conflicts.

Second, Code first

This section will build two classes of "Employee", "Department", and store the relevant data. When the program runs, EF Code first creates the corresponding tables for these two classes and generates a table's data record when the class instance data is generated and saved.

1. Open the project you created in the previous article (click here to enter) and Open App. Config. Under the <connectionStrings> configuration section, modify the user name and password to connect to the HR database. Note the name of the link:oracledbcontext, We'll have this name in the program later.

2. Copy the code snippet from the downloaded file and glue it to the Program.cs of the project. This code requires the configuration of odp.net and EF, which has been configured in the previous article. Reading the code carefully reveals that the database operation class Oracledbcontext name is the same as the name in the <connectionStrings> configuration section mentioned in step 1th. The program uses connection string information to connect to the database, then creates an instance of Employee and Department, and generates data.

For example, look at how these two classes are defined in a program. Each class contains fields that can be read and saved. One of the fields (location) is commented out, and subsequent comments on the field are removed.

If you do not want to use the HR architecture, you can Code "Modelbuilder.hasdefaultschema" ("HR"); , replace HR with the schema you want to use.

3, run the program, the database will be created "Employees" and "departments" table, and add a piece of data. Note that these table names are case-sensitive.

4, check the database, the data of these tables is added normally. After running the program, click HR in the server Explorer . ORCL link. This step requires the installation of the configuration ODT (Oracle Developer Tools for Visual Studio)

Please enter the correct password

Expand the table to check whether the two tables defined in the class are created

Right-click departments and Employees to Query the table data to verify the consistency of the data with the inserts in the program.

Third, Code first migration

Classes change the class definition as business requirements and database schemas change, this section changes the employee class to simulate this change. With a small amount of code, you can synchronize class changes to database tables.

In the second part of the code, the third table _migrationhistory is created at the same time. This table will track the changes in the code First class.

This step demonstrates adding a field location to the employee class and synchronizing it to a database table.

1. Turn on View > Other Windows > Package Manager Console. The function of this window is to enter the migration command of code first to synchronize. NET classes and database structures.

2, enter the enable-migrations in the window. This command allows the Code first migrations function to be enabled.

3, remove the comment in Program.cs, simulate the case of adding a field in the Employee class.

4. In the PMC window, enter Add-migration first, and the framework will generate migration code for the changes in the data model.

5. Type in the window Update-Database and the changes will be synchronized to the database

6, then look at the changes in the database table. Open the Employees table in Server Explorer

Verify that the Location field is added

Iv. Summary

This article points to the following:

    • Use Odp.net to create a Code first app that stores classes and data in a database table
    • Updating the database structure using Code first migrations

. NET Entity Framework (with Oracle odp.net)-code First

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.