更新领域条目的属性

时间:2018-10-28 14:33:59

标签: c# .net xamarin xamarin.ios realm

我在Xamarin项目中使用Realm数据库

我的模型具有领域对象

public class UserModel: RealmObject
{
    public string Id { get; set;}
    public string Email { get; set; }
    public string Password { get; set; }
    public byte[] UserAvatar { get; set; }
    public string ApiKey { get; set; }
    public string Name { get; set; }
    public string Surname { get; set; }
    public string Birthday { get; set; }
    public int Country_id { get; set; }
    public bool IsAuthorized { get; set; }
    public string Base64Avatar { get; set; }
    public string Telephone { get; set; }
}

我需要更新Name属性。

我该怎么做

var realm = Realm.GetInstance();
var user_check = realm.All<UserModel>().First(); 
user_check.Name = "Test"

并得到此错误

enter image description here

我该如何解决?

1 个答案:

答案 0 :(得分:3)

添加/更新/删除对象必须在事务内完成,最简单的方法是将其包装在Write方法中。

realm.Write(() => 
{
    user_check.Name = "Test";
});

有关更多信息,请检查Rleam Write docs