I have three entities:
BaseEntity (Guid Id) Primary key (for all entities)
Person (Name, FamilyName) : BaseEntity
Employee (Salary) : Person
How to map Employee so that its Primary key was also a foreign key for Person?
Should I inherit from Person in Employee class?
Or maybe I should both classes: Person and Employee inherits from BaseEntity
and in Employee Add property: Person?
Anyway I was trying to achieve the first option. Is it possible?
EDIT
I am using auto mapper (FluentNHibernate.Automapping).
I wanted here to achieve inheritance per type. That Both entities have their tables: Person (with Id, name, familyname) and Employee (id, salary)
Related
My professor is telling me to have 2 relationships between my employee and department entities, one for the employee relationship and the other for the manager relationship (a manager is also an employee). What attributes do I connect them on? Do I need another primary/foreign key?
I intend making manager id a primary key in the department table, but I'm not sure if that is correct.
It seems to me that the departmentId has to be the primary key so that you don't have duplicate departments. If that's true, then in the Employee table, I would have one relationship from Employee.Department_ID to Department.Department_ID to define which department the employee belongs to. Then I would have a second relationship from Employee.Employee_ID to Department.Manager_ID which defines which employee is the department manager.
I have two tables. Teacher and student. The teacher table consist with id, name, classid, studentId. In student table consist of id, name, TeacherId.
One student can have more than one teachers. And the teacherid is set as foreign key in student table. Then how can I save my student table with multiple teacherid?
If a student can have multiple teachers then you can't put the teacher ID in the student table. You need a third table that contains the combinations of student ID and teacher ID. That way, one student record can be related to multiple teachers and each teacher can be related to multiple students. A third table is how you implement a many-to-many relationship.
You can have a separate table with student and teacher mapping. So, that a single student id could be mapped with multiple teacher ids and you can also have date range or deactivation columns to change your mappings.
Relationships between entities in relational databases are commonly divided into 3 types. This is called the cardinality of the relationship:
1 to N: For example an order (1) with it's products (N). In this case the product would have the order's key.
1 to 1: For example a teacher (1) with it's contact info (1). This type of relationships might raise an eyebrown since you could put the contact's info inside the teacher's table. In this case either the contact has the teacher's key or the teacher has the contact's key (or both share the same exact key).
N to M: Like in your case, a particular teacher may have more than 1 student and each of those students might have another teachers. You can't store this relationship in 2 tables (not in a good way at least), so the solution is creating a 3rd table that links the two.
For this last case, you would have something like the following:
CREATE TABLE Teacher (
TeacherID INT PRIMARY KEY,
-- Other teacher columns
)
CREATE TABLE Student (
StudentID INT PRIMARY KEY,
-- Other student columns
)
CREATE TABLE TeacherByStudent (
TeacherID INT,
StudentID INT,
PRIMARY KEY (TeacherID, StudentID),
FOREIGN KEY (TeacherID) REFERENCES Teacher (TeacherID),
FOREIGN KEY (StudentID) REFERENCES Student (StudentID))
Better you should following tables
Student
Id (Primary Key)
Name
Teacher
Id (Primary Key)
Name
ClassId (Foreign Key)
TeacherStudentMapping
Id (Primary Key)
StudentId
TeacherId
Set Unique Key for StudentId & TeacherId.
In an existing SQL Server database, someone has defined the following:
Table Customer has a CustomerID column which is also identity. Then they have defined a relationship where Customer.CustomerID is primary key and Customer.CustomerID is also foreign key. I.e. the relationship points back to the same table and same column.
What is the purpose of this? Seems entirely pointless to me, so I plan to remove this relationship from the DB.
The relation is called a recursive association or reflexive relationship, you will need this type of relationship when you need to present a relationship between two or more elements of the same type.
For example: for presenting a relationship between employees, you could create two tables Employee and Manager. But because the manager is also an employee, you won't need two tables. So we create a recursive association that point for the same entity.
More about recursive association
UPDATE
Setting a column as PK and FK at the same time could also represent the concept of inheritance.
For example:
class Person {
int ID;
string name;
}
class Customer extends Person {
String workPlace;
}
That would result the tables Person and Customer as listed below:
Person
------------
int id (PK)
string name
Employee
--------------
int id (PK, FK)
string workPlace
If you have the following class:
Public Class Male
Inherits Person
Private Age As integer
Private TelephoneNumber As String
End Class
and the following table
CREATE TABLE Person (ID int identity not null, Age int, TelephoneNumber varhchar(30), sex varhchar(6), primary key (ID))
In order to populate the Male class you would have to run an SQL statement, saying: WHERE Sex='MALE'. Is this a poor design choice where one database table has two classes? i.e. there is a one to many relationship.
This is Table per Class Hierarchy, and is perfectly acceptable. This design pattern uses a single table to store multiple subclasses and uses a discriminator (in your case Sex) to distinguish between them.
I have a problem with one-to-one relationships in the fluent nHibernate.
I have the following relational table from the AdventureWorks2008 database.
BusinessEntity (Table)
BusinessEntityId Int (PK, Identity)
Person (Table)
BusinessEntityId int (PK, Reference with BusinessEntity table)
FullName varchar(255)
The relationship between BusinessEntity table and Person table is one-to-one.
How do I map fluently without any extra field like "Id" in the Person table?
There should be 2 class one for Person and another for BusinessEntity, or an appropriate model to best describe the above relation.
Thanks,
Ashraf.
presuming your Person mapping is pretty standard, the way you do this is by saying:
Id(x => x.BusinessEntityId)
.GeneratedBy.Foreign("BusinessEntity");
on the Person class.
This presumes that your Person class has a property called BusinessEntity which is of type BusinessEntity.
You'll also need to map BusinessEntity to Person with constrained set to true (to say that they primary key of Person is a foreign key reference to BusinessEntity).
The key thing is the GeneratedBy.Foreign() to say that your identity is generated by a link to another class.