views:

536

answers:

2

Is it possible to have something like ContactAddress.Contact in LINQ without create a foreign key relationship in SQL Server between those two (which would by Contact.Id <-> ContactAddress.ContactId)?

Thanks :)

+3  A: 

Yes, you may define foreign keys that do not exist in the target database. The joins generated by LINQ to SQL will end up the same, by which I mean this:

from category in db.Categories
from product in category.Products
select new
{
    Category = category,
    Product = product
}

will generate the same T-SQL as this:

from category in db.Categories
join product in db.Products on category.CategoryId equals product.CategoryId
select new
{
    Category = category,
    Product = product
}

which would be this:

SELECT
    *
FROM
    Category INNER JOIN Product ON Category.CategoryId = Product.CategoryId

This is a useful technique to make legacy schemas more usable.

Bryan Watts
+3  A: 

If you want to create a relationship in your Object-Relational Map (even when that relationship doesn't exist in the database as a declared foreign key), then you can do so using the Object-Relational designer.

http://msdn.microsoft.com/en-us/library/bb629295.aspx

David B