h1

LINQ To SQL requires Primary Key!

March 6, 2008

After 8 months of playing with LINQ I have only JUST found out that LINQ To SQL *Requires* a primary key on every table. This includes many to many mapping tables!

I only found this out by accident after forgetting to mark my ID column as PK. I guess this means that my good practice of putting a Primary key on every table is a good one! =]

Advertisements

4 comments

  1. hi.

    how to requires Primary Key?


  2. The primary key value will be in that property after the SubmitChanges()??

    but my code didn’t work


  3. Hi CSS,

    Yes you need to call SubmitChanges for the primary key property to get populated. If you want, email me your code and I can take a look.


  4. hi.
    robert112s2k.

    thanks you. it is done.
    ===========================
    old code
    ===========================
    db.SubCategoryValueRelation.InsertOnSubmit(subCategoryValueRelation.ToDataAccess());
    db.SubmitChanges();
    return subCategoryValueRelation.SCVRID;

    =================================
    new code
    ================================= DataAccess.SubCategoryValueRelation s =
    subCategoryValueRelation.ToDataAccess();
    db.SubCategoryValueRelation.InsertOnSubmit(s);
    db.SubmitChanges();
    return s.SCVRID;



Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: