Profile
There are always problems when Oracle and EF work together, so here's a summary of the solution.
Schema schema
People who have used Oracle should know that Oracle's user name is typically its schema name, and if you are building an EDMX file in database First mode, then congratulations, once you change the username you will get an error!
So try to avoid using EDMX to use, recommended model first mode, is to change the data structure, the corresponding models have been changed.
At this point, there is a way to avoid the pattern problem:
protected override void OnModelCreating(DbModelBuilder modelBuilder){ //模式调整 modelBuilder.HasDefaultSchema(GetSchemaByConName(ConnectionName));}
This initializes the schema based on the database connection string, avoiding the pattern pit.
Table and column names uppercase
Oracle's table and column names are uppercase by default, case-sensitive, but when I first encountered them, I was so miserable ...
The method is later found to convert all column names (attribute names), table names all to uppercase, and add a uniform mapping.
//<summary>//Create//</summary>/<param name= "Modelbuild Er "></param> protected override void Onmodelcreating (Dbmodelbuilder modelBuilder) {//table name Adjust the modelbuilder.types (). Configure (x = x.totable (Efstartup.settablename (X.clrtype.name))); if (isoracledb) {//Mode adjustment Modelbuilder.hasdefaultschema (Efstartup.getschemabyconna Me (connectionname)); Column name Adjustment modelbuilder.properties (). Configure (c = c.hascolumnname (C.clrpropertyinfo.name.toupper ())); } }
<summary>//EF Preheat//</summary> internal static class Efstartup {//<summary >////For the schema name (Oracle)////</summary>//<param name= "ConName" ></param> <returns></returns> internal static string Getschemabyconname (String conName) { if (string. IsNullOrEmpty (ConName)) throw new ArgumentNullException (Nameof (conName)); var c = Configurationmanager.connectionstrings[conname]; if (c = = null) throw new InvalidOperationException ("Db Connection not Found:" + conName); if (string. IsNullOrEmpty (c.connectionstring)) throw new Exception ($ "ConnectionString {conName} is empty!"); Get the user Id var sp = C.connectionstring.split (';'); var u = sp. Where (x =!string. IsNullOrEmpty (x) && x.contains ("=")). FirstOrDefault (y = Y.trim (). ToLower (). StartsWith ("User")); if (U = null) throw new Exception ($ "ConnectionString {conName} is invalid!"); SP = u.split (' = '); Return sp[1]. Trim (). ToUpper (); }///<summary>//Adjustment table name///</summary>//<param name= "TableName" ;</param>//<returns></returns> public static string Settablename (String tableName) {var N2 = Tablename.replace ("Ms", "Mon_"); if (N2. EndsWith ("History")) N2 = n2. Replace ("History", "he"); N2 = n2. ToUpper (); return n2; } }
With this setting, you can capitalize the names of all the entities, add prefixes as needed, and change all the fields in the entity model to uppercase so that you don't have to tangle with those.
Some things about Oracle and EntityFramework (EF)