[英]Entity framework generating wrong SQL
我正在尝试通过Entity Framework插入一条记录,并且看来生成的SQL不正确。 我要插入“ PostingSelections”表。 该表具有FK到其他三个表'PropertyPosting','PropertySummary'和'User'。 用户的FK为空。 这是使用MySQL数据库,我怀疑这是问题所在。
PostingSelections表的DDL为:
CREATE TABLE `PostingSelections`(
`Id` int NOT NULL AUTO_INCREMENT UNIQUE,
`SelectionType` TINYINT UNSIGNED NOT NULL,
`Date` datetime NOT NULL,
`SourceIPAddress` longtext NOT NULL,
`PropertyPosting_Id` int NOT NULL,
`PropertySummary_Id` int NOT NULL,
`User_Id` int);
我的代码是:
var postingselection = new postingselection();
postingselection.PropertyPosting_Id = 24;
postingselection.PropertySummary_Id = 24;
postingselection.SelectionType = 1;
postingselection.Date = DateTime.UtcNow;
postingselection.SourceIPAddress = "TBD";
db.postingselections.Add(postingselection);
db.SaveChanges();
该代码与当我自动生成控制器时由entityframework自动生成的代码完全相同。 EF生成的SQL是:
INSERT INTO `postingselections`(
`SelectionType`,
`Date`,
`SourceIPAddress`,
`PropertyPosting_Id`,
`PropertySummary_Id`,
`User_Id`,
`propertyposting_Id`,
`propertysummary_Id`,
`user_Id`) VALUES (
1,
4/18/2017,
TBD,
24,
24,
0,
24,
24,
NULL);
SELECT
`Id`
FROM `postingselections`
WHERE row_count() > 0 AND `Id`=last_insert_id()
看看它是如何两次生成三列的? PropertyPosting_Id,PropertyPosting_Id和User_Id? 按预期生成的错误是:
在71毫秒内失败,并出现以下错误:列“ User_Id”指定了两次
有人可以帮忙吗? 这是一个非常基本的插入内容,我找不到其他类似的帖子。 我只能得出结论,我已经在EF中找到了mySQL的错误,因此必须使用手动SQL语句来完成。
几天前,我遇到了类似的问题,我认为它与“ PostingSelections”表的主键有关。
尝试为您的表指定一个PK,否则EF不会区分记录。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.