Using EF to manipulate different databases (SQLite for example)

Source: Internet
Author: User
Tags sqlite sqlite database

has been working against the database recently.

I was given a SQLite database from the Android platform, requesting the program to read different files. Because the field is too much, do not want to directly use the original directly read the DataTable way to do, manual write mapping too painful ... So I thought of EF coming.

So the problem, when learning ef, is generally directly in the app. Config or Web. config to write ConnectionString, the operation of a database is very good, but if you want to operate the database needs to be temporarily specified, it is more troublesome to write in the inappropriate.

My first thought was to use the overloads of the DbContext constructor

 Public Mydbcontext ()    :base("connectionstringorname") {}
View Code

This can accept the name of a connection string or config file.

P.S. When using the connection string, just fill it in, and when you use name, the fill looks like "name=myconn"

Using the name is not appropriate, directly using the connection string, provider how to specify? Don't specify whether to use SQL Express directly? Think of oneself, did not try again, should also be able, write finish again.

The second option is to use Database.connection to set the connection string, as follows:

 Public Mydbcontext (string  connection)        {            = getsqlitestring (connection);        }

Here does not call the method inside the base, for Mysqlite,getsqlitestring as follows:

        Private string Getsqlitestring (string  Connect)        {            return"Data source=" + connect;        }

This will enable the operation of the ConnectionString, only need to connect the time to pass a path on it.

Similarly, the use of other types of databases is also possible, although it is actually estimated that there are not many people.

Using EF to manipulate different databases (SQLite for example)

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.