gpt4 book ai didi

c# - EF5 和 Winform 的 MySql 异常

转载 作者:行者123 更新时间:2023-11-29 13:19:03 25 4
gpt4 key购买 nike

我正在尝试使用 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。希望你们能帮助我!

最佳答案

我在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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com