8 The Oracle Entity Framework Data Provider : Code First and Model First Support

Code First and Model First Support
Entity Framework 4.1 and 4.2 provide support for the Model First and Code First concepts. Implementing support for these features required changes to the data provider, such as the way that long identifier names are handled. However, these changes should not require changes to your application.
Code First and Model First implementations require type mapping changes. See “Type Mapping for Model First” for more information.
Handling Long Identifier Names
Most Oracle identifiers have a maximum length of 30 bytes. The identifier name can exceed this size because the names of the objects to be created on the server are taken from the class and property names. In addition, constraint names are often created by concatenating several object names; in these cases, the chances of exceeding the maximum identifier length are even greater.
The data provider shortens identifiers to 30 bytes, replacing the end of the identifier with an integer hash-code, for example, the string Seq_RkoPersonWithMoodAndShirts_Id is shortened to Seq_RkoPersonWithMo_3584187919. If you access or view the DB object using a DB tool, the names of the created tables may differ from what you might expect based on the Plain Old CLR Object (POCO) class names and property names (Code First), or the entity names and entity property names (Model First).
Note that when two identifiers that have the same leading characters are shortened, the difference between the identifiers is less obvious to a visual inspection. For example, a table has two supporting sequences, Seq_RkoPersonWithMoodAndShirts_Id and Seq_RkoPersonWithMoodAndJackets_Id. When these sequences are shortened, they are renamed as Seq_RkoPersonWithMo_3584187919 and Seq_RkoPersonWithMo_2865783621.
Creating an Entity Data Model Using the Entity Data Wizard and Modeling Tool
You can use the Entity Data Model Wizard to create an Entity Data Model. In the Properties window, you select the database schema and a template to be used to generate the data definition language (DDL) statements required to create or delete the database.
The Oracle Entity Framework data provider includes a T4 text template, DataDirect SSDL ToOracle.tt. You can edit the text template for modifying the naming conventions of constraints, sequences, triggers, and so on. Because the file is installed under the Visual Studio installation directory, the Generate Database wizard adds a VS identifier to the file name, as shown in the following figure.
The DDL Connection Template Code Generation Strategy option is selected. The values in the drop-down list all have a VS identifier to show that the text template is installed under the Visual Studion installation directory..
The Oracle Entity Framework data provider uses the connection's current schema name, specified by the Database Schema Name property, when generating DDL. Note that the value of the Database Schema Name property is case sensitive. When the schema name value is set to default, that is, do or left blank, the data provider uses the connection’s current schema.
Using OracleConnectionFactory
When a Code First application does not provide a connection or name when creating a DbContext, the Entity Framework uses the DefaultConnectionFactory to create a DbConnection object to service the context. The application can set Database.DefaultConnectionFactory using a named connection string, as shown in the following example:
app.config entry
<configuration>
   <connectionStrings>
<add name="EF_Oracle_10g_UTF8" connectionString="host=server1;port=1522;user id=test;password=xxx;sid=NorthwindEFUTF8" providerName="DDTek.Oracle" />
   </connectionStrings>
</configuration>
The following code snippet shows how the OracleConnectionFactory class can be used to provide connection information. Because the Entity Framework uses the SqlCeConnectionFactory class by default, you must specify the OracleConnectionFactory interface.
Database.DefaultConnectionFactory = new OracleConnectionFactory();
EF41SampleContext context = new EF41SampleContext("Host=Server1;
Password=TEST38; Port=1521; SID=CP871; User ID=TEST38");
context.Pocos.Add(poco);
context.SaveChanges();
If the EntityFramework.dll revision referenced by your application is subsequent to the revision used by the Entity Framework data provider (currently version 4.2), an error message is returned:
Could not load file or assembly 'EntityFramework, Version=4.2.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference.
When the DefaultConnectionFactory is set to an OracleConnectionFactory instance, the Entity Framework uses an OracleConnection instead. For example, you can modify the app.config file to include connection information, as shown in the following code snippet:
app.config entry
<configuration>
   <connectionStrings>
     <add name="EF_Oracle_10g_UTF8" connectionString="host=server1;
        port=1522;user id=test;password=xxx;sid=NorthwindEFUTF8"
        providerName="DDTek.Oracle" />
   </connectionStrings>
</configuration>
Create the context as follows:
model2 ctx = new model2("EF_Oracle_10g_UTF8");
The OracleConnectionFactory also lets you specify a "base" connectionString to use that can act as a default connection string.