3

BLToolkitを使い始めましたが、新しい利点があります。InsertOrReplaceこれを使おうとすると、例外が発生します。「InsertOrUpdateメソッドはIDフィールド「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"
                               });

DBクラス:

private var db = new RetailerDb();

public class RetailerDb : DbManager
{
    public RetailerDb() : base("DBConnection")
    {
    }

    public Table<DataModel.Margin> Margin
    {
        get { return GetTable<DataModel.Margin>(); }
    }
}
4

1 に答える 1

2

これまでのところ、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);
        }
    });
}

InsertOrUpdateクラスには使用法はありませんが、Personクラスには使用法があることがわかりますPatient。したがって、このメソッドは、Identityフィールド以外を渡す場合にのみサポートされます。

注: InsertOrUpdate廃止されましたが、プロジェクトのソースコードのテストで引き続き使用されています。それでも、影響はないはずです。と考えてInsertOrReplaceください。

于 2012-12-14T10:54:00.793 に答える