如何在BLToolkit.3中使用InsertOrReplace



我开始使用BLToolkit,并且有一个新的优势:InsertOrReplace当我尝试使用它时,有一个例外:"插入或更新方法不支持标识字段'Margin.id'"我的模型在这里:

[TableName("Margin")]
    public class Margin
    {
      [PrimaryKey, Identity]    public int      id;
      [NotNull]                 public string   StoreID;
      [PrimaryKey]              public int?     PrTypeID;
                                public decimal  MarginRate;
      [Association(ThisKey = "PrTypeID", OtherKey = "ProductID", CanBeNull = true)] public Product Product;
    }

调用该方法:

 db.InsertOrReplace(new CSSWarranty.DataModel.DataModel.Margin()
                                      {
                                          MarginRate = newMargin.Margin,
                                          PrTypeID = newMargin.ProductTypeID == 0 ? null : newMargin.ProductTypeID,
                                          StoreID = newMargin.StoreID,
                                          id = newMargin.MarginID
                                      });

可能有人可以说如何使用下一个构造:db。Margin.InsertOrUpdate(x,y)所有问候!

我不明白为什么这个例子有效:

[TableName("Notification")]
    public class Notification
    {
        [PrimaryKey]    public      string NotificationID;
                        public      string     Note;
    }

叫:

 var db = new RetailerDb()
db.InsertOrReplace(new DataModel.DataModel.Notification()
                              {
                                  Note = note,
                                  NotificationID = "ConfirmNote"
                               });

数据库类:

private var db = new RetailerDb();
public class RetailerDb : DbManager
{
    public RetailerDb() : base("DBConnection")
    {
    }
    public Table<DataModel.Margin> Margin
    {
        get { return GetTable<DataModel.Margin>(); }
    }
}
到目前为止,

我已经设法从bltoolkit/Source/Data/Linq/Query.cs类中找到:

public static int InsertOrReplace(IDataContextInfo dataContextInfo, T obj)
{
    ...
    else if (field.IsIdentity)
    {
        throw new LinqException("InsertOrUpdate method does not support identity field '{0}.{1}'.", sqlTable.Name, field.Name);
    }
    ...
}

静态方法从 bltoolkit/Source/Data/Linq/Extensions.cs 类调用:

public static int InsertOrReplace<T>(this IDataContext dataContext, T obj)
{
    return Query<T>.InsertOrReplace(DataContextInfo.Create(dataContext), obj);
}

似乎Identity字段会引发异常。

[PrimaryKey, Identity]    public int      id; //remove this field or change the column definition

[编辑]

看看以下类是如何定义的:

public class Patient
{
    [PrimaryKey]
    public int    PersonID;
    public string Diagnosis;
    //more class definition
}
public class Person
{
    //some class definition
    [Identity, PrimaryKey]
    //[SequenceName("PostgreSQL", "Seq")]
    [SequenceName("Firebird",   "PersonID")]
    [MapField("PersonID")] public int    ID;
                           public string FirstName { get; set; }
                           public string LastName;
    [Nullable]             public string MiddleName;
                           public Gender Gender;
    [MapIgnore]            public string Name { get { return FirstName + " " + LastName; }}
    [Association(ThisKey = "ID", OtherKey = "PersonID", CanBeNull = true)]
    public Patient Patient;
    //more class definition
}

下面是测试方法中的示例用法:

[Test]
public void InsertOrUpdate1()
{
    ForEachProvider(db =>
    {
        var id = 0;
        try
        {
            id = Convert.ToInt32(db.Person.InsertWithIdentity(() => new Person
            {
                FirstName = "John",
                LastName  = "Shepard",
                Gender    = Gender.Male
            }));
            for (var i = 0; i < 3; i++)
            {
                db.Patient.InsertOrUpdate(
                    () => new Patient
                    {
                        PersonID  = id,
                        Diagnosis = "abc",
                    },
                    p => new Patient
                    {
                        Diagnosis = (p.Diagnosis.Length + i).ToString(),
                    });
            }
            Assert.AreEqual("3", db.Patient.Single(p => p.PersonID == id).Diagnosis);
        }
        finally
        {
            db.Patient.Delete(p => p.PersonID == id);
            db.Person. Delete(p => p.ID       == id);
        }
    });
}

您可以看到,Person类没有使用 InsertOrUpdate,但Patient类有用法。因此,仅当您传入非Identity字段时,才支持该方法。

注意: InsertOrUpdate已过时,但仍在项目源代码的测试中使用。不过,它应该没有影响,只是把它想象成InsertOrReplace.

最新更新