Why is foreign key not set?

Posts   
 
    
Ian avatar
Ian
User
Posts: 511
Joined: 01-Apr-2005
# Posted on: 12-Sep-2017 15:44:40   

I have

guestEntity.GuestAccount = new AccountEntity()
{
   IsNew = true
};

When guestEntity is recursively saved, the database complains that its property GuestAccountId is null.

Given that I've set the object, shouldn't LLBLGen automatically update the key field?

guestEntity in this instance is new too.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39590
Joined: 17-Aug-2003
# Posted on: 12-Sep-2017 15:54:21   

https://www.llblgen.com/tinyforum/Messages.aspx?ThreadID=7722

also, why setting isnew to true in accountentity ? It's a new entity instance, so that's set by default.

Anyway, if AccountEntity doesn't have an identity PK, it won't be saved as it's not 'dirty' so nothing is synced as there's nothing to sync

Frans Bouma | Lead developer LLBLGen Pro
Ian avatar
Ian
User
Posts: 511
Joined: 01-Apr-2005
# Posted on: 12-Sep-2017 16:01:11   

yes, I was going to remove isNew once its working.

The model looks ok. I've attached a screen shot.

Attachments
Filename File size Added on Approval
Screen Shot 2017-09-12 at 14.59.55.png 32,822 12-Sep-2017 16:01.28 Approved
Ian avatar
Ian
User
Posts: 511
Joined: 01-Apr-2005
# Posted on: 12-Sep-2017 16:12:26   

Designer: 3.5 Final Runtime: 3.5.0.0 Stack:

at SD.LLBLGen.Pro.ORMSupportClasses.ActionQuery.Execute() at SD.LLBLGen.Pro.ORMSupportClasses.BatchActionQuery.Execute() at SD.LLBLGen.Pro.ORMSupportClasses.DataAccessAdapterBase.ExecuteActionQuery(IActionQuery queryToExecute) at SD.LLBLGen.Pro.ORMSupportClasses.DataAccessAdapterBase.PersistQueue(List`1 queueToPersist, Boolean insertActions, Int32& totalAmountSaved) at SD.LLBLGen.Pro.ORMSupportClasses.DataAccessAdapterBase.SaveEntity(IEntity2 entityToSave, Boolean refetchAfterSave, IPredicateExpression updateRestriction, Boolean recurse) at SD.LLBLGen.Pro.ORMSupportClasses.DataAccessAdapterBase.SaveEntity(IEntity2 entityToSave, Boolean refetchAfterSave, Boolean recurse) at JGSR.BFWeekender.LLBLGenDataAccess.Shared.SharedGenericRepository.SaveEntity(IEntity2 entity, Boolean refretch) in C:\Users\ian\Documents\Source\Commerce\Projects\Websites\Commerce\JGSR.BFWeekender.LLBLGenDataAccess\Shared\SharedGenericRepository.cs:line 13 at JGSR.BFWeekender.BusinessServices.AccountService.UpdateGuest(GuestEntity guestEntity) in C:\Users\ian\Documents\Source\Commerce\Projects\Websites\Commerce\JGSR.BFWeekender.Domain\AccountService.cs:line 34 at JGSR.BFWeekender.Web.group.raver.btnSubmit_OnClick(Object sender, EventArgs e) in C:\Users\ian\Documents\Source\Commerce\Projects\Websites\Commerce\JGSR.BFWeekender.Web\account\guest.aspx.cs:line 119

DB: Sql Server 13.0.4001

Guest is a contact, guest has an account parent and a single guest account.

Profiler shows foreign key isn't set at all...

exec sp_executesql N'INSERT INTO [mydb].[dbo].[tbl_guest] ([GuestID], [AccountID], [Ordinal]) VALUES (@p13, @p14, @p15)',N'@p13 int,@p14 int,@p15 int',@p13=45749,@p14=17298,@p15=1
Ian avatar
Ian
User
Posts: 511
Joined: 01-Apr-2005
# Posted on: 12-Sep-2017 16:34:03   

aha, so it works if I set a property on Account, which I should have added anyway.

guestEntity.GuestAccount = new AccountEntity { EticketRef = Guid.NewGuid().ToString() };

LLBLGen won't save the Account at all if I don't set the property.

Walaa avatar
Walaa
Support Team
Posts: 14946
Joined: 21-Aug-2005
# Posted on: 12-Sep-2017 18:41:17   

True.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39590
Joined: 17-Aug-2003
# Posted on: 12-Sep-2017 20:51:33   

If it has an identity PK it should be saved, but that wasn't always the case, tho I think on 3.5 that was already implemented. If it doesn't have a pk which is an identity it isn't saved indeed.

Frans Bouma | Lead developer LLBLGen Pro