Entity Framework TPT - Insert sub-type for existing base-type

c# ef-code-first entity-framework-6 poco sql-server

Popular Answer

because you make use of the methodAdd when you notify yourDbContext that the database is adding a new Employee, and that yourDbContext should provide a new value for the new Employee's main key.

If it were possible to provide an Id while adding a new item, you could designate the same Person as two distinct Employees. A change in one employee's birthday would also change the other employee's birthday. You wouldn't want this, for sure.

As a result, DbContext will interpret the Add command into a SQL Insert query that ignores the Id you specify.

How do I create an employee record using an existing person record?

An employee "is" a person, not "has" a person, as you specifically said. You generate new Person-data when you create a new Employee object. It's the result of object-oriented design. Databases are not involved in any way. You must copy the Person attributes if you want a new Employee object with the identical values for its Person properties.

You should aggregate rather than derive if you want the notion to have a new Employee without a new Person. This is a really weird idea for an employee-person, yet it is pretty common for an employee-address: A Worker resides at an Address. Possibly residing at the same Address is a new employee. You may create a new employee and give it the same address as the existing employee—not a copy! As a result, you will have two Employees with the same address. Even better, you may change one of the Employees' addresses without changing the address of the other Employee.

0
2/19/2018 12:29:22 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow