gpt4 book ai didi

c# - BLToolkit.3中InsertOrReplace的使用方法

转载 作者:行者123 更新时间:2023-11-30 17:08:18 25 4
gpt4 key购买 nike

我开始使用 BLToolkit 并且有一个新的优势:InsertOrReplace当我尝试使用它时出现异常:“InsertOrUpdate 方法不支持身份字段‘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

关于c# - BLToolkit.3中InsertOrReplace的使用方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13876811/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com