Search code examples
c#persistenceentitybltoolkit

bltoolkit inserting data to a table


I have this entity :

namespace Entities.dbo
{
    [TableName("tbl_snapshot")]
    public class Snapshot : AbstractEntity
    {
        [MapField("track")]
        public int TrackId { get; set; }

        [Association(CanBeNull = false, OtherKey = "id", ThisKey = "track")]
        public Track Track { get; set; }

        [MapField("snapshotnumber")]
        public int SnapshotNumber { get; set; }

        [MapField("data")]
        public string Data { get; set; }
}}

and I try to insert a new Snapshot into a database like this :

public static void XXX(Snapshot snapshot)
        {
            using (var db = new MyDbManager())
            {

                var s = new Snapshot
                                 {
                                     Id = snapshot.Id,
                                     Data = snapshot.Data,
                                     SnapshotNumber = snapshot.SnapshotNumber,
                                     TrackId = snapshot.Track.Id
                                 };


                db.GetTable<Snapshot>().Insert(() => s);
            }
        }

Can you see any problem there? the snapshot I send to the XXX method look like this:

(Serialized in JSON ):

{"TrackId":2,"Track":null,"SnapshotNumber":2,"Data":"030405","Id":3}

any idea where is the problem?

thanks


Solution

  • The problem is Bltoolkit's LinQ insert method does not support a constant value: db.GetTable().Insert(() => s); s is a constant value.

    Here are 02 solutions you can try:

    1. Simply don't use the LinQ method:

      db.Insert(s);

    2. If you still want LinQ method? please follows this link, you will find a workaround:

    http://yfilonov.blogspot.com/2012/10/linq-warkaround-for-bltoolkit-insert.html