Entity Framework Code First pseudo implementation in Oracle, entityoracle
Why is it a pseudo-implementation, because it cannot be as full as in MsSql. We still need to manually create the database in Oralce. Here, we discard the EDMX file in Model First and write the Model and ing relationships in the Code. This is a bit like the Code First Model, so I say it is a pseudo implementation. The complete Code First should be supported by the Oracle development driver.
To connect to the Oracle database through EF, you must download the ODP. NET driver. Google will find it.
Model
public class Student { public Student() { this.Teachers = new HashSet<Teacher>(); } public Guid ID { get; set; } public string Name { get; set; } public int StudnetNumber { get; set; } public bool IsMale { get; set; } public virtual ICollection<Teacher> Teachers { get; set; } } public class Teacher { public Teacher() { this.Students = new HashSet<Student>(); } public Guid ID { get; set; } public string Name { get; set; } public virtual ICollection<Student> Students { get; set; } }
The model is simple. Here we have written two sample models, Student and Teacher, and they have many-to-many relationships.
Primary Key preparation
There are two primary keys in the Oralce Database: the backend uses the database itself to generate a self-increasing primary key, for example, the combination of trigger and sequence; another way is to assign a value to the ID displayed on the front-end, such as ID = 1, to be inserted into the database. If you want to manually insert data through the foreground, You need to configure the auto-increment of the primary key to NONE.
HasKey(k => k.ID);
Property(p => p.ID).HasColumnName("ID").HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
Data Type ing
ODP. NET supports automatic ing of most data types, such as int, datetime, and string. However, for some special data types, such as boolean, guid, and Oracle databases, there is no corresponding bit type or GUID type. We need to display the specified value during ing.
// GUID ing Property (p => p. typeCode ). hasColumnName ("TYPECODE "). hasColumnType ("GUID"); // ing Property (p => p. isSale ). hasColumnName ("ISSALE "). hasColumnType ("odp_internal_use_type ");
Link ing
ODP. NET can support one-to-many ing, just as we configured MsSql database.
Multi-to-Multi-relationship ing: because two multi-to-many tables generate an intermediate table to save the primary keys of the two tables, this requires EF to obtain the primary keys of the two tables. If the primary keys of these two tables are generated by Sequence in the database, EF cannot obtain them. Therefore, if you want to configure the many-to-many relationship between tables, the table's primary key must be inserted through the front-end display.
HasMany(t => t.Teachers).WithMany(s => s.Students).Map(m => { m.MapLeftKey("STUDENTID"); m.MapRightKey("TEACHERID"); m.ToTable("STUDENT_TEACHER", "GYOUNG"); });
The total ing is as follows:
Public class StudentConfiguration: EntityTypeConfiguration <Student> {public StudentConfiguration () {HasKey (k => k. ID); Property (p => p. ID ). hasColumnName ("ID "). hasDatabaseGeneratedOption (DatabaseGeneratedOption. none) // GUID type ing. hasColumnType ("GUID"); Property (p => p. name ). hasColumnName ("NAME"); // ing Property (p => p. isMale ). hasColumnName ("ISMALE "). hasColumnType ("odp_internal_use_type"); Property (p => p. studentNumber ). hasColumnName ("STUDENTNUMBER"); haswon (t => t. teachers ). withiterator (s => s. students ). map (m => {// The primary key m of the current table of LeftKey. mapLeftKey ("STUDENTID"); // The primary key m of another table in RightKey. mapRightKey ("TEACHERID"); // you must specify the database architecture name m. toTable ("STUDENT_TEACHER", "GYOUNG") ;}); // table name + database architecture name ToTable ("STUDENT", "GYOUNG") ;}} public class TeacherConfiguration: entityTypeConfiguration <Teacher> {public TeacherConfiguration () {HasKey (k => k. ID); Property (p => p. ID ). hasColumnName ("ID "). hasDatabaseGeneratedOption (DatabaseGeneratedOption. none ). hasColumnType ("GUID"); Property (p => p. name ). hasColumnName ("NAME"); ToTable ("TEACHER", "GYOUNG ");}}
DbContext
public class TestContext : DbContext { public DbSet<Student> Students { get; set; } public DbSet<Teacher> Teachers { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Configurations.Add(new TeacherConfiguration()).Add(new StudentConfiguration()); base.OnModelCreating(modelBuilder); } }
SQL statement for table creation:
-- Creating table 'TEACHERS'CREATE TABLE "GYOUNG"."TEACHERS" ( "ID" RAW(16) NOT NULL, "NAME" NVARCHAR2(200) NULL);-- Creating table 'STUDENTS'CREATE TABLE "GYOUNG"."STUDENTS" ( "ID" RAW(16) NOT NULL, "NAME" NVARCHAR2(200) NULL, "ISMALE" NUMBER(10,0) NULL, "STUDENTNUMBER" NUMBER(10,0) NULL);-- Creating table 'STUDENT_TEACHER'CREATE TABLE "GYOUNG"."STUDENT_TEACHER" ( "STUDENTID" RAW(16) NOT NULL, "TEACHERID" RAW(16) NOT NULL);
If you forget the configuration, subsidize it:
<?xml version="1.0" encoding="utf-8"?><configuration> <configSections> <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --> <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /> </configSections> <connectionStrings> <add name="MfTest" providerName="Oracle.DataAccess.Client" connectionString="Data Source=Gyoung;user id=test;password=123456" /> </connectionStrings> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> </startup> <entityFramework> <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" /> </entityFramework> <oracle.dataaccess.client> <settings> <add name="bool" value="edmmapping number(1,0)" /> <add name="byte" value="edmmapping number(3,0)" /> <add name="int16" value="edmmapping number(4,0)" /> <add name="int32" value="edmmapping number(9,0)" /> <add name="int64" value="edmmapping number(18,0)" /> </settings> </oracle.dataaccess.client></configuration>
MfTest is the connection string, which is similar to SQLSERVER and has no key design. The database name is Gyoung. You must configure the listening address in the tnsnames. ora file of Oracle.
Reference page: http://qingqingquege.cnblogs.com/p/5933752.html