Saving parent-child pair using InsertWithIdentity not works

75 views
Skip to first unread message

Kate-SV

unread,
Apr 26, 2013, 9:02:46 AM4/26/13
to blto...@googlegroups.com
Hi, I'm new to BLToolkit, so please advise. Do I use mapping and saving code right?
I have 2 entities, one is independent (Broker), other contains FK from first (Account).
First class with mapping:
    [TableName("BROKER")]
    public abstract class Broker
    {
        [MapField("BROKER_ID"), PrimaryKey, Identity]
        public int BROKER_ID { get; set; }

        [MapField("NAME")]
        public string Name { get; set; }

        [Association(ThisKey = "BROKER_ID", OtherKey = "Broker.Id")]
        public abstract List<Account> Accounts { get; set; }

    }
Second class:    
        [TableName("ACCOUNT")]
    [MapField("BROKER_ID", "Broker.Id")]
    public abstract class Account
    {
        [MapField("NAME")]
        public string Name { get; set; }

        public abstract Broker Broker { get; set; }

        [MapField("ACCOUNT_ID"), PrimaryKey, Identity, NonUpdatable(IsIdentity = true)]
        public abstract int Id { get; set; }
    }
Then I try to generate data and save them to DB.
private static Account InsertAccountWithIdentity()
        {
            var account = GenerateAccount();//I create and set Name field there
            var broker = GenerateBroker();//I create and set Name field there
            account.Broker = broker;
            broker.Accounts.Add(account);
            var id = dbManager.InsertWithIdentity(broker);
            broker.BROKER_ID = Convert.ToInt32(id);
            return account;
        }
This code inserts both entities, but table field ACCOUNT.BROKER_ID remains empty. It shouldn't be! What is wrong?
Thank you in advance.

Kate-SV

unread,
May 2, 2013, 8:46:52 AM5/2/13
to blto...@googlegroups.com
Seems it is fixed in 4.1.21 version of BLToolkit (not in release yet). I downloaded source, compiled and it worked fine with new version.
Reply all
Reply to author
Forward
0 new messages