首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MySql实体框架auto_increment错误

MySql实体框架auto_increment错误
EN

Stack Overflow用户
提问于 2015-10-07 11:05:00
回答 4查看 2.6K关注 0票数 0

我有一个系统,它对运行良好的SQL Server db使用实体框架。最近,我决定搬到MySql作为后端。该错误是由一些代码导致的,这些代码对SQLServer运行非常好,但对MySql却失败。

MySql 5.6.27,EF6.

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

这是表def:

代码语言:javascript
复制
create table tblCompanySequence (
    Id int auto_increment primary key not null default 1
);

下面是相应的c# def:

代码语言:javascript
复制
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; }
    }
}

这是密码:

代码语言:javascript
复制
var newSeq = new EFCompanySequence();
var tableSeq = context.GetTable<EFCompanySequence>();
tableSeq.InsertOnSubmit(newSeq);
context.SubmitChanges();
var newId = newSeq.Id;

在调用提交更改时,我会收到一个错误。

代码语言:javascript
复制
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

我试过一些排列,例如:

代码语言:javascript
复制
create table tblCompanySequence (
    Id int auto_increment not null,
    primary key (Id)
);

并在EF表对象上使用DbGenerated注释,但仍然碰到相同的墙。

任何建议都非常感谢。

干杯,安迪

更新1:

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

代码语言:javascript
复制
<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:

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

代码语言:javascript
复制
var newId = context.ExecuteCommand("insert into tblCompanySequence values (null); select LAST_INSERT_ID();");

此代码成功地向数据库插入了一个新行,该行的id递增,但select的返回值始终为1。

我肯定这肯定是我做错了什么明显的事情。

EN

回答 4

Stack Overflow用户

发布于 2015-10-07 11:24:13

您应该将您的EntityFramework配置为使用MySql,然后它将使用LAST_INSERT_ID()而不是score_identity()。查看配置文件

代码语言:javascript
复制
  <connectionStrings>
<add name="DefaultConnection"
providerName="MySql.Data.MySqlClient"
connectionString="[Insert your ConnectionString to the mysql database here]"/>
</connectionStrings>
票数 0
EN

Stack Overflow用户

发布于 2015-10-07 11:35:35

如果你真的需要这个,你可以通过这个语法来达到你的目标。试一试,并根据您的需要修改:

代码语言:javascript
复制
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)
);

尝尝这个。我希望你能理解

票数 0
EN

Stack Overflow用户

发布于 2015-10-07 11:58:09

您应该配置您的EntityFramework以使用MySql,并根据您的需求修改下面的sode

MySQL的语法以下SQL语句将"ID“列定义为”Person“表中的自动增量主键字段:

创建表人员( ID int NULL AUTO_INCREMENT,LastName varchar(255) NULL,FirstName varchar(255),Address varchar(255),City varchar(255),主键(ID) )

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32990568

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档