I am attempting to insert a record through Entity Framework and it appears the SQL generated is incorrect. I am inserting into the 'PostingSelections' table. This table has FK's to three other tables 'PropertyPosting', 'PropertySummary' and 'User'. The FK to User is nullable. This is using a MySQL database, which I am suspicious is the problem.
The DDL for the PostingSelections table is:
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);
My code is:
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();
The code is exactly the same as what is automatically generated by entityframework when I generate the controller automatically. The SQL that EF is generating is:
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()
See how it has generated three columns twice? PropertyPosting_Id, PropertyPosting_Id and User_Id? The error that is generated as expected is:
Failed in 71 ms with error: Column 'User_Id' specified twice
Could someone please help? This is a very basic insert, and I can't find any other similar postings. I can only conclude I have found a bug in EF for mySQL and will have to do this instead with a manual SQL statement.