【问题标题】:Realm deleting valid object raises "Attempted to access detached row" error领域删除有效对象引发“尝试访问分离行”错误
【发布时间】:2018-12-07 23:12:13
【问题描述】:

我正在尝试从 winForm dotnet 应用程序中的领域数据库中删除对象,但我的代码给了我这个错误

抛出异常:'Realms.Exceptions.RealmInvalidObjectException' in Realm.dll 类型异常 在 Realm.dll 中发生“Realms.Exceptions.RealmInvalidObjectException” 但未在用户代码中处理尝试访问分离行

Realm realm;
IQueryable<RObj> items;

public Form1()
    {
        InitializeComponent();
        var config = new RealmConfiguration("DB.realm");
        config.ShouldDeleteIfMigrationNeeded = true;
        realm = Realm.GetInstance(config);
        //WriteRealmTest();
    }

private void Form1_Load(object sender, EventArgs e)
    {
        items = realm.All<RObj>();

        listboxMain.DisplayMember = "title";
        listboxMain.ValueMember = "id";


        BindingSource bs = new BindingSource();
        bs.DataSource = items;
        listboxMain.DataSource = bs;

    }

private void listboxMain_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Delete)
        {
            int index = listboxMain.SelectedIndex;
            if (index < 0) { return; }
            RObj item = items.ElementAt(index);
            if (!item.IsValid)
                return;
            using (var trans = realm.BeginWrite())
            {
                realm.Remove(item);
                trans.Commit();            //when this line is commented, no error is raised
            }

        }
    }

【问题讨论】:

    标签: c# .net winforms realm


    【解决方案1】:

    我发现了问题,这是供遇到同样问题的人将来参考

    我必须先从 BindingSource 中删除该项目,然后再从领域数据库中删除它

    using (var trans = realm.BeginWrite()) 之前添加bs.RemoveAt(index) 解决了这个问题

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-21
      • 1970-01-01
      • 1970-01-01
      • 2015-10-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多