MySql实体框架auto_increment错误

本文关键字:increment 错误 auto 实体 框架 MySql | 更新日期: 2023-09-27 18:16:20

我有一个系统,使用实体框架对运行良好的SQL Server数据库。最近我决定将MySql作为后端。这个错误是由一些代码导致的,这些代码在SQLServer上运行得很好,但是在MySql上失败了。

MySql 5.6.27, EF6.

我有一个表1列(称为Id),我想用它作为一个序列计数器。我通过使Id为主键和auto_generated来实现这一点。

下面是表def:

create table tblCompanySequence (
    Id int auto_increment primary key not null default 1
);
下面是相应的c# def:
using System.Data.Linq.Mapping;
namespace Foo.DataAccess.EF.Entity
{
    [Table(Name = "tblCompanySequence")]
    public class EFCompanySequence
    {   
        [Column(IsPrimaryKey = true, IsDbGenerated = true, AutoSync = AutoSync.OnInsert)]
        public int Id { get; set; }
    }
}

代码如下:

var newSeq = new EFCompanySequence();
var tableSeq = context.GetTable<EFCompanySequence>();
tableSeq.InsertOnSubmit(newSeq);
context.SubmitChanges();
var newId = newSeq.Id;

我得到一个错误的调用提交更改。

A first chance exception of type 'MySql.Data.MySqlClient.MySqlException' occurred in System.Data.Linq.dll
Additional information: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DEFAULT VALUES
SELECT CONVERT(Int,SCOPE_IDENTITY()) AS `value`' at line 1

我已经尝试了一些排列,例如:

create table tblCompanySequence (
    Id int auto_increment not null,
    primary key (Id)
);

和在EF表对象上使用DbGenerated注释,但仍然碰壁。

任何建议都非常感谢。

欢呼,安迪

UPDATE 1:

以下是我的配置设置(按照https://dev.mysql.com/doc/connector-net/en/connector-net-entityframework60.html设置)

<configSections>
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<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.9.7.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
    </DbProviderFactories>
</system.data>
<entityFramework codeConfigurationType="MySql.Data.Entity.MySqlEFConfiguration, MySql.Data.Entity.EF6">
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
    <providers>
        <provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6"/>
        <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
</entityFramework>
更新2:

我在另一个网站上读到,我可以使用下面的代码来克服这个问题。

var newId = context.ExecuteCommand("insert into tblCompanySequence values (null); select LAST_INSERT_ID();");

这段代码成功地向数据库中插入了一个id递增的新行,但是从选择中返回的值始终是1。

我敢肯定这一定是我做错了什么。

MySql实体框架auto_increment错误

你应该配置你的EntityFramework与MySql一起工作,然后它将使用LAST_INSERT_ID()而不是score_identity()。查看配置文件

  <connectionStrings>
<add name="DefaultConnection"
providerName="MySql.Data.MySqlClient"
connectionString="[Insert your ConnectionString to the mysql database here]"/>
</connectionStrings>

你应该配置你的EntityFramework与MySql一起工作并根据您的需求修改下面的代码

MySQL语法下面的SQL语句将"ID"列定义为"Persons"表中的自动递增主键字段:

CREATE TABLE Persons(ID int不NULL AUTO_INCREMENT,LastName varchar(255) NOT NULL,FirstName varchar (255),地址varchar (255),城市varchar (255),主键(id))

如果你真的需要这个,你可以在这个语法的帮助下实现你的目标。试试这个,并根据您的需要修改它:

CREATE TABLE table1_seq
(
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY
);
CREATE TABLE table1
(
id VARCHAR(7) NOT NULL PRIMARY KEY DEFAULT '0', name VARCHAR(30)
);

试试这个。我希望你能理解,

我想我可能已经找到了答案(但还没有令人满意的解决方案)。

我使用的是Linq to SQL(不是Linq to Entities), MySql中似乎没有开箱即用的支持。数据等。

我已经开始玩的分离linqExpress解决方案,但在最初的一瞥看来,我将不得不重新编写代码的实质性部分,使此工作。

除非有人有别的解决办法。

欢呼。