EF是否完全支持跨平台数据库?

时间:2021-04-18 12:26:23

For past few hours, I tried to "port" my current project from Sql Server to MySQL, I ended up with errors like:

在过去的几个小时里,我试图将我当前的项目从Sql Server“移植”到MySQL,结果出现了如下错误:

The underlying provider does not support the type varchar(max) (or uniqueidentifier, or something else)

底层提供程序不支持类型varchar(max)(或惟一标识符,或其他的)

I use POCO, and Code First - I thought, that one of the purposes of EF was to provide easy repository "switching", but it would seam that even basic types are not mapped correctly :/

我使用POCO和代码,我想,EF的目的之一是提供简单的存储库“切换”,但是即使基本的类型没有正确映射,它也会这样做:/。

I also tried SQlite but it is even in worst shape, and as far as I know, Postrage SQL support is also lacking.

我还尝试了SQlite,但它的形状很糟糕,据我所知,Postrage SQL支持也很缺乏。

So... question is - is there any cross-platform (preferably free) database out there that has real (SQL Server like) support in EF?

所以…问题是,在EF中是否存在一个具有真实(SQL Server like)支持的跨平台(最好是免费的)数据库?

Thanks in advance.

提前谢谢。

Best regards.

致以最亲切的问候。

Edit:

编辑:

To by more specific about my approach:

更具体地说我的方法:

I use POCO, and Code First, I want to automatically create the database if it doesn't exists, I don't use anything special inside my POCO classes, just standard .NET types. Example POCO looks like this:

我使用POCO和代码,如果它不存在,我想自动创建数据库,我在POCO类中不使用任何特殊的东西,只是标准的。net类型。例子POCO是这样的:

[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int ID { get; set; }

[Required]
[StringLength(128)]
public string Name { get; set; }

[Required]
public int AgencyID { get; set; }

[Required]
public bool IsEnabled { get; set; }

#region NavigationProperties

[NavigationProperty]
public virtual Agency Agency { get; set; }

#endregion

Edit2:

Edit2:

My app.config looks like this:

我的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=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <entityFramework>
    <!--<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>-->
    <defaultConnectionFactory type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data" />
  </entityFramework>
</configuration>

2 个解决方案

#1


2  

You seem to have SqlServer set as your defaultConnectionFactory in the app/wbeb.config file which results in EF using SqlServer types. You need to set the defaultConnectionFactory to a MySQL connection factory.

您似乎在app/wbeb中设置了SqlServer设置为defaultConnectionFactory。配置文件,该文件使用SqlServer类型导致EF。您需要将defaultConnectionFactory设置为MySQL连接工厂。

#2


0  

I struggled with this same error all night. The fix was counter-intuitive but so far so good... It does seem that the issue had to do with old migrations that were set up with LocalDB (SQL Server) yet even deleting the old migrations had no effect. I also spent lots of unnecessary time fiddling with my Web.config, Database Context, etc... I suggest trying the following before banging your head on the wall...

我整晚都在做同样的错误。这个修正是违反直觉的,但到目前为止还不错……这个问题似乎与使用LocalDB (SQL Server)设置的旧迁移有关,但即使删除旧的迁移也没有影响。我也花了很多不必要的时间摆弄我的网页。配置、数据库环境,等等……我建议你在把脑袋撞到墙上之前试一下……

  1. Delete the migrations folder
  2. 删除迁移文件夹
  3. Try enabling migrations and adding your initial migration (in the PM type "enable-migrations" and then "add-migration init")
  4. 尝试启用迁移并添加初始迁移(在PM类型的“enable-migrations”和“add-migration init”中)
  5. If you get the above error in the PM, delete the migrations folder again, then open up MySQL Workbench
  6. 如果您在PM中得到上述错误,请再次删除migrations文件夹,然后打开MySQL工作台。
  7. Within MySQL Workbench, locate the Schema with the name you gave in your connectionString (will look like database=whateverYouCalledIt)
  8. 在MySQL Workbench中,在connectionString(将会看起来像数据库=whateverYouCalledIt)中找到模式的名称
  9. In that schema, open the tables and find "__migrationhistory"
  10. 在该模式中,打开表并找到“__migrationhistory”
  11. Right click that table, click select rows, and delete any existing migration entries, then repeat step 2
  12. 右键单击该表,单击“选择行”,并删除现有的迁移条目,然后重复步骤2。

For some reason this did the trick for me when deleting migrations in the solution explorer had no effect. Apparently, that didn't delete the migration history in the database...

出于某种原因,当删除解决方案资源管理器中的迁移时,这对我起了很大的作用。显然,这并没有删除数据库中的迁移历史……

And for the record to the OP, this worked just fine with ASP.net Identity and the MVC Auth system... I think SimpleMembership is now antiquated but then I'm not developing desktop apps...

对于OP的记录,这对ASP.net Identity和MVC Auth系统都很好…我认为SimpleMembership现在已经过时了,但我还没有开发桌面应用程序……

#1


2  

You seem to have SqlServer set as your defaultConnectionFactory in the app/wbeb.config file which results in EF using SqlServer types. You need to set the defaultConnectionFactory to a MySQL connection factory.

您似乎在app/wbeb中设置了SqlServer设置为defaultConnectionFactory。配置文件,该文件使用SqlServer类型导致EF。您需要将defaultConnectionFactory设置为MySQL连接工厂。

#2


0  

I struggled with this same error all night. The fix was counter-intuitive but so far so good... It does seem that the issue had to do with old migrations that were set up with LocalDB (SQL Server) yet even deleting the old migrations had no effect. I also spent lots of unnecessary time fiddling with my Web.config, Database Context, etc... I suggest trying the following before banging your head on the wall...

我整晚都在做同样的错误。这个修正是违反直觉的,但到目前为止还不错……这个问题似乎与使用LocalDB (SQL Server)设置的旧迁移有关,但即使删除旧的迁移也没有影响。我也花了很多不必要的时间摆弄我的网页。配置、数据库环境,等等……我建议你在把脑袋撞到墙上之前试一下……

  1. Delete the migrations folder
  2. 删除迁移文件夹
  3. Try enabling migrations and adding your initial migration (in the PM type "enable-migrations" and then "add-migration init")
  4. 尝试启用迁移并添加初始迁移(在PM类型的“enable-migrations”和“add-migration init”中)
  5. If you get the above error in the PM, delete the migrations folder again, then open up MySQL Workbench
  6. 如果您在PM中得到上述错误,请再次删除migrations文件夹,然后打开MySQL工作台。
  7. Within MySQL Workbench, locate the Schema with the name you gave in your connectionString (will look like database=whateverYouCalledIt)
  8. 在MySQL Workbench中,在connectionString(将会看起来像数据库=whateverYouCalledIt)中找到模式的名称
  9. In that schema, open the tables and find "__migrationhistory"
  10. 在该模式中,打开表并找到“__migrationhistory”
  11. Right click that table, click select rows, and delete any existing migration entries, then repeat step 2
  12. 右键单击该表,单击“选择行”,并删除现有的迁移条目,然后重复步骤2。

For some reason this did the trick for me when deleting migrations in the solution explorer had no effect. Apparently, that didn't delete the migration history in the database...

出于某种原因,当删除解决方案资源管理器中的迁移时,这对我起了很大的作用。显然,这并没有删除数据库中的迁移历史……

And for the record to the OP, this worked just fine with ASP.net Identity and the MVC Auth system... I think SimpleMembership is now antiquated but then I'm not developing desktop apps...

对于OP的记录,这对ASP.net Identity和MVC Auth系统都很好…我认为SimpleMembership现在已经过时了,但我还没有开发桌面应用程序……