Click here to Skip to main content
15,877,103 members

Comments by je-wo (Top 8 by date)

je-wo 10-Mar-15 8:17am View    
There is the Entity Model class:
There are Clients (aka the Streets) and ContactPersons (aka the houses)

--- Maciej Los --
Content has been moved to the question
je-wo 10-Mar-15 7:26am View    
I do have one table called "House" and one table that is called "Street".
"Street" contains street information and "House" contains house information.
"Street" does have an association to "House" by 0...many relationship. So, each Street object contains a list of house objects...even if it is empty.

We now create one or more House object(s) and store it in the DB. Afterwards
we create one or more Street object(s) and store it in the DB
For example:

House h1 = new House();

House h2 = new House();

Street s1 = new Street();

Then...a while later we need to tell the street, which already created house objects (stored in the DB table "House") belongs to it. Therefore we need to connect already created House objects to one Street Object.

My idea was, to Add() all House objects to the List of houses in the Street.
So, I called:

ctx.Streets.Where (x=>x.ID == 5).First().Houses.Add(h1);
ctx.Streets.Where (x=>x.ID == 5).First().Houses.Add(h2);

But instead of the expected connection, it created two new house objects in the "House" table.

The two "old" house objects are still there but are not connected to any other object. Two "new" house objects are also there AND connected to the Street.
But I do not want objects to get copied, I want the original house objects to be connected to the Street...

Thank you very much in advance...
je-wo 10-Mar-15 6:51am View    
Yes, that is exactly the relationship. But I do have the case that a Street object gets created with no houses attached to it. And one or more house objects get created without knowledge of a street.

Now, after both types of objects are available, I`d like to connect them. So, I`d like to add a house to a street, but both objects already exist in the database.

The code looks like this:

House h1 = new House();

Street s1 = new Street();

so far, so good. Now, I`d like to do the connection between the extisting House and Street objects:

// adds h1 to the strees with the ID no. 5:
ctx.Streets.Where (x=>x.ID == 5).First().Houses.Add(h1);

But with this code, the EF thinks,I`d like to create a new house object in the DB and so it does. Why doesn`t it connect the already created and existing h1 house to the street with the id no. 5? And how to change this behaviour...or is my thinking wrong and I need to change my approach?

Problem is, that all the houses gets created at a time I do know nothing about the street they will get added to, later on. So, I need to have the possiblity to add houses to the collection of houses in a street at a later time.

Hope this was explained in a way that it is understandable...
je-wo 10-Mar-15 6:22am View    
Tried it with

existingHouse.Street = existingStreet;

But the result is the same, I get another "new" existingStreet object created in the database with a new id. Everything else is an exact copy of the "original" existing street.
je-wo 10-Mar-15 4:38am View    
but where is the difference between

<pre>existingHouse.Street = existingStreet;</pre>


<pre>existingStreet.Houses = existingHouse;</pre>

how do I know which direction is the right one? It should work in both directions, shoudln`t it?