EF6 and MySQL Troubleshooting issues record

Source: Internet
Author: User

These days the company structure adjustment, the new iteration back end uses the ABP framework and the Codefirst model, the execution process encountered a non-inevitable difficult to locate the problem, hereby records.

Phenomenon

The program accesses the MySQL database times an exception

System.InvalidOperationException:The default Dbconfiguration instance is used by the Entity Framework before the ' Mysqle Fconfiguration ' type was discovered. An instance of ' mysqlefconfiguration ' must is set at application start before using any Entity Framework features or must Be registered in the Application ' config file.

Find a lot of solutions on the StackOverflow, can not find reliable reason analysis, toss for a long time finally in the official documents found the answer.

Reason


First look at an explanation of the official documentation:

(https://msdn.microsoft.com/en-us/data/jj680699#Moving)

There is some situations where configuration may is needed before any DbContext type have been used. Examples of this include:

  • Using Dbmodelbuilder to build a model without a context
  • Using Some other framework/utility code that utilizes a DbContext where the context is used before your application conte XT is used

In such situations EF are unable to discover the configuration automatically and you must instead do one of the following:

  • Set the dbconfiguration type in the config file, as described in the moving dbconfiguration section above
  • Call the static Dbconfiguration.setconfiguration method during application startup

The official document address given in parentheses contains two ways to specify the DbContext configuration:

One, config node

Second, the dbconfigurationtype characteristics are marked on the DbContext

We chose the second method, that is, the way to configure automatically after the specified type. The code snippet is as follows:

[Dbconfigurationtype (typeof(MySql.Data.Entity.MySqlEFConfiguration))]      Public class Shmedicalrecorddbcontext:abpdbcontext    {        ......    }

This is not a problem in a standalone project, but in the case of a previous project there is a bug reported in the question, such as the official explanation that some of the cases are not automatically configured: some frameworks or generic code used it before the program called DbContext.

How to understand this problem, the situation of our project team is:

Legacy systems also use EF6, the new iteration of the development of the system needs to be embedded in the old system to use, then there are two dbcontext in the old system, if the new system DbContext first initialized, then the program will not have a problem.

If the context of the legacy system is initialized first, it conforms to the official description of the second, so there is an exception. (The new system is initialized with multi-threaded start, so the sequence of each start service is different)

Workaround

The solution is also in the text below, using config or static initialization to explicitly specify the dbconfiguration, and then I add a specified in the EF node of config, the code snippet is as follows:

<entityframework codeconfigurationtype="MySql.Data.Entity.MySqlEFConfiguration, MySql.Data.Entity.EF6"> .....

Problem solving.

Leave something handy for the man who later stepped on the pit.

Another, ef+mysql do codefirest phase is not very good (Oracle also almost), automatic migration of various acclimatized, conditional company or recommended on MSSQL.

EF6 and MySQL Troubleshooting issues record

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.