15,881,882 members
Sign in
Sign in
Email
Password
Forgot your password?
Sign in with
home
articles
Browse Topics
>
Latest Articles
Top Articles
Posting/Update Guidelines
Article Help Forum
Submit an article or tip
Import GitHub Project
Import your Blog
quick answers
Q&A
Ask a Question
View Unanswered Questions
View All Questions
View C# questions
View C++ questions
View Javascript questions
View Visual Basic questions
View Python questions
discussions
forums
CodeProject.AI Server
All Message Boards...
Application Lifecycle
>
Running a Business
Sales / Marketing
Collaboration / Beta Testing
Work Issues
Design and Architecture
Artificial Intelligence
ASP.NET
JavaScript
Internet of Things
C / C++ / MFC
>
ATL / WTL / STL
Managed C++/CLI
C#
Free Tools
Objective-C and Swift
Database
Hardware & Devices
>
System Admin
Hosting and Servers
Java
Linux Programming
Python
.NET (Core and Framework)
Android
iOS
Mobile
WPF
Visual Basic
Web Development
Site Bugs / Suggestions
Spam and Abuse Watch
features
features
Competitions
News
The Insider Newsletter
The Daily Build Newsletter
Newsletter archive
Surveys
CodeProject Stuff
community
lounge
Who's Who
Most Valuable Professionals
The Lounge
The CodeProject Blog
Where I Am: Member Photos
The Insider News
The Weird & The Wonderful
help
?
What is 'CodeProject'?
General FAQ
Ask a Question
Bugs and Suggestions
Article Help Forum
About Us
Search within:
Articles
Quick Answers
Messages
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();
ctx.Houses.Add(h1);
House h2 = new House();
ctx.Houses.Add(h2);
Street s1 = new Street();
ctx.Streets.Add(s1);
ctx.SaveChanges();
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);
etc.
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();
ctx.Houses.Add(h1);
Street s1 = new Street();
ctx.Streets.Add(s1);
ctx.SaveChanges();
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>
and
<pre>existingStreet.Houses = existingHouse;</pre>
how do I know which direction is the right one? It should work in both directions, shoudln`t it?
je-wo
20-Oct-14 9:12am
View
From my points of view, this is exactly what I do.
Bset does have a an object which is List automatically created by the entity framework. The problem is, that even if the Bset does have 5x objects of type Aset in the collection, after the Update in the DB, it doesn`t update the 5 existing Asets (which I would like to have). It updates the Bset, but creates 5 new Aset`s. So, after an update of Bset, I do have 10 Asets in the DB. 5x the old objects, but 5x new Aset objects.
How can I make sure that the original 5 Aset objects get used for an update, instead of creating new objects?
je-wo
11-Aug-13 13:47pm
View
Thank you very much, that helped a lot. So I do not need to invest any more time in research.
je-wo
9-Aug-13 8:40am
View
You mean LINQ?
Show More