0

我想做最简单的事情——从数据库中获取数据。当我进行查询并稍后在 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; }
}

有什么建议么?

4

3 回答 3

0

确保你告诉 Linq 你想分别对表格做什么

FlashcardsTable.DeleteAllOnSubmit(list);
    SubmitChanges();

这是删除所有这些的简单方法

于 2012-11-30T12:04:52.987 回答
0

我 99.999999% 确信这是一个错误诊断的案例。

也许您的数据确实已从数据库中删除,但我相信这不是因为这段代码。向我们展示真实的代码(在这个例子中你没有数据上下文,所以它是某种简化..)

您可以分析生成的 sql 以检查其中是否有删除。如果是这样,我将非常乐意为您的问题添加赏金。

于 2012-11-30T19:50:18.370 回答
0

I figured it out. After change declaration of association Flashcard->Category everything works fine. Right code is here:

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;
        }
    }
}
于 2012-12-02T17:29:04.227 回答