I'm using version 2.6.88 of NPoco Micro-ORM.
Lets say I have a class hierarchy like this:
[NPoco.TableName("Person")]
[NPoco.PrimaryKey("PersonID", AutoIncrement = false)]
class Person
{
public Guid PersonID { get; set; }
public String Name { get; set; }
}
class Employee : Person
{
public String Department { get; set; }
}
I need to map this class hierarchy to a database designed with the "Table per Subclass" approach. This means I have Person
table with columns PersonID
and Name
, and I have a Employee
table with columns PersonID
and Department
.
Now my question is how do I insert a new Employee into the database with NPoco? I tried something like this:
Employee myEmployee = new Employee() { PersonID = Guid.NewGuid(), Name = "Employee Name", Department = "TestDepartment" };
NPoco.Database myDb = new NPoco("MyConnection");
using (var transaction = myDb.GetTransaction())
{
myDb.Insert<Person>(myEmployee as Person);
myDb.Insert<Employee>("Employee", "PersonID", false, myEmployee);
transaction.Complete();
}
This code fails on the first insert, because NPoco tries to insert the Employee specific fields into the Person table.
How do I implement this correctly?