0

I have 2 project in my .net solution.each one has entity data model.how I can make an association between two entities one in the first project data model while the other in the second project data model in the same solution?

what if each data model mapped to different database?

moataz
  • 1
  • Possible duplicate of [Add Foreign Key relationship between two Databases](https://stackoverflow.com/questions/4452132/add-foreign-key-relationship-between-two-databases) – Daniël Tulp Nov 08 '17 at 13:12

2 Answers2

1

As far as I know you can't achieve this within a reasonable solution.

Lars Thorén
  • 453
  • 2
  • 7
  • 22
0

It is impossible to have a database relation between two fields in separate databases, however, you can do this of course with server side logic in your application code. If you do, make sure you add a trigger to enforce referential integrity to make sure the relation can be made if it requires a field not to be null

Add Foreign Key relationship between two Databases

Create Trigger dbo.MyTableTrigger ON dbo.MyTable, After Insert, Update
As
Begin

   If NOT Exists(select PK from OtherDB.dbo.TableName where PK in (Select FK from inserted) BEGIN
      -- Handle the Referential Error Here
   END

END
Daniël Tulp
  • 1,745
  • 2
  • 22
  • 51