简体   繁体   English

MySql Entity Framework auto_increment错误

[英]MySql Entity Framework auto_increment error

I'm have a system that uses Entity Framework against a SQL Server db that runs fine. 我有一个系统,该系统对运行良好的SQL Server数据库使用Entity Framework。 Recently I decided to move to MySql as the backend. 最近,我决定将MySql作为后端。 The error is caused by some code that runs perfectly well against SQLServer, but fails against MySql. 该错误是由于某些代码在SQLServer上运行得很好而在MySql上失败导致的。

MySql 5.6.27, EF6. MySql 5.6.27,EF6。

I have a table with 1 column (called Id), which i want to use as a sequence counter. 我有一个带有1列(称为Id)的表,我想将其用作序列计数器。 I achieve this by making Id a primary key and auto_generated. 我通过将ID作为主键并自动生成来实现此目的。

Here is the table def: 这是表def:

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

Here is the corresponding c# def: 这是相应的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; }
    }
}

and here is the code: 这是代码:

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

I get an error on the call to submit changes. 我在提交更改的呼叫中遇到错误。

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

I've tried a number of permutations, eg: 我尝试了多种排列,例如:

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

and using DbGenerated annotations on the EF table object, but still hitting the same wall. 并在EF表格对象上使用DbGenerated批注,但仍然碰到了同一堵墙。

Any suggestions greatly appreciated. 任何建议,不胜感激。

Cheers, Andy 干杯,安迪

UPDATE 1: 更新1:

Here are my configuration settings (set up as per https://dev.mysql.com/doc/connector-net/en/connector-net-entityframework60.html ) 这是我的配置设置(根据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>

UPDATE 2: 更新2:

I read on another site that i may be able to use the following code to get overcome this. 我在另一个网站上读到,我也许可以使用以下代码来克服这一问题。

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

This code succeeds in inserting a new row with incremented id into the database, but the return value from the select is always 1. 此代码成功将ID递增的新行插入数据库,但是select的返回值始终为1。

I'm sure this must be something obvious I'm doing wrong. 我确信这一定是我做错了的明显事情。

You shoul configure your EntityFramework to work with MySql and then it will use LAST_INSERT_ID() instead of score_identity() . 您应该将EntityFramework配置为与MySql一起使用,然后它将使用LAST_INSERT_ID()而不是score_identity() Have a look at the config file 看看配置文件

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

If you really need this you can achieve your goal with help of this syntax. 如果您确实需要这样做,则可以借助此语法来实现您的目标。 Try this and modify it according to your needs: 尝试并根据您的需要对其进行修改:

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)
);

Try this. 尝试这个。 I hope you will understand, 希望你能理解

You shoul configure your EntityFramework to work with MySql and Modify the sode below according to your demand 您应该将EntityFramework配置为与MySql一起使用,并根据需要修改下面的sode

Syntax for MySQL The following SQL statement defines the "ID" column to be an auto-increment primary key field in the "Persons" table: MySQL的语法以下SQL语句将“ ID”列定义为“人”表中的自动增量主键字段:

CREATE TABLE Persons ( ID int NOT NULL AUTO_INCREMENT, LastName varchar(255) NOT NULL, FirstName varchar(255), Address varchar(255), City varchar(255), PRIMARY KEY (ID) ) 创建表人员(ID int NOT NULL AUTO_INCREMENT,LastName varchar(255)NOT NULL,Firstname varchar(255),Address varchar(255),City varchar(255),PRIMARY KEY(ID))

I think i may have found an answer (but no satisfactory solution yet). 我想我可能已经找到了答案(但是还没有令人满意的解决方案)。

I'm using Linq to SQL (not Linq to Entities), and it appears there is no out of the box support for this in MySql.Data etc. 我正在使用Linq to SQL(而不是Linq to Entities),并且在MySql.Data等中似乎没有开箱即用的支持。

I've started playing with the Depart linqExpress solution, but on initial glance it appears I would have to re-write substantial portions of code to make this work. 我已经开始使用Depart linqExpress解决方案,但是乍一看,看来我必须重新编写大部分代码才能完成此工作。

Unless anyone has another solution. 除非有人有其他解决方案。

Cheers. 干杯。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM