views:

63

answers:

1

Pardon the massive headline.

I'm in the situation of having to build an application on top of a database, that I cannot make any changes to. The database does not have any primary- or foreignkeys set.

I'm using linq-2-sql, and I'm interested in having some properties exposed on the entities generated from my dbml. For instance, in the hypothetical example of a one-to-many relationship between table education and student - where each student record has a reference to an education id, I'd like to be able to go:

var student = GetAStudentFromContextOrWhatever();
var studentsEducation = student.Education;

It is my experience, that this kind of property is automatically generated when I drag'n'drop tables with foreignkey relationships from the server explorer.

However as previously mentioned, in this case I do not have these foreign key relationships - rather I am adding the relationships manually in the dbml file, specifying parent and child class. When I add these relationships, I expect the involved entities in the designer.cs of my context to get populated with properties of a kind like those described above.

This, however, does not happen.

What must I do for my dbml to create these properties for me - based on these manually mapped associations between entities/tables that, on a database level, do not have foreign key associations?

Cheers!

A: 

L2S is just that Linq-to-SQL. If it isn't in SQL it won't be generated. The expression trees behind L2S just can't understand what you are doing. The place for your association is in a partial class file which you will have create manually. Also it probably won't update or insert through the association.

Gary
I'm quite certain it will be added on update or insert. A quick search here reveals this topic discussing sort of a similar issue http://stackoverflow.com/questions/619231/linq-to-sql-without-explicit-foreign-key-relationships
hard_life
And just to clarify some more, I think i stated that my complaint is not that it does not create properties magically for me, when dragging in tables with no foreign keys. My problem is that when i create the associations by hand in the dbml file, that properties are not created in the code.
hard_life