views:

178

answers:

3

I have two different dbml diagrams reflecting my Linq-To-SQL classes. (This is necessary, because they appear in different projects.) One of the objects in the one diagram needs an association with an object in the other diagram.

How do I do it?

A: 

Actually your two diagrams meens two different data contexts will be generated. I also guess your using SqlMetal on the diagrams to generate your entities.

You will need to include all associated objects in one diagram or the datacontext wont be able to retrieve that relationship from the database for you.

Another option is using custom entities and an XML mapping file.

Johannes Rudolph
Yes, 2 different data contexts, but the one is visible to the other. Dunno what SqlMetal is; I'm just dragging-and-dropping tables from the Server Explorer.
Shaul
Sqlmetal is the Tool that generates Code from your dbml file
Johannes Rudolph
A: 

As it turns out, the easiest way I found to accomplish this is by forcing the relationship. I created my own partial class to match the class containing the FK, and just mimicked the generated code that I found for other relationships.

This has only one shortcoming AFAI can tell: there's a piece of generated code in the actual foreign key field's set property that should throw an exception if you try and set the value when there's already a value in place:

if (this._Parent.HasLoadedOrAssignedValue)
{
  throw new System.Data.Linq.ForeignKeyReferenceAlreadyHasValueException();
}

But I'm prepared to live without that, as long as I know that I shouldn't be setting the FK field explicitly.

Shaul
A: 

I had a concern about this issue myself, which is why in my case, I put all the entities in one context. The context is too big and complex to use in the designer now (it takes about 20 minutes to load and probably has more than 100 entities), so we use SQLMetal (the command line form of the DBML compiler/generator) to build it instead. The DBML itself is maintained with (generated by) a tool I created for designing our schema. It's not exactly an answer to your question, but is one way to deal with this concern.

BlueMonkMN
Ya, but I don't have this option since the 2nd diagram is in a different project.
Shaul