Subtype Insert Help

Posts   
 
    
lotek
User
Posts: 56
Joined: 14-Sep-2005
# Posted on: 17-Nov-2005 02:34:53   

I have a supertype, lets call it

Person -PersonId -Name

I have a subtype, lets call it

Employee -PersonId -Employee Data

If the person already exists, and i want them to become an employee, how can i do it?

Im trying:

Employee employee = new Employee(1); adapter.FetchEntity(employee); employee.EmployeeData = somedata; adapter.SaveEntity(employee);

Also tried:

Employee employee = new Employee(1); employee.IsNew = true; employee.PersonId = 1; employee.EmployeeData = somedata; adapter.SaveEntity(employee);

And other variations...

Ideas?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 17-Nov-2005 08:56:41   

lotek wrote:

I have a supertype, lets call it

Person -PersonId -Name

I have a subtype, lets call it

Employee -PersonId -Employee Data

If the person already exists, and i want them to become an employee, how can i do it?

Im trying:

Employee employee = new Employee(1); adapter.FetchEntity(employee); employee.EmployeeData = somedata; adapter.SaveEntity(employee);

Also tried:

Employee employee = new Employee(1); employee.IsNew = true; employee.PersonId = 1; employee.EmployeeData = somedata; adapter.SaveEntity(employee);

And other variations...

Be aware that if a type can change for a given entity, it's likely that entity isn't a good candidate for an inheritance hierarchy. Your example is one of them. See it as this: you have 2 classes, Foo and Bar and Bar inherits from Foo. You create an instance of Foo, now, you'll never be able to cast that instance to 'Bar'.

To solve your problem: load the data of person into a new employee. Delete Person, Save Employee.

Frans Bouma | Lead developer LLBLGen Pro
lotek
User
Posts: 56
Joined: 14-Sep-2005
# Posted on: 17-Nov-2005 18:22:40   

Well the problem with deleting then adding is everything associated with person will be deleted as well. So probably best to just remove the entities from the hierarchy.

Thanks for the help. -Matt

lotek
User
Posts: 56
Joined: 14-Sep-2005
# Posted on: 17-Nov-2005 21:48:39   

Humm, im having another problem now using hierarchies...

3 Tables, lets call them:

Person -personid (pk, identity)

Employee -personid (pk) -employeestuff

Manager -personid (pk) -managerstuff

I have a hierarchy enabled between manager and employee only. (Not employee and person)

Manager manager = new Manager(); manager.IsNew = true; manager.personid = id; manager.employeestuff = stuff; manager.managerstuff = stuff; adapter.SaveEntity(manager);

I get an error saying cannot insert null into employee.personid....

Thanks Matt

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 17-Nov-2005 22:00:55   

very strange. is PersonId in employee a true PK or does employee have a relation with person as well, and is personid actually an FK to the PK of person?

Frans Bouma | Lead developer LLBLGen Pro
lotek
User
Posts: 56
Joined: 14-Sep-2005
# Posted on: 17-Nov-2005 22:40:51   

Ok:

Person (this table is replicated) CONSTRAINT PK_Person PRIMARY KEY NONCLUSTERED (personid)

Employee CONSTRAINT PK_Employee PRIMARY KEY CLUSTERED (personid) CONSTRAINT FK_Employee_Person FOREIGN KEY (personid) REFERENCES Person (personid)

Manager CONSTRAINT PK_Manager PRIMARY KEY CLUSTERED (personid) CONSTRAINT FK_Manager_Employee FOREIGN KEY (personid) REFERENCES Employee (entity_id)

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 17-Nov-2005 23:16:54   

Ok, personid in employee isn't the real PK, (it's depending on another value, as it's an FK also). so that's why it's not working. Include person in the Hierarchy or remove the fk employee.personid -> person.personid

Frans Bouma | Lead developer LLBLGen Pro
lotek
User
Posts: 56
Joined: 14-Sep-2005
# Posted on: 18-Nov-2005 00:07:29   

Im just striking out today...

I took out all the hierarchies...

But still have the table structure...

Person (this table is replicated) CONSTRAINT PK_Person PRIMARY KEY NONCLUSTERED (personid)

Employee CONSTRAINT PK_Employee PRIMARY KEY CLUSTERED (personid) CONSTRAINT FK_Employee_Person FOREIGN KEY (personid) REFERENCES Person (personid)

Manager CONSTRAINT PK_Manager PRIMARY KEY CLUSTERED (personid) CONSTRAINT FK_Manager_Employee FOREIGN KEY (personid) REFERENCES Employee (entity_id)

Im trying to simple insert into Employee and i get "Cannot insert the value NULL into column 'personid', table 'Employee'; column does not allow nulls.

Employee employee = new Employee(); employee.IsNew = true; employee.personid = id; employee.employestuff = stuff; adapter.SaveEntity(employee);

Am i missing something, is this a bad table design? Im not sure what the problem is with this. I traces the id that personid is being set to and ensured it had a value.

Nevermind, personid was marked as identity in the designer. I unchecked it and this fixed the problem.

Thanks

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 18-Nov-2005 11:11:32   

lotek wrote:

Nevermind, personid was marked as identity in the designer. I unchecked it and this fixed the problem.

That was my last hope indeed simple_smile Glad it's solved simple_smile

Frans Bouma | Lead developer LLBLGen Pro