У меня есть необычная ситуация, когда мы переходим из существующего хранилища данных в Sql, используя EF.Для одной сущности у меня есть свойства, подобные этому:
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 - нет.
Мой вопрос - ПОЧЕМУ?