Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
445 views
in Technique[技术] by (71.8m points)

c# - Entity Framework defaultConnectionFactory for MySQL

The below configuration works fine when I specify the connectionString name as a parameter to the base constructor. Because I want to be able to change the default provider later on without recompiling, I want to set it in my app.config instead, but I have no idea what to provide as type for defaultConnectionFactory.

  <connectionStrings>
    <add name="MySQL" providerName="MySql.Data.MySqlClient" connectionString="SERVER=localhost;DATABASE=abc;UID=root;PASSWORD=;" />
  </connectionStrings>
  <entityFramework>
    <defaultConnectionFactory type="MySql.Data.Entity.MySqlConnectionFactory, EntityFramework"></defaultConnectionFactory>
    <providers>
      <provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6, Version=6.8.3.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d"></provider>
    </providers>
  </entityFramework>

Exception:

Failed to set Database.DefaultConnectionFactory to an instance of the 'MySql.Data.Entity.MySqlConnectionFactory, EntityFramework' type as specified in the application configuration. See inner exception for details.

Inner exception (translated, VS is set to English but some messages are still in my language):

Could not load type "MySql.Data.Entity.MySqlConnectionFactory" in assembly "EntityFramework"

The documentation states that the assembly name follows after the colon, thus I also tried MySql.Data.Entity.MySqlConnectionFactory, MySql.Data.Entity.EF6, giving me this exception:

Format of the initialization string does not conform to specification starting at index 0.

I'm using EF6, MySql.Data & MySql.Data.Entity.EF6 6.8.3.0.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The name property of the connectionString must be the same as your context. This works fine:

  <connectionStrings>
    <add name="NAMEOFYOURCONTEXT" providerName="MySql.Data.MySqlClient" connectionString="Server=localhost;Database=abc;Uid=root;Pwd='';" />
  </connectionStrings>
  <entityFramework>
    <defaultConnectionFactory type="MySql.Data.Entity.MySqlConnectionFactory, MySql.Data.Entity.EF6"></defaultConnectionFactory>
    <providers>
      <provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6"></provider>
    </providers>
  </entityFramework>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...