Set Foreign Key Value

2 posts, 0 answers
  1. Grant Drury-Green
    Grant Drury-Green avatar
    23 posts
    Member since:
    Jul 2009

    Posted 06 Feb 2012 Link to this post


    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

  2. PetarP
    PetarP avatar
    754 posts

    Posted 09 Feb 2012 Link to this post

    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.

    the Telerik team
    Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
Back to Top