2015-10-07 57 views
0

我有一个系统使用实体框架对运行良好的SQL Server数据库。最近我决定将MySql作为后端。该错误是由一些对SQLServer运行良好的代码造成的,但是对MySql失败。MySql实体框架auto_increment错误

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 

我已经尝试了数排列,e.g:

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

我敢肯定,这一定有什么明显我做错误。

+0

scope_idntity()是一个tsql函数,而不是一个mysql函数。你配置EF使用MySQL而不是MS SQL? – Shadow

+0

谈到MySql时,我是一个相对的noob。我在连接字符串上设置了Sql Server Mode开关。你是这个意思吗。 andy

回答

0

你可以配置你的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> 
+0

这里是我的 - 看起来不错我的 andy

0

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

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

您的第一个创建声明看起来与我的相同,但不包括默认的1(这是我尝试的另一种排列方式)。我不明白在这方面第二次创建声明应该做什么 - 你可以进一步解释一下吗? – andy

0

你建议立即进行删除配置的EntityFramework与MySQL 工作,请根据您的需求

语法的MySQL 下面的SQL语句定义的“ID”列是以下的sode在 “人” 表自动增量的主键字段:

CREATE TABLE人员 ( ID INT NOT NULL AUTO_INCREMENT, 名字VARCHAR(255)NOT NULL, 姓VARCHAR(255), 地址VARCHAR(255 ), City varchar(255), PRIMARY KEY(ID) )

+0

这就是我所拥有的 - 请参阅上面的问题:create table tblCompanySequence( Id int auto_increment not null, primary key(Id) ); – andy

0

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

我使用LINQ到SQL(不LINQ到实体),而且似乎也没有现成的支持该在MySql.Data等

我已经开始与出发linqExpress打解决方案,但初看起来,我将不得不重写大量代码才能完成这项工作。

除非任何人有另一种解决方案。

干杯。