EF5和Winform的MySql异常

时间:2014-01-13 19:04:30

标签: c# mysql winforms entity-framework

我正在尝试使用EF5创建一个MySql Db,所以我有我的dbSet和My SqlContext类,但是当我运行该程序时抛出此异常enter image description here

我在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 Studio 2012.希望你们能帮我解决这个问题!

1 个答案:

答案 0 :(得分:1)

我在帖子中找到了回复,即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>());

现在有效!