我想做最简单的事情——从数据库中获取数据。当我进行查询并稍后在 DataContext 对象上调用 SubmitChanges() 时,所有获取的项目都会从数据库中删除。这是代码:
SubmitChanges(); // nothing happened (it is OK)
List<Flashcard> list = (from f in FlashcardsTable
where f.Category.IsSelected
&& !excluded.Contains(f.FlashcardId)
select f).ToList();
SubmitChanges(); // All items from list are deleting from DB!!
FlashcardsTable 是 DB 中的一个表,excluded 只是一个整数列表。
我的问题是为什么在调用 SubmitChanges() 时会删除数据库中的数据以及如何修复它(我想将数据留在数据库中)。
我仍在寻找我的问题的答案...这是我的代码中的更多详细信息,也许有帮助?
我在我的应用程序中做了两件事。首先创建 100 个项目并将它们放入 DB:
var db = new DbContext();
var flashcard = new Flashcard();
// here sets some fields on flashcard, not important...
for (var i = 0; i < 100; i++)
{
var cat = db.CategoriesTable.First(c => c.CategoryId == categoryId);
cat.Count++;
flashcard.Category = cat;
flashcard.BasketNr = 1;
flashcard.TimeToCheck = Utils.CurrentDate.AddHours(12);
db.FlashcardsTable.InsertOnSubmit(new Flashcard(flashcard));
}
db.SubmitChanges();
接下来我对 DB 进行查询:
var db = new DbContext();
db.SubmitChanges(); // here is OK, no SQL was generated
var a = (from f in db.FlashcardsTable
where f.Category.IsSelected
select f).ToList();
db.SubmitChanges(); // here are generated 100 SQLs deleting my data
调用第二个 db.SubmitChanges() 后会生成 100 个 SQL,例如:
DELETE FROM [Flashcard] WHERE [FlashcardId] = @p0
-- @p0: Input Int32 (Size = 4; Prec = 0; Scale = 0) [1]
-- Context: SqlProvider(SqlCE) Model: AttributedMetaModel Build: System.Data.Linq, Version=7.0.0.0, Culture=neutral, PublicKeyToken=24EEC0D8C86CDA1E
也许表格的定义是错误的?这里是:
[Table]
public class Category : INotifyPropertyChanged {
public event PropertyChangedEventHandler PropertyChanged;
private string name;
[Column(IsPrimaryKey = true, IsDbGenerated = true, DbType = "INT NOT NULL Identity", CanBeNull = false, AutoSync = AutoSync.OnInsert)]
public int CategoryId { get; set; }
[Column(UpdateCheck = UpdateCheck.Never)]
public string Name {
get { return name; }
set {
if (name != value) {
name = value;
if (PropertyChanged != null) {
PropertyChanged(this, new PropertyChangedEventArgs("Name"));
}
}
}
}
[Column(UpdateCheck = UpdateCheck.Never)]
public int Count { get; set; }
[Column(UpdateCheck = UpdateCheck.Never)]
public bool IsSelected { get; set; }
}
[Table]
public class Flashcard {
public const int TYPE_TEXT = 1;
public const int TYPE_BITMAP = 2;
[Column(IsPrimaryKey = true, IsDbGenerated = true, DbType = "INT NOT NULL Identity", CanBeNull = false, AutoSync = AutoSync.OnInsert)]
public int FlashcardId { get; set; }
[Column(UpdateCheck = UpdateCheck.Never)]
public DateTime TimeToCheck { get; set; }
[Column(CanBeNull = false, UpdateCheck = UpdateCheck.Never)]
public int Type { get; set; }
private byte[] firstPageBitmap;
private byte[] secondPageBitmap;
private string firstPageText;
private string secondPageText;
public Flashcard(Flashcard flashcard) {
FirstPageBitmap = flashcard.FirstPageBitmap;
SecondPageBitmap = flashcard.SecondPageBitmap;
FirstPageType = flashcard.FirstPageType;
SecondPageType = flashcard.SecondPageType;
BasketNr = flashcard.BasketNr;
Category = flashcard.Category;
TimeToCheck = flashcard.TimeToCheck;
}
public Flashcard() {
}
[Column(DbType = "image", UpdateCheck = UpdateCheck.Never, CanBeNull = true)]
public byte[] FirstPageBitmap {
get { return firstPageBitmap; }
set {
FirstPageType = TYPE_BITMAP;
firstPageBitmap = value;
}
}
[Column(CanBeNull = true, UpdateCheck = UpdateCheck.Never)]
public string FirstPageText {
get { return firstPageText; }
set {
FirstPageType = TYPE_TEXT;
firstPageText = value;
}
}
[Column(UpdateCheck = UpdateCheck.Never)]
public int FirstPageType { get; set; }
[Column(UpdateCheck = UpdateCheck.Never)]
public int SecondPageType { get; set; }
[Column(DbType = "image", UpdateCheck = UpdateCheck.Never, CanBeNull = true)]
public byte[] SecondPageBitmap {
get { return secondPageBitmap; }
set {
SecondPageType = TYPE_BITMAP;
secondPageBitmap = value;
}
}
[Column(CanBeNull = true, UpdateCheck = UpdateCheck.Never)]
public string SecondPageText {
get { return secondPageText; }
set {
SecondPageType = TYPE_TEXT;
secondPageText = value;
}
}
[Column(UpdateCheck = UpdateCheck.Never)]
public int BasketNr { get; set; }
[Column(UpdateCheck = UpdateCheck.Never)]
public int CatId { get; set; }
[Association(ThisKey = "CatId", OtherKey = "CategoryId", IsForeignKey = true, DeleteOnNull = true)]
public Category Category { get; set; }
}
有什么建议么?