2012-11-30 46 views
0

我想最簡單的做法項目 - 從數據庫中提取數據。當我進行查詢並稍後調用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; } 
} 

有什麼建議嗎?

回答

0

我想通了。關聯更改聲明後Flashcard->類別一切工作正常。正確的代碼在這裏:

private EntityRef<Category> _categoryRef = new EntityRef<Category>(); 
[Association(ThisKey = "CatId", OtherKey = "CategoryId", IsForeignKey = true, DeleteOnNull = true, Storage = "_categoryRef")] 
public Category Category 
{ 
    get { return _categoryRef.Entity; } 
    set 
    { 
     if (_categoryRef.Entity != value || !_categoryRef.HasLoadedOrAssignedValue) 
     { 
      _categoryRef.Entity = value; 
     } 
    } 
} 
0

確保你告訴LINQ的你想要做的表有什麼分別

FlashcardsTable.DeleteAllOnSubmit(list); 
    SubmitChanges(); 

這是一個簡單的方法來刪除它們

+0

那麼問題是,他的數據正在從數據庫中刪除 – Habib

0

我99.999999%肯定這是一個的情況下,不正確的診斷。

也許你的數據真的從數據庫中刪除,但我相信它是不是這段代碼的原因。向我們展示真實的代碼(在這個例子中,你沒有datacontext,所以它是一種簡化..)

你可以剖析生成的sql來檢查是否有刪除。如果是這樣,我會很高興爲你的問題添加賞金。

+0

嗨。感謝您的重播。代碼是真實的。沒有datacontext,因爲它是從DataContext繼承的類的一部分(SubmitChanges()== this.SubmitChanges())。我看着生成的SQL。在第一次調用SubmitChanges()之後,沒有生成SQL。第二次SubmitChanges()後生成的命令,其中刪除對象od表Flashcard ... – lgi

+0

嗯,我想你應該閱讀如何使用Linq2Sql,因爲我不認爲你是在正確的軌道上。我從來沒有見過從DataContext繼承的類來做CRUD。你應該爲一個工作單元實時創建它們...... – Pleun

+0

我創建了一個類,它使得所有的數據庫人員和我從DataContext繼承它,因爲它在這種情況下是簡單的解決方案(我不需要始終創建DataContext對象,但仍然在DataContext的一個實例上工作 - 是不好主意嗎?)。我認爲這篇文章是初學者常見的錯誤,但現在我發現它並不是那麼明顯,我必須更努力地找到解決方案......感謝回覆 – lgi