据我所知,平台原语,即使它们可能是引用类型(如system.string),也只会在实例是另一个对象(即在字段中引用)的子对象,然后存储该对象的情况下存储。因此,调用C或Java中的原语调用StureE()不能够保存它。基本上,为了保存字符串,您需要创建一个引用字符串的实体;这与您需要创建一个表以便将字符串保存到RDBMS的方式非常相似。
public class StringEntity
{
private readonly string value;
public StringEntity(string value)
: base()
{
this.value = value;
}
}
...
Assert.IsTrue(Repository.FindAll<StringEntity>().Count() == 0);
Repository.Save(new StringEntity("New String"));
Assert.IsTrue(Repository.FindAll<StringEntity>().Count() == 1);
编辑:
或者更好
public class Primitive<T>
{
private readonly T value;
public Primitive(T value)
: base()
{
this.value = value;
}
}
...
Assert.IsTrue(Repository.FindAll<Primitive<string>>().Count() == 0);
Repository.Save(new Primitive<string>("New String"));
Assert.IsTrue(Repository.FindAll<Primitive<string>>().Count() == 1);