c# 将新对象添加到对象上下文返回“更新条目时发生错误”,主键重复条目

发布于 2024-07-30 01:11:35 字数 1602 浏览 5 评论 0原文

我在尝试将对象添加到实体框架中的对象上下文时遇到问题。

代码如下:

//related object ids are retrieved from another context.
//since I thought the problem could be linked to object loaded in this context, I tried to
//close it and start on a fresh object context... 
int idetab = currentEtab.Id;
long idnivcycle = selectedNivcycle.id_nivcycle;
long idanscol = AnneeScolaireEnCours.id_annee_scolaire;
//and this context is closed
context.Dispose();


//This is the new object context
objectcontextEntities ctest = new objectcontextEntities();
//from where I retrieve all the objects I want to link to the object I want to create :            
Nivcycle niv = ctest.Nivcycles.Where("it.id_nivcycle=@id", new ObjectParameter("id", idnivcycle)).First();
Etablissement eta = ctest.Etablissements.Where("it.Id=@id", new ObjectParameter("id", Convert.ToInt32(idetab))).First();
annee_scolaire an = ctest.annee_scolaire.Where("it.en_cours = @id", new ObjectParameter("id", Convert.ToSByte(true))).First();
// Now I try to create my new object           
Composante co = new Composante();
co.Nom = "test";
co.Nivcycle = niv;
co.Etablissement = eta;
co.AnneeScolDeb = an;
ctest.AddToComposantes(co);
ctest.SaveChanges();
ctest.Dispose();

在 SaveChanges() 上,我收到一个 UpdateException,表示键“PRIMARY”有重复条目“0”。 确实,我的“Composantes”中已经有一个 id=0 的对象。 如果我删除它,我至少可以创建一个ID=0的对象。 我不明白的是,当一个新对象添加到上下文中时,为什么它不采用下一个可用的 ID??? 这与我尝试在新上下文中创建此对象之前发生的错误完全相同......这可能是因为我的 model.edmx 中缺少参数? 有没有办法强制将新 id 归因于新创建的对象?

我使用实体框架模型连接到 mysql 数据库,使用 Devart dot.connector 连接到数据库。 预先感谢大家对这个简单问题的答复。

皮埃尔

I'm having a problem trying to add an object to an object context in Entity Framework.

Here is the code :

//related object ids are retrieved from another context.
//since I thought the problem could be linked to object loaded in this context, I tried to
//close it and start on a fresh object context... 
int idetab = currentEtab.Id;
long idnivcycle = selectedNivcycle.id_nivcycle;
long idanscol = AnneeScolaireEnCours.id_annee_scolaire;
//and this context is closed
context.Dispose();


//This is the new object context
objectcontextEntities ctest = new objectcontextEntities();
//from where I retrieve all the objects I want to link to the object I want to create :            
Nivcycle niv = ctest.Nivcycles.Where("it.id_nivcycle=@id", new ObjectParameter("id", idnivcycle)).First();
Etablissement eta = ctest.Etablissements.Where("it.Id=@id", new ObjectParameter("id", Convert.ToInt32(idetab))).First();
annee_scolaire an = ctest.annee_scolaire.Where("it.en_cours = @id", new ObjectParameter("id", Convert.ToSByte(true))).First();
// Now I try to create my new object           
Composante co = new Composante();
co.Nom = "test";
co.Nivcycle = niv;
co.Etablissement = eta;
co.AnneeScolDeb = an;
ctest.AddToComposantes(co);
ctest.SaveChanges();
ctest.Dispose();

On SaveChanges(), I receive an UpdateException, saying that there is a duplicate entry '0' for Key 'PRIMARY'.
It is true that I already have an object in my "Composantes" with id=0. And if I delete it, I can at least create one object with ID=0. What I don't understand is that when a new object is added to the context, why doesn't it take the next available ID ???? This is exactly the same error that was happening before I tried to create this object in a new context.... This is maybe because of a missing paramter in my model.edmx ? Is there a way somewhere to force the attribution of a new id to the newly created object ?

I'm connecting to the mysql database using an entity Framework Model using Devart dot.connector to connect to the database.
Thanking all of you in advance for your reply to this simple question.

Pierre

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凶凌 2024-08-06 01:11:35

我自己找到了解决方案。 对于陷入这种愚蠢事情的人来说可能很有用......
这只是因为在数据库本身中,ID 字段没有设置为 AUTOINCRMENT...
更改此设置并更新 model.edmx 解决了问题。
抱歉打扰您了!!!!

I found the solution myself. It may be usefull for someone stuck on this stupid thing...
It is just because in the database iteself the ID field was not set to AUTOINCREMENT...
Changing this and updating the model.edmx solved the problem.
Sorry for disturbing you with this !!!!!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文