简体   繁体   中英

How to implement the right relationships between tables?

Hello everyone,

I'm planning to change the current backend for my company. I'm planning to create new web service with asp.net core and ef core . To be honest I'm stuck with the architecture.

I don't know how should I implement the relationships between the tables for Entity Framework. There is more than one positions as employees. And I don't want to implement a role layer for this architecture. So I've created tables like this: Employees , Cashiers , Packers , ... etc. And designed them like this: Employees (EmployeeId, EmployeeName, ...) Cashiers (EmployeeId, Fee, ...) But I've read somewhere there is no way to use 1 to 1 relationships in sql. So how can I implement this relationship?

ER Diagram: Entity Relationships

If you are using SQL Server, then I agree, it's not possible to have a real 1-to-1 relationship.

The relatioship would need to be 1:0..1 since you need to insert one row first, and the other one second. For some minimal amount of time, the relationship would be 1:0 and then will switch to 1:1.

To implement a real 1-to-1 you'll need to use the standard SQL feature called Constraint Deferrability . Unfortunately this feature is available only in PostgreSQL and Oracle databases, as far as I know. On these databases you can truly have 1:1 relationships, since the constraint is validated not on every row insert, but at the end of the transaction.

Having said that, I wouldn't care too much. If you perform the insertion using database transactions, then I don't see why this would cause any problem. Using a process like:

  1. Start transaction.

  2. Insert on table Employee .

  3. Insert on table Cashier .

  4. Commit transaction.

This should ensure you never have a dangling employee with no related table.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM