我正在尝试使用EF5创建MySql Db,所以我有dbSet和My SqlContext类,但是当我运行程序时会抛出此异常
我的App.config中有此代码
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="v11.0" />
</parameters>
</defaultConnectionFactory>
</entityFramework>
<system.data>
<DbProviderFactories>
<remove invariant="MySql.Data.MySqlClient"/>
<add name="MySQL Data Provider" invariant="MySql.Data.MySqlClient" description=".Net Framework Data Provider for MySQL" type="MySql.Data.MySqlClient.MySqlClientFactory,MySql.Data, Version=6.7.4.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
</DbProviderFactories>
</system.data>
<connectionStrings>
<add name="ConnectionStringName" connectionString="server=localhost;Database=auth;uid=root;pwd=1234;" providerName="MySql.Data.MySqlClient" />
</connectionStrings>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="MySql.Data" publicKeyToken="c5687fc88969c44d" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-6.7.4.0" newVersion="6.7.4.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
和MySqlContext.cs
public class MySqlContext : System.Data.Entity.DbContext
{
public System.Data.Entity.DbSet<LoginModel> LoginModel { get; set; }
//public System.Data.Entity.DbSet<Roles> Roles { get; set; }
public MySqlContext()
: base("ConnectionStringName")
{ }
}
我正在使用Entity Framework 5,MySqlConector 6.7.4.0,Visual Studio2012。希望大家对此有所帮助!
最佳答案
我找到了user2453734指出EF5 Getting this error message: Model compatibility cannot be checked because the database does not contain model metadata的帖子的答案,并在Program.cs中的SetInitializer中进行了更改,
这个:
Database.SetInitializer(new DropCreateDatabaseIfModelChanges<MySqlContext>());
到这个:
Database.SetInitializer<MySqlContext>(new DropCreateDatabaseIfModelChanges<MySqlContext>());
现在可以了!
关于c# - EF5和Winform的MySql异常,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21099275/