Reputation: 563
I am using the entity framework for a webapp I am working on. The web app keeps track of venues. Each venue can host several types of events. I have a table with all the different type of event types (EventTypes) and another table that keeps track of which events each venue can have (VenueEventTypes), when I am adding a new venue I create a new VenueEventTypes collection which contains an EventType Propery and go out to the database and find the event type and add it to each of the VenueEventTypes. The problem is when I save the venue it creates everything fine but it is adding an empty record in the database for the EventTypes, but the VenueEventType foreign key is point to the valid event type and not the newly create empty one. Here is some code:
public void addVenue(VEN_Venues venue)
{
using (var db = new VenueEntities())
{
var parish = from dbParish in db.VEN_Parishes
where dbParish.ParishID == venue.ParishID
select dbParish;
venue.VEN_Parishes = parish.First();
venue = this.processEventTypes(venue, db);
db.VEN_Venues.AddObject(venue);
db.SaveChanges();
}
}
private VEN_Venues processEventTypes(VEN_Venues venue, VenueEntities db)
{
foreach (VEN_VenueEventTypes eventType in venue.VEN_VenueEventTypes)
{
eventType.VEN_EventTypes = new EventTypeFacade().findEventTypes(eventType.VEN_EventTypes, db);
}
return venue;
}
Could someone point me to the error?
Thanks!
Upvotes: 1
Views: 1273
Reputation: 5573
It doesn't seem like you are actually adding the VEN_EventType
to the venue.VEN_VenueEventTypes
.
You have to do something along these lines:
venue.VEN_VenueEventTypes.Add(eventType);
if you don't want any duplicates you will have to use attach:
venue.VEN_VenueEventTypes.Attach(eventType);
Upvotes: 1