Db4O - 我可以保存字符串吗?
我有以下代码:
Assert.IsTrue(Repository.FindAll<string>().Count() == 0);
string newString = "New String";
Repository.Save(newString);
Assert.IsTrue(Repository.FindAll<string>().Count() == 1);
但它失败了。我想这与我保存字符串有关。
我的 Save() 代码是这样的:
public void Save<T>(T obj)
{
if (obj == null)
throw new ArgumentNullException("obj not allowed to be null");
Db.Store(obj);
Db.Commit();
}
我的持久类应该有一些特别的东西吗?或者我可以用 db4o 保存几乎所有东西?
I have the following code:
Assert.IsTrue(Repository.FindAll<string>().Count() == 0);
string newString = "New String";
Repository.Save(newString);
Assert.IsTrue(Repository.FindAll<string>().Count() == 1);
But it is failing. I suppose it has something to do with the fact that I'm saving a string.
My Save() code is this:
public void Save<T>(T obj)
{
if (obj == null)
throw new ArgumentNullException("obj not allowed to be null");
Db.Store(obj);
Db.Commit();
}
Should my persistent classes have something special? Or I can just save pretty much anything with db4o?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
据我所知,平台基元,即使它们可能是引用类型(如 System.String),也只有在实例是另一个对象的子对象(即在字段中引用)时才会被存储,然后该对象才会被存储。因此,在 C# 或 Java 中对原语调用 Store() 不会保存它。基本上,您需要创建一个实体来引用您的字符串才能保存它们;与您需要创建表以将字符串保存到 RDBMS 的方式大致相同。
编辑:
或者更好
As far as I know, platform primitives, even though they may be reference types (like System.String), will only be stored if an instance is a child of another object (i.e. referenced in a field) and then that object is stored. Therefore, calling Store() on a primitive in C# or Java will not save it. Basically, you'll need to create an entity to reference your strings in order to save them; much the same way as you'd need to create a table in order to save strings to an RDBMS.
EDIT:
or better yet