Click here to Skip to main content
15,887,585 members
Home / Discussions / C#
   

C#

 
GeneralRe: How do i select the radiobutton depending on the info on my textbox? Pin
PIEBALDconsult12-Jul-09 18:20
mvePIEBALDconsult12-Jul-09 18:20 
AnswerRe: How do i select the radiobutton depending on the info on my textbox? Pin
Natza Mitzi12-Jul-09 18:20
Natza Mitzi12-Jul-09 18:20 
GeneralRe: How do i select the radiobutton depending on the info on my textbox? Pin
daffy_200312-Jul-09 18:53
daffy_200312-Jul-09 18:53 
QuestionIssues with adding a web reference using WSDL.exe Pin
Tina P12-Jul-09 13:14
Tina P12-Jul-09 13:14 
AnswerRe: Issues with adding a web reference using WSDL.exe Pin
Ravi Bhavnani12-Jul-09 15:11
professionalRavi Bhavnani12-Jul-09 15:11 
GeneralRe: Issues with adding a web reference using WSDL.exe Pin
Tina P12-Jul-09 15:51
Tina P12-Jul-09 15:51 
GeneralRe: Issues with adding a web reference using WSDL.exe Pin
Ravi Bhavnani12-Jul-09 18:27
professionalRavi Bhavnani12-Jul-09 18:27 
QuestionEntity Framework: Create new Object (when relation exists) PinPopular
TheShihan12-Jul-09 12:58
TheShihan12-Jul-09 12:58 
Hi,

I'm trying to figuring out how to add an object (insert a new row), when the object is related to another entity.

Eg.: I have a user entity, each user can have a title ("Mr.", "Ms.", "Dr.", etc.) or no Title assigned at all.
Entity Model

So you see, the user entity is mapped to the the title entity. Navigation properties exist also (directly generated from the wizard).

In the database, the tables look like this:
DB diagram

The user table has a column "title_id" which is the foreign key for the primary key of the table "titles".

My question is now, how do I add a new user object and directly associate it with an existing title.

I tried various ways, but I get different exceptions when attaching the user entity to the Context. Eg.:
{System.InvalidOperationException: The object cannot be added to the ObjectStateManager because it already has an EntityKey.}

or
"An object with the same key already exists in the ObjectStateManager. The existing object is in the Unchanged state. An object can only be added to the ObjectStateManager again if it is in the added state."


My create user method looks like this:
public int CreateUser(User user)
{
    this.context.AddToUsers(user);
    this.context.SaveChanges();

    return user.UserId;
}


... where I've previously assigned the desired "Title"-Entity to the passed User-Entity, eg:
user.Title = dataAccess.GetTitleByTitleId(titleId);


So, I guess the problem is in this part.. as this given Title-Entity modifies the User-Entity in a undesired way. I've also tried to get rid of the "Title"-navigation property and add the "TitleId" property direclty to the "User" entity, so I could simple assigne the desired "TitleId" to the user (user.TitleId = titleId;), but this gives me various validation errors in the entity model designer.

Does anybody know how to do this (simple task?) in a correct way?

Thanks, Shi
QuestionWIA with Vista... Pin
Jacob Dixon12-Jul-09 10:08
Jacob Dixon12-Jul-09 10:08 
AnswerRe: WIA with Vista... Pin
Jacob Dixon12-Jul-09 14:45
Jacob Dixon12-Jul-09 14:45 
AnswerRe: WIA with Vista... Pin
Jacob Dixon12-Jul-09 15:28
Jacob Dixon12-Jul-09 15:28 
GeneralRe: WIA with Vista... Pin
Sean Devoy26-Aug-09 14:44
Sean Devoy26-Aug-09 14:44 
GeneralRe: WIA with Vista... Pin
Jacob Dixon26-Aug-09 15:12
Jacob Dixon26-Aug-09 15:12 
QuestionImprove Speed of send message from server to all Clients Pin
Vu Nam Son12-Jul-09 7:34
Vu Nam Son12-Jul-09 7:34 
AnswerRe: Improve Speed of send message from server to all Clients Pin
Christian Graus12-Jul-09 7:36
protectorChristian Graus12-Jul-09 7:36 
AnswerRe: Improve Speed of send message from server to all Clients Pin
Behrooz_cs12-Jul-09 9:27
Behrooz_cs12-Jul-09 9:27 
QuestionHow can I intercept audio output? Pin
blametom12-Jul-09 7:01
blametom12-Jul-09 7:01 
AnswerRe: How can I intercept audio output? Pin
Christian Graus12-Jul-09 7:35
protectorChristian Graus12-Jul-09 7:35 
AnswerRe: How can I intercept audio output? Pin
Richard Andrew x6412-Jul-09 17:32
professionalRichard Andrew x6412-Jul-09 17:32 
GeneralRe: How can I intercept audio output? Pin
blametom13-Jul-09 6:19
blametom13-Jul-09 6:19 
QuestionRe: How can I intercept audio output? Pin
blametom13-Jul-09 8:27
blametom13-Jul-09 8:27 
NewsRe: How can I intercept audio output? Pin
blametom14-Jul-09 2:26
blametom14-Jul-09 2:26 
Questionconvert ascii bytearray to string Pin
samy10012-Jul-09 3:50
samy10012-Jul-09 3:50 
AnswerRe: convert ascii bytearray to string Pin
Eslam Afifi12-Jul-09 3:55
Eslam Afifi12-Jul-09 3:55 
GeneralRe: convert ascii bytearray to string Pin
samy10012-Jul-09 4:42
samy10012-Jul-09 4:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.