Inheritance and Existing Data

Posts   
 
    
ElQueso avatar
ElQueso
User
Posts: 27
Joined: 08-Oct-2005
# Posted on: 17-Jan-2008 22:24:41   

I have an issue with inheritance that I'm not sure how to solve. Using 2.5 Final, 12/5/2007 release. SQL Server 2005. Adapter scenario.

I have a Countries table and an ECCNLicenseCountries table. The ECCNLicenseCountries table is a 1-to-1 relationship to Countries.

Countries is already populated with a fixed list of country records. The user will be able to select from a list of Countries and create an ECCNLicenseCountry record. At this point, the only field that I have in the ECCNLicenseCountries table is the primary key from the Countries table, although more may be added in further releases.

I'm not sure how to create a new ECCNLicenseCountry record and associate it with an EXISTING CountryEntity master record.

I thought that I would need to fetch the CountryEntity that corresponded to the Country that I am trying to add as an ECCNLicenseCountry and assign it to the ECCNLicenseCountryEntity's CountryEntity property (so that when I saved the new sub-class entity GenPro would put the Country's primary key in the ECCNLicenseCountry table), but that property doesn't exist. However, in the designer, the ECCNLicenseCountry entity does show a CountryEntity property, 1-to-1, under the "Fields on relations" tab. It also shows the proper inheritance hierarchy on the "Inheritance info" tab. So my assumption is that because it is sub-classed from CountryEntity, it just doesn't show up at design time in the generated code. Which brings me back to how to created a sub-classed, inherited entity from an existing master record.

It seems to me that the only way to have done this with entities is to have created a new ECCNLicenseCountry entity, fill in the fields for both CountryEntity (as inherited) and ECCNLicenseCountryEntity and save. Which is what I do under normal circumstances with other sub-classed entities I've done. I've just never had an existing "master" record and needed to add a related record after the fact.

The only way I see to do this now is to do a scalar query to insert the primary key data into the ECCNLicenseCountry table, then fetch the ECCNLicenseCountryEntity. Seems to me this could be a fairly common issue and I'm either missing something or using this in the wrong way?

I hope I'm explaining this well enough to understand... Appreciate any help anyone can give me on this.

Walaa avatar
Walaa
Support Team
Posts: 14950
Joined: 21-Aug-2005
# Posted on: 18-Jan-2008 09:14:12   

First rule in LLBLGen Inheritance model is that an entity can't change it's type. By asociating a subType with an existing SuperType, you are attempting to change the type of the SuperTypeEntity to be of the SubType.

To overcome this, don't specify an Inheritance hierarchy for this relation and treat as normal relation.

Or do the following: 1- Fetch the Country. 2- Create a new instance of ECCNLicenseCountries and fill it with all the data needed, using the fetched Country entity. 3- Delete the fetched Country 4- Save the ECCNLicenseCountries entity (it will automatically insert the country before inserting the ECCNLicenseCountries )