我有一种不寻常的情况,我们正在使用EF从现有的数据存储迁移到Sql。对于一个实体,我具有类似于以下的属性:
public class User : Entity
{
[NotMapped] //this is the original collection from old data store
public IList<Foo> Things
{
get { return _things.ToList().AsReadonly(); }
set { _things = value.ToList(); }
}
//this is the new ef presentation of the original collection
public ICollection<Foo> TheThings
{
get { return _things; }
set { _things = value.ToList(); }
}
[NotMapped]
//this is the original collection from old data store,
//but just a List<string>
public IList<string> OtherThings
{
get { return _otherThings.AsReadonly(); }
set { _things = _otherThings.ToList(); }
}
//this is the new ef presentation of the original collection,
//I can't store a List<string> in sql so I have to wrap it in a class
public ICollection<ThingWrapper> TheOtherThings
{
get { return _otherThings.Select(x => new ThingWrapper(x)).ToList(); }
set { _otherThings= value.Select(x => x.Value).ToList(); }
}
}
public class ThingWrapper : Entity
{
public ThingWrapper(string value) { this.Value = value; }
public string Value { get; set; }
public virtual User User { get; set; }
}
public class Foo : Entity
{
public SomeOtherComplexType AThing { get; set; }
public virtual User User { get; set; }
}
var user = DbSet<User>.Include(x => x.TheThings).Include(x => x.TheOtherThings).SingleOrDefault(x => x.Id == someId);
在上面的示例中,我最终得到的用户对象的“ TheThings”集合正确填充,但“ TheOtherThings”集合为空。因此Foo类可以正常返回,而ThingWrapper类则不能。
我的问题是为什么?