不能用密钥已在使用添加的实体 [英] Cannot add an entity with a key that is already in use
问题描述
我有一个有点麻烦试图对象添加到使用LINQ to SQL数据库。
I'm having a bit of trouble trying to add an object to the database using LINQ to SQL.
我有三个表在我的数据库:
I have three tables in my database:
--------------------------------------------
tblShows
--------------------------------------------
ShowID | Name
--------------------------------------------
--------------------------------------------
tblShowEvents
--------------------------------------------
ShowEventID | ShowID | Name
--------------------------------------------
--------------------------------------------
tblShowEventAttendees
--------------------------------------------
ShowEventAttendeeID | ShowEventID | Name
--------------------------------------------
一个显示有许多事件,每个事件都有许多参加者。
A Show has many Events, and each Event has many Attendees.
我在那里我创建一个显示的页面。在页面上,我添加到事件显示,和与会者对每个事件,然后保存节目的一次。
I have a page where I'm creating a Show. On the page, I'm adding Events to the Show, and Attendees to each Event, and then saving the show all at once.
下面是我的code:
Show show = new Show();
show.Name = txtName.Text.Trim();
//add ShowEvents to Show
//showEventsList is a variable containing a List of ShowEvents
foreach (ShowEvent ev in showEventsList)
{
show.ShowEvents.Add(new ShowEvent
{
ShowID = show.ShowID,
Name = ev.Name
});
}
//Add ShowEventAttendees to each ShowEvent
//attendeesList is a variable containing a List of ShowEventAttendees
foreach (ShowEvent ev in show.ShowEvents)
{
foreach (ShowEventAttendee attendee in attendeesList)
{
ev.ShowEventAttendees.Add(new ShowEventAttendee
{
ShowEventID = ev.ShowEventID,
Name = attendee.Name
});
}
}
AppDataContext.DataContext.Shows.InsertOnSubmit(show);
AppDataContext.DataContext.SubmitChanges();
发生在第二个foreach循环的问题。如果我把它拿出来,展示和ShowEvents被添加到正确的数据库。
The issue occurs in the second foreach loop. If I take it out, the Show and ShowEvents get added to the database correctly.
什么是这里的问题?
编辑周杰伦:
我现在已经试过这样:
AppDataContext.DataContext.Shows.InsertOnSubmit(show);
AppDataContext.DataContext.SubmitChanges();
//Add ShowEventAttendees to each ShowEvent
//attendeesList is a variable containing a List of ShowEventAttendees
foreach (ShowEvent ev in show.ShowEvents)
{
foreach (ShowEventAttendee attendee in attendeesList)
{
ev.ShowEventAttendees.Add(new ShowEventAttendee
{
ShowEventID = ev.ShowEventID,
Name = attendee.Name
});
}
}
AppDataContext.DataContext.SubmitChanges(); //trying to update, error here
但我得到最后一行同样的错误。通过调试去,ShowEventID不再是0,但我得到了同样的错误。
But I get the same error on that last line. Going through the debugger, ShowEventID is no longer 0, but I'm getting the same error.
推荐答案
当你调用 ShowEventID = ev.ShowEventID
,它始终是 0
,因为没有身份证被分配。
When you call ShowEventID = ev.ShowEventID
, it is always 0
because no ID has been assigned.
因为你可能正在使用标识字段作为主键标识符,你必须保存节目(其ShowEvents),以具有分配给那些对这些ShowEvents标识。
Since you are probably using identity fields as your primary key identifiers, you'll have to save the show (with its ShowEvents) in order to have IDs assigned to those on those ShowEvents.
此后,第二环路将工作
另外,如果你保存在 ShowEventAttendee
实际 ShowEvent
对象的引用(一个在LINQ-协会到-SQL)而不是使用完整的标识,你会打电话 ShowEvent = EV
,你可以立刻节省一切。
Alternatively, if you store a reference to the actual ShowEvent
object in ShowEventAttendee
, (an association in LINQ-to-SQL) instead of using the integral identifier, you would call ShowEvent = ev
and you could save everything at once.
东西是关闭的。每当你循环要添加与会者同样 ShowEvent
,堪称一个局部变量 showEvent
。我会承担的意图这里每个 ShowEvents
的显示
要添加与会者,所以应该 showEvent
与 EV
?
Something is off. Every time you loop you are adding attendees to the same ShowEvent
, a local variable called showEvent
. I would have assumed the intent here to be adding attendees for each of the ShowEvents
of show
, so should showEvent
be replaced with ev
?
这篇关于不能用密钥已在使用添加的实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!