我想最簡單的做法項目 - 從數據庫中提取數據。當我進行查詢並稍後調用DataContext對象上的SubmitChanges()時,所有提取的項目都將從數據庫中刪除。代碼如下:WP7和LINQ到SQL:query.toList後的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中的一個表格,排除的只是一個帶整數的列表。
我的問題是,爲什麼從數據庫數據時的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查詢:產生100個SQLS等)
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後(:
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; }
}
有什麼建議嗎?
那麼問題是,他的數據正在從數據庫中刪除 – Habib