Saving single-field entities

Posts   
 
    
HcD avatar
HcD
User
Posts: 214
Joined: 12-May-2005
# Posted on: 03-Jan-2006 21:52:43   

Hi,

in our project, all entities that have "description" fields must be made multi-language. We have solved this by moving the descriptions into a separate table "xxx_LOCALE" where all descriptions reside, one for each language. Now the problem is that sometimes we have entities with nothing more then a description, so the entities has only one field, the autonumber primary key. Let's say we have an entity REGION, then the DB looks like : REGION : 1 field REGION_ID (int,PK) REGION_LOCALE : 3 fields : REGION_ID (int,FK), LANGUAGE_ID (int,FK) and DESCRIPTION (string) Now take the situation when we want to add such an entity to the the Database. What i do is create a new RegionEntity, and for each available language in the system, create a RegionLocaleEntity and add it to the RegionEntity.RegionLocale collection Now if i want so save this entity i got the error message :


An exception was caught during the execution of an action query: Cannot insert the value NULL into column 'REGION_ID', table 'AGRO.dbo.REGION_LOCALE'; column does not allow nulls. INSERT fails.
The statement has been terminated.. Check InnerException, QueryExecuted and Parameters of this exception to examine the cause of this exception.

Now after some fiddling i have set the region.IsDirty to true to mage sure the region got saved and then i got the error message :


The insert query doesn't contain any fields.
   at SD.LLBLGen.Pro.ORMSupportClasses.EntityBase.Save(IPredicate updateRestriction, Boolean recurse)
   at Agro.Datalayer.EntityClasses.RegionEntityBase.Save(IPredicate updateRestriction, Boolean recurse)
   at SD.LLBLGen.Pro.ORMSupportClasses.UnitOfWork.Commit(ITransaction transactionToUse, Boolean autoCommit)
   at Agro.Detailforms.RegionForm.PersistData()

Since i have used this mechanism at numerous other locations i'm sure it has to do with the fact there is only one (autonumbered) field in the entity. Is there any solution ?

thnx.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 03-Jan-2006 22:45:09   

For sqlserver this is supported in 1.0.2005.1, for other databases it isn't. Entities with solely a PK with an identity field aren't really supported, for sqlserver there's a statement (INSERT INTO table DEFAULT VALUES) which offers the ability to work around it and why it is supported there, but for other databases it isn't. Add a field to the entity with solely a PK which is an identity.

Frans Bouma | Lead developer LLBLGen Pro
HcD avatar
HcD
User
Posts: 214
Joined: 12-May-2005
# Posted on: 03-Jan-2006 23:30:39   

hmm okay, i'm indeed using SQL server (2000) but still also using llbl 1.2004.2 and i'd rather not switch to the 1.2005 to not jeopardize the thousands of lines of code in the __LLBLGENPRO_USER_CODE_REGION blocks at this stage of the project. So the best i can do is just add a dummy field to my entity ?

Paul.Lewis
User
Posts: 147
Joined: 22-Aug-2005
# Posted on: 04-Jan-2006 05:15:30   

HcD wrote:

So the best i can do is just add a dummy field to my entity ?

That's exactly what I did in a previous project. Turned out the dummy field was used latter...saved me the need to rebuild the schema. wink

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 04-Jan-2006 09:11:43   

HcD wrote:

hmm okay, i'm indeed using SQL server (2000) but still also using llbl 1.2004.2 and i'd rather not switch to the 1.2005 to not jeopardize the thousands of lines of code in the __LLBLGENPRO_USER_CODE_REGION blocks at this stage of the project.

Those are preserved simple_smile Try it. Install 1.0.2005.1 on a machine, copy your current code to a new empty folder, generate code on top of that code using 1.0.2005.1 and compile against the new runtimes. 10 to 1 you don't even have to do a thing. simple_smile

Frans Bouma | Lead developer LLBLGen Pro
HcD avatar
HcD
User
Posts: 214
Joined: 12-May-2005
# Posted on: 04-Jan-2006 09:19:04   

Lo and behold !

that was exactly the same idea i had this morning, so i took a test machine, installed the 2005 (version 22december) on it, got the lastest datalayer from my source control and ... no problem whatsoever ! could read my LGP file and just regenerate (self-service full/safe) and everything works ! so the switch to 2005 was much smaller then i anticipated, it took all of 5 minutes simple_smile

Then on to the test ... same error (it fails when saving the description nagging about the FK field being null). BUT if i manually set my single field entity ".isDirty = true" then it DOES get saved and the descriptions save ok ! So everything works as expected now ! (only thing is to not forget the isDirty flag )

Thanx once again ! /me bows humbly to the master ...

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 04-Jan-2006 10:27:23   

smile glad it's sorted out! smile

Frans Bouma | Lead developer LLBLGen Pro