Multiple data source

In DbEntry, DataProvider is the main interface to access execute SQL. And ORM layer will call it to access database. From v4.0, The model stuffs moved to a class called ModelContext, all the things about the model such as ObjectInfo, ModelOprator etc, are in this class now.

If there is DefaultContext node in App.config/Web.config, it will use this argument to get the default context, otherwise it will use the empty string as the argument to get it.

    <add key="DataBase" value="@Access : @~test.mdb" />

And the following is the same meaning as above:

    <add key="DefaultContext" value="Access" />
    <add key="Access.DataBase" value="@Access : @~test.mdb" />

More details about config file please read Configuration.

And we can use other context(s) to operate other database(s).

    <add key="DataBase" value="@Access : @~test.mdb" />
    <add key="Excel.DataBase" value="@Excel : @~test.xls" />

And all of the key names of one database setting should have the same prefix:

    <add key="DefaultContext" value="MySql" />

    <add key="MySql.DataBase" value="@MySql : server=localhost;user id=root; password=123; database=mytest; pooling=false" />
    <add key="MySql.DbProviderFactory" value="@SmartDbFactory : MySql.Data, Version=, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />

    <add key="sqlite.DataBase" value="@SQLite : @~Test.db" />
    <add key="sqlite.DbProviderFactory" value="System.Data.SQLite.SQLiteFactory, System.Data.SQLite, Version=, Culture=neutral, PublicKeyToken=db937bc2d44ff139" />

The model should be defined to use the specified context by attribute:

public User : DbObjectModel<User>
    public string Name { get; set; }
var l1 = User.FindAll();
var l2 = DbEntry.From<User>().Where(null).Select();

And we can use specified ModelContext to get more feature(s) like:

var ctx = ModelContext.GetInstance(typeof(User));

Last edited Apr 9, 2014 at 2:08 PM by lifeng, version 11


No comments yet.