简体   繁体   English

实体框架(EF6)+ MySql数据库优先模型多对多关系错误查询生成

[英]Entity Framework (EF6) + MySql Database First Model Many to Many Relationship Wrong Query Generation

I have a project which has an existing database structure and data. 我有一个具有现有数据库结构和数据的项目。 Currently project is running on PHP + MySQL and I'm trying to switch from PHP to C# EF 6.0 + MySQL Database first approach. 当前项目正在PHP + MySQL上运行,我正在尝试从PHP切换到C#EF 6.0 + MySQL Database第一种方法。

I have installed NuGet Packages (EF 6.0 and MySQL.Data, MySQL.Web, MySQL.Data.Entity, MySQL.Data.Entities) and modified my app.config according to use MySQL data provider. 我已经安装了NuGet软件包(EF 6.0和MySQL.Data,MySQL.Web,MySQL.Data.Entity,MySQL.Data.Entities),并根据使用MySQL数据提供程序修改了app.config。 Up to this state everything works perfectly, I can generate model from database and can insert data on each table without any issue but many-to-many relation tables. 到此状态,一切都可以正常运行,我可以从数据库生成模型,并且可以在每个表上插入数据而没有任何问题,但多对多关系表。 I have created 2 dummy tables with name TableA and TableB and the relation table TableA_TableB for requesting help about my issue. 我创建了2个虚拟表,名称分别为TableA和TableB以及关系表TableA_TableB,以请求有关我的问题的帮助。

You can see the screen shots of the model generation below: 您可以在下面看到模型生成的屏幕截图: 在此处输入图片说明

And the generated model below: 以及生成的模型如下: 在此处输入图片说明

Up to here everything looks fine but as I mentioned when I try to insert a relation between TableA and TableB it fails. 到目前为止,一切看起来都不错,但是正如我在尝试在TableA和TableB之间插入关系时提到的那样,它失败了。 You can see my C# code below: 您可以在下面看到我的C#代码:

public static void Create()
{
    using (MainDataContext mainDataContext = new MainDataContext())
    {
        try {
            mainDataContext.Database.Log = s => System.Diagnostics.Debug.WriteLine(s);
            TableA tableA = new TableA();
            tableA.Title = "My Title";
            tableA.Description = "MyDescription";
            mainDataContext.TableA.Add(tableA);
            mainDataContext.SaveChanges();

            TableB tableB = new TableB();
            tableB.Title = "Table B Title";
            tableB.Description = "Table B Description";
            mainDataContext.TableB.Add(tableB);
            mainDataContext.SaveChanges();

            tableA.TableB.Add(tableB);
            mainDataContext.SaveChanges();
        }
        catch (Exception ex)
        {

        }
    }
}

The first two of the mainDataContext.SaveChanges(); mainDataContext.SaveChanges()的前两个; works fine but the last mainDataContext.SaveChanges(); 工作正常,但最后一个mainDataContext.SaveChanges(); throws exception (I tried the single transaction it fails too) about FK of my table but after I dig the debug output I faced the exception's real reason. 抛出关于表FK的异常(我也尝试过失败的单个事务),但是在挖掘调试输出后,我遇到了异常的真正原因。 I turn on debug output within my data context and here you can see the output of the insert statements of the EF you can see it below: 我在数据上下文中打开调试输出,在这里您可以看到EF的insert语句的输出,您可以在下面看到它:

Opened connection at 11.1.2016 23:30:46 +02:00 Started transaction at 11.1.2016 23:30:46 +02:00 在2016年1月1日23:30:46 +02:00打开连接在2016年1月11日23:30:46 +02:00开始交易

SET SESSION sql_mode='ANSI';INSERT INTO TableA ( Title , Description ) VALUES ( @gp1, @gp2); SET SESSION sql_mode ='ANSI';在TableA插入( TitleDescription )值(@ gp1,@ gp2); SELECT ID FROM TableA WHERE row_count() > 0 AND ID =last_insert_id() -- @gp1: 'My Title' (Type = String, IsNullable = false, Size = 8) -- @gp2: 'MyDescription' (Type = String, IsNullable = false, Size = 13) -- Executing at 11.1.2016 23:30:47 +02:00 -- Completed in 1 ms with result: EFMySqlDataReader TableA SELECT ID row_count()> 0 AND ID = last_insert_id()-@ gp1:'我的标题'(类型=字符串,IsNullable = false,大小= 8)-@ gp2:'MyDescription'(类型=字符串,IsNullable = false,Size = 13)-在11.1.2016 23:30:47 +02:00执行-在1毫秒内完成,结果为:EFMySqlDataReader

Committed transaction at 11.1.2016 23:30:47 +02:00 Closed connection at 11.1.2016 23:30:47 +02:00 Disposed transaction at 11.1.2016 23:30:47 +02:00 Opened connection at 11.1.2016 23:30:55 +02:00 Started transaction at 11.1.2016 23:30:55 +02:00 在11.1.2016 23:30:47 +02:00提交的交易在11.1.2016 23:30:47 +02:00的关闭的交易在2016年1月11日23:30:47 +02:00的已关闭的交易。 2016 23:30:55 +02:00在11.1.2016开始交易

SET SESSION sql_mode='ANSI';INSERT INTO TableB ( Title , Description ) VALUES ( @gp1, @gp2); SET SESSION sql_mode ='ANSI';将INERT INTO TableBTitleDescription )值(@ gp1,@ gp2); SELECT ID FROM TableB WHERE row_count() > 0 AND ID =last_insert_id() TableB选择ID row_count()> 0 AND ID = last_insert_id()

-- @gp1: 'Table B Title' (Type = String, IsNullable = false, Size = 13) -- @gp2: 'Table B Description' (Type = String, IsNullable = false, Size = 19) -- Executing at 11.1.2016 23:30:55 +02:00 -- Completed in 6 ms with result: EFMySqlDataReader -@ gp1:'表B标题'(类型=字符串,IsNullable =假,大小= 13)-@ gp2:'表B描述'(类型=字符串,IsNullable =假,大小= 19)-执行于11.1.2016 23:30:55 +02:00-在6毫秒内完成,结果为:EFMySqlDataReader

Committed transaction at 11.1.2016 23:30:55 +02:00 Closed connection at 11.1.2016 23:30:55 +02:00 Disposed transaction at 11.1.2016 23:30:55 +02:00 Opened connection at 11.1.2016 23:30:58 +02:00 Started transaction at 11.1.2016 23:30:58 +02:00 在11.1.2016 23:30:55 +02:00提交的交易在11.1.2016 23:30:55 +02:00的关闭的交易在2016年1月11日23:30:55 +02:00的已关闭的交易。 2016 23:30:58 +02:00在2016年1月1日开始交易

INSERT INTO (SELECT TableA_TableB . TableAID , TableA_TableB . TableBID FROM TableA_TableB AS TableA_TableB )( TableAID , TableBID ) VALUES ( 1, 1) INSERT INTO(SELECT TableA_TableBTableAIDTableA_TableBTableBID FROM TableA_TableB AS TableA_TableB )( TableAIDTableBID )VALUES(1,1)

-- Executing at 11.1.2016 23:30:58 +02:00 -在11.1.2016 23:30:58 +02:00执行

-- Failed in 3 ms with error: You have an error in your SQL syntax; -3毫秒内失败,并出现错误:您的SQL语法有错误; check the manual that corresponds to your MySQL server version for the right syntax to use near '(SELECT TableA_TableB . TableAID , TableA_TableB . TableBID FROM `TableA_Tab' at line 1 检查对应于你的MySQL服务器版本使用附近的正确语法手册“(SELECT TableA_TableBTableAIDTableA_TableBTableBID FROM`TableA_Tab”在1号线

Closed connection at 11.1.2016 23:30:58 +02:00 Disposed transaction at 11.1.2016 23:30:58 +02:00 Exception thrown: 'System.Data.Entity.Infrastructure.DbUpdateException' in EntityFramework.dll 在11.1.2016 23:30:58 +02:00处关闭的连接在11.1.2016 23:30:58 +02:00处已处置的事务抛出异常:EntityFramework.dll中的'System.Data.Entity.Infrastructure.DbUpdateException'

Also my table's DDLs below: 还有我表的以下DDL:

CREATE TABLE `TableA` (
  `ID` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `Title` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `Description` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  PRIMARY KEY (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

CREATE TABLE `TableB` (
  `ID` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `Title` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `Description` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  PRIMARY KEY (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

CREATE TABLE `TableA_TableB` (
  `TableAID` int(10) unsigned NOT NULL,
  `TableBID` int(10) unsigned NOT NULL,
  KEY `TableA_TableB_TableAID` (`TableAID`),
  KEY `TableA_TableB_TableBID` (`TableBID`),
  CONSTRAINT `TableA_TableB_TableAID` FOREIGN KEY (`TableAID`) REFERENCES `TableA` (`ID`) ON DELETE CASCADE ON UPDATE CASCADE,
  CONSTRAINT `TableA_TableB_TableBID` FOREIGN KEY (`TableBID`) REFERENCES `TableB` (`ID`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

I wonder if this is a bug or I'm doing something wrong does anyone have any idea for my issue ? 我想知道这是一个错误还是我做错了任何人对我的问题有任何想法吗?

Thanks in advance! 提前致谢!

The problem was about the missing PK on my relation table TableA_TableB after adding PK (TableAID,TableBID) the problem solved. 问题是关于添加问题解决的PK(TableAID,TableBID)之后我的关系表TableA_TableB缺少PK。 Additionally using uint on EF causes single save issue (you can save tables one by one and after getting ID's you can create relations) 此外,在EF上使用uint会导致一次保存问题(您可以一张一张地保存表,获得ID后可以创建关系)

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

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