This is a migrated thread and some comments may be shown as answers.

Set Foreign Key Value

1 Answer 42 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Grant Drury-Green
Top achievements
Rank 1
Grant Drury-Green asked on 07 Feb 2012, 05:15 AM
Hi,

I'm working with an database first object model - and I'm facing an issue which I can only assume is something strange with my set-up.

Basically, I'm trying to add a new object to the database - the table for which has some foreign keys like thus:

public class Project : Group
{
     public int CreatedByUserId {get; set;}
     public User CreatedByUser {get; set;}
  
     public string CountryCode {get; set;}
     public Country Country {get; set;}

      // more properties...
}

Rather than getting an instance of the User and the Country from the database, I'm setting just the CreatedByUserId and CountryCode properties (I know what these values are)

Unfortunately, when I set just the value properties - I get an error:

Insert of '235936052-' failed: Telerik.OpenAccess.RT.sql.SQLException:
Cannot insert the value NULL into column
'CountryCode', table 'Alumni.dbo.Group';
column does not allow nulls. INSERT fails.
The statement has been terminated.

CountryCode has definitely been populated. Also, if I change the code to set just the Country and CreatedByUser properties - all is good...

There's two possible things I can think of:
  1. OpenAccess is getting confused as the Project class is derived from Group (using flat hierarchy)
  2. OpenAccess doesn't support simply setting the foreign key value (I'm hoping this isn't the case - as I REALLY don't want to have to re-retrieve the user each time I create an object!)

Any help greatly appreciated

1 Answer, 1 is accepted

Sort by
0
PetarP
Telerik team
answered on 09 Feb 2012, 07:13 PM
Hi Grant Drury-Green,

 Can you please share with us the mapping of the association that is failing for you. To obtain the mapping you will need to open your rlinq file with an XML editor and look for the association in question.
We are looking forward to your reply.

Greetings,
Petar
the Telerik team
Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
Tags
General Discussions
Asked by
Grant Drury-Green
Top achievements
Rank 1
Answers by
PetarP
Telerik team
Share this question
or