私は最も簡単なことをしたいと思います-dbからデータをフェッチします。クエリを実行し、後でDataContextオブジェクトに対してSubmitChanges()を呼び出すと、フェッチされたすべてのアイテムがDBから削除されます。コードは次のとおりです。
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()が呼び出されたときにDBからデータが削除される理由と、それを修正する方法です(データベースにデータを残したい)。
私はまだ私の質問に対する答えを探しています...ここに私のコードからの詳細があります、多分それは助けになりますか?
私は自分のアプリで2つのことをします。最初に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
2番目の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; }
}
助言がありますか?