Problem statement – [Business] 1—* [Branch] A business must have one or more branch(es). Only one branch can be main branch.
I am have two design in my mind
- [Branch table] – {id, BusinessID (FK), Name, etc , IsMainBranch}
- [Branch table] – {id, BusinessID (FK), Name, etc }, [MainBranch table] – {BranchID (PK, FK), BusinessID (FK)}.
- [Business table] – {id, name, mainbranchid}, [Branch table] – {id, BusinessID (FK), Name, etc}
Problem with #1 – To put constrain on IsMainBranch I have to use triggers, which can go out of sync latter.
Problem with #2 – Bit complicated to access the data in EF.
Problem with #3 – Don’t think its a good design.
I am using EF 4.1 as ORM and don’t want to make things complicated, which design I should choose. Please suggest if there is a better/alternate way available for the same.
With SQL Server 2008+ you can use filtered indexes
In your
Branchtable, just have a flagIsMainBranch. Then create a unique filtered index: this will allow one row only whereIsMainBranch= 1per Business.Branch table:
Then
This gives
MainBranchIDin the Business table)Also see: