views:

63

answers:

1

Assume I have a two tables, A and B. Table A has a primary Key called A_ID of type int, and table B has a foreign key called A_ID.

When I add the two tables to a Linq To SQL data context class it correctly creates the classes and the association between them.

My question is, class B will have a property of type int called A_ID. It will also have property called A, of type A.

Now, if at runtime, I make a linq to sql call which populates an instance of B, B.A_ID and B.A.A_ID will be the same value. What is stopping a developer from making some mistake and putting a different value in B.A_ID than is in B.A.A_ID. In a database you couldn't do this because of the foreign key constraints, but how do the Linq to sql classes enforce those constraints on the client side?

+1  A: 

Linq to SQL, when attached to a relational database, is really just building SQL for you and so if you do something illegal in the database (like violate a fk constraint) it's the database that is really stopping you, not Linq itself. And constraints in Linq to SQL itself are just retreived from the DB.

So, unless you're talking about attaching it to an xml segment or the like (and I don't see that your question suggests that) then linq really isn't enforcing anything in and of itself. It's really just a very sophisticated codewriter.

Edit: I said that Linq to SQL is just a codewriter, but I should note that the metadata that lets you use the strong typing etc is nice. I don't mean to belittle the product.

Aramis wyler