Blake Rivell
Blake Rivell

Reputation: 13875

Entity Framework Navigation Property for a field that can store a foreign key from two different tables

I need a table called Loan.

The columns of this table will be: Id, Type, BorrowId, Description

The trick here is that Type field will determine whether the borrower is an Employee or a Customer and then the BorrowerId will either be an Id from the Employee table or an Id from the Customer table.

Is this bad design as far as Entity Framework is concerned? The reason I ask is because it seems like I won't be able to create a Borrower Navigation property on the Loan table since the Id can be from two tables.

Does anyone have a solution for this? Like how I can change my data models to work with Navigation properties.

Upvotes: 0

Views: 88

Answers (1)

Anthony Hancock
Anthony Hancock

Reputation: 931

A simple answer to your question is "Yes it's a bad design". Referential Integrity should be strictly enforced and when you remove that ability by alternating the reference you create a window for errors. If you want two options create two columns, and create foreign keys on each to the tables they reference. Then your application will be effectively foolproof. :D

Upvotes: 1

Related Questions