.net core3.1 dapper use (Oracle connection strings)

After the hardships do not say (.net welcome newcomers do not like do not spray pointing) ado, directly on the topic:
1. First build a class to manage the database connection
public class DbFactory: IDbFactory
{
Readonly IOptions _Options;
public DbFactory (IOptions Options)
{
_Options = Options;
}
public the DbConnection the Create (DBType DbType = DBType.System)
{
the DbConnection CNN = null;
dboption DB = _options.Value;
Switch (db.DbModel)
{
Case DbModel.MySQL:
CNN new new = MySqlConnection (db.ConnectionString); Dapper.SimpleCRUD.SetDialect (Dapper.SimpleCRUD.Dialect.MySQL ); // because mysql statement does not support brackets, add this setting to avoid the error
BREAK;
Case DbModel.SQLServer:
CNN the SqlConnection new new = ( db.ConnectionString);
BREAK;

            case DbModel.Oracle:
                switch (dbtype)
                {
                    case DBType.System:
                        cnn = new OracleConnection(db.ORACLE_SYSTEM);
                        //cnn.Open();
                        break;
                    case DBType.Forwarder:
                        cnn = new OracleConnection(db.ORACLE_FORWARDER);
                        //这里要注意OracleConnection 引用.net core的 坑!!!
                        //cnn.Open();
                        break;
                }
                break;

            default:
                throw new Exception("please specify DbModel");
        }

        return cnn;
    }
}

Here Insert Picture Description

2. Add one line of code in the method ConfigureContainer Startup.cs file:
builder.RegisterType().As();
3.DbOption class definition database connection parameters
Here Insert Picture Description
4. The method of implantation ConfigureServices Startup.cs file:
services.Configure(Configuration.GetSection("DbOption"));
5. In appsettings.json file
Here Insert Picture Description
// noted here that connector I quote here the string is the SOURCE = local DESKTOP-BCJ2548 the DATA: 1522 / TESTFXC
6. here is the call
Here Insert Picture Description
7.Ok test is successful it!
Here Insert Picture Description
8. Come together!

Released nine original articles · won praise 1 · views 1476

Guess you like

Origin blog.csdn.net/FengxcLf/article/details/105022586