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!

hard_life
  • 117
  • 2
  • 4
  • What are you using to manually add the relationships? Are you using the L2S designer in VS or are you manually editing the DBML file? – Lazarus Oct 20 '09 at 12:12
  • Are the associated EntitySet/EntityRefs not even being created in the generated code? – Lazarus Oct 20 '09 at 12:19
  • i.e. are you getting intellisense for this statement "var studentsEducation = student.Education;" – Lazarus Oct 20 '09 at 12:21
  • Nope and nope, nothing is generated. I just have an association on my designer. No code is added, thus no intellisense. – hard_life Oct 20 '09 at 12:27

2 Answers2

1

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
  • 1,847
  • 2
  • 14
  • 22
  • 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 Oct 20 '09 at 12:44
  • 1
    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 Oct 20 '09 at 12:47
1

I know this is a very old question, but I just ran into the same problem. In order for the relationship in the DBML designer to automatically create the association properties for you, you need to have primary keys on your objects. If you click the column name in the designer, you'll see that your PK field has PrimaryKey = false. Switch that to True and build; all should be well.

Patrick

D. Patrick
  • 2,894
  • 26
  • 37
  • This did the job. As soon as I saved the DBML file, the EntitySet was created in the generated class. Thanks – Mark42 Oct 12 '18 at 15:51