Some things about Oracle and EntityFramework (EF)

Source: Internet
Author: User
Tags static class

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)

Related Article

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.