1

Giving the following tables.

Project
Id: int not null PK
Type: nvarchar(10) not null 
SomeProperty: nvarchar(50)

ChangeRequest
Id: int not null PK
Type: nvarchar(10) not nul
SomeProperty: nvarchar(50)

Approval
Id: int not null PK
RowId: int not null
RowType: nvarchar(10) not null
SomeProperty: nvarchar(50)

I want to have associations like this:

  • Project(Id, Type) -> Approval(RowId, RowType)
  • ChangeRequest(Id, Type) -> Approval(RowId, RowType)

In a way that if I was selecting the data from database I would do something like:

select p*, a.*
from Project p
left join Approval a on a.RowId = p.Id and a.RowType = p.Type

Project and ChangeRequest are entities completely different (I just put the simplified model), but both may have 0..N Approvals.

Is it possible to map such relations using Entity Framework?

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Rael Silva
  • 11
  • 2
  • Are you asking how to add constraints to the ERD on the database, or are you asking how to SELECT related records from tables with C#? – peter_the_oak Mar 31 '15 at 22:37
  • Do you think you can use Inheritance? Create a Base class called Master, and then inherit Master1 and Master2 from the base class. It would be great if you can give details about Master1 and Master2 classes? – renakre Mar 31 '15 at 22:45
  • You might want to rethink your table design. With your current design, how would you know if an entry in the Detail table should link to Master1 or Master2? – failedprogramming Mar 31 '15 at 23:11
  • how do you have a relationship *without* a Foreign Key? the FK is what defines the relationship..... – Claies Mar 31 '15 at 23:43
  • Similar to your case for code first in EF http://stackoverflow.com/questions/5559043/entity-framework-code-first-two-foreign-keys-from-same-table – Eric Apr 01 '15 at 01:27

0 Answers0