Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them, it only takes a minute:

In this method, I am inserting a new item (Room) into the database. That process functions as expected.

But, in addition to that, each time I add a room, I want to add a piece of furniture as the initial piece. Each item of type Furniture has a "RoomID" to designate its location. Thus, Room contains a collection of Furniture. Below, I am the piece of "primary" furniture from the database, adding it to the room's furniture collection, and submitting changes. The room gets added to the database, but the Furniture.RoomID column remains as null.

public void AddResidentToUniverse(int residentID, int universeID)
{
    Universe uni = _context.Universes.FirstOrDefault(u => u.UniverseID == universeID);
    Resident res = _context.Residents.FirstOrDefault(r=>r.ResidentID == residentID);

    if (uni != null && res!=null)
    {
        Room e = new Room();
        Furniture primary = _context.Furnitures.FirstOrDefault(p => p.FurnitureID == new FurnitureController().GetPrimary(universeID).FurnitureID);

        e.UniverseID = uni.UniverseID;
        e.RoomName = res.RootName;
        e.ResidentID = residentID;
        e.Expired = null;
        e.Furniture.Add(primary);
        uni.Rooms.Add(e);

        _context.SubmitChanges();
    }
}
share|improve this question
    
how about save the room first. and then add the furniture? – cpoDesign Jun 6 '13 at 14:14
    
Sure. I tried that. Instead of the e.Furniture.Add(primary); line, after the SubmitChanges(), I added Furniture Primary = _context.Furnitures.FirstOrDefault(p => p.FurnitureID == new FurnitureController().GetPrimary(universeID).FurnitureID); Primary.RoomID = e.RoomID; _context.SubmitChanges(); Not sure if the second SubmitChanges() is permissible – Joe Jun 6 '13 at 15:30

2 Answers 2

up vote 1 down vote accepted

You need to add a line that tells your database what you want to insert. For example,

uni.Rooms.InsertOnSubmit(Room object);
uni.Furniture.InsertOnSubmit(furniture piece);

after this, you can write your

uni.SubmitChanges();

line.

share|improve this answer
    
The furniture piece already exists. Only the room is new. I am adding the existing piece to the new room. InsertOnSubmit on something that exist causes a 'Cannot add an entity that already exists.' – Joe Jun 6 '13 at 18:03
    
Are you able to add the ID to the Room's Furniture Piece and then insert the Room using the InsertOnSubmit line? What I mean is, Can you set e.FurnitureID = primary.ID? You need to make sure that you are adding the correct Foreign Key relationship to your Room object. – CSadosky Jun 6 '13 at 18:48
    
I am not following. The room (e) does not have a furniture id, it has a collection of Furniture. – Joe Jun 6 '13 at 21:19
    
How are you storing the collection in the database (specifically how is the List<Furinture> (or whatever container you are using) being stored as an attribute of the Room in the database's "Room" table)? – CSadosky Jun 7 '13 at 18:43
    
When I have done this in the past, I would have a column in the Furniture table that is called RoomID and would have a foreign key relationship to the room it is in. Then, when a Room object is created, another Furniture object is added to the database, and the Room's ID is assigned to the Furniture object. Then to retrieve, call a foreach on the Furniture table and search where(x => x.RoomID = Room.ID). – CSadosky Jun 7 '13 at 18:45

I finally bit the bullet and erase my dbml, dropped and recreated the tables, and recreated my dbml. The Furniture.RoomID column updates correctly now. A totally unsatisfying, ham-handed and brute-force approach, I know.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.