gpt4 book ai didi

mysql - 如何使用 Entity Framework 1 和 MySQL 更新多对多相关对象?

转载 作者:行者123 更新时间:2023-11-29 02:07:22 24 4
gpt4 key购买 nike

我有这样的问题。我将 EF1 与 VS2008 SP1 和 MySQL 与 MySQL Connector/Net 6.3.4 一起使用

我的数据库架构如下所示:

CREATE TABLE IF NOT EXISTS `credential` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(50) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=3 ;

CREATE TABLE IF NOT EXISTS `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(50) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=3 ;

CREATE TABLE IF NOT EXISTS `user_credential` (
`user_id` int(11) NOT NULL,
`credential_id` int(11) NOT NULL,
KEY `credential_id` (`credential_id`),
KEY `user_id` (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

ALTER TABLE `user_credential`
ADD CONSTRAINT `user_credential_ibfk_2` FOREIGN KEY (`credential_id`) REFERENCES `credential` (`id`) ON DELETE CASCADE,
ADD CONSTRAINT `user_credential_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `user` (`id`) ON DELETE CASCADE;

当我尝试执行以下代码时,我遇到了无法理解的异常

var entities = new studyEntities();

var user = new User { Name = "test" };
var credential = new Credential { Name = "admin" };

entities.AddToCredentialSet(credential);
entities.AddToUserSet(user);

entities.SaveChanges();
user.Credentials.Add(credential);
entities.SaveChanges(); // He I have a strange exception thrown


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 '(SELECT
`user_credential`.`credential_id`,
`user_credential`.`user_' at line 1

这是什么意思?我怎样才能看到整个查询来寻找其中的问题?或者我做错了什么?

最佳答案

我有同样的错误,因为我在中间表中没有主键。

这为我解决了问题:

CREATE TABLE IF NOT EXISTS `user_credential` (
`user_id` int(11) NOT NULL,
`credential_id` int(11) NOT NULL,

PRIMARY KEY(`user_id`, `credential_id`)
KEY `user_id` (`user_id`)

CONSTRAINT .... FOREIGN KEY....
CONSTRAINT .... FOREIGN KEY....

) ENGINE=InnoDB DEFAULT CHARSET=utf8;

关于mysql - 如何使用 Entity Framework 1 和 MySQL 更新多对多相关对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3643735/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com