我将 IUser 对象存储在 Db4o 数据库中,如下所示(客户端是 IObjectClient):
public Guid AddUser(IUser user)
{
lock (userLock)
{
user.Id = Guid.NewGuid();
client.Store(user);
client.Commit();
}
return user.Id;
}
此处使用的 IUser 实现具有 IUser 中描述的字段,该字段IEnumerable<Roles> Roles { get;}
使用支持字段实现IList<Roles> = new List<Roles>()
当我运行以下 UnitTest 时,一切正常:
[TestMethod]
public void UserStaysInRoleAfterServerRestart()
{
string filePath = IOHelper.MakeAbsolute(ConfigurationManager.AppSettings["userRepository.path.db4o"] + "1");
IUser expected = GenerateTestUser();
expected.AddRole(Roles.Anonymous);
IObjectServer userServer1 = Db4oFactory.OpenServer(filePath, 0);
IUserRepository repo = new UserRepository(userServer1);
repo.AddUser(expected);
userServer1.Close();
IObjectServer userServer2 = Db4oFactory.OpenServer(filePath, 0);
IUserRepository repo2 = new UserRepository(userServer2);
IUser actual = repo2.GetUser(expected.Id);
Assert.IsTrue(actual.IsInRole(Roles.Anonymous));
}
但是,当我在我的网站中使用相同的方法(AddUser 和 GetUser)并由“重新启动调试”分隔时,GetUser() 会正确返回 IUser,但Roles.Count() == 0