通过EF更新时,我无法添加子实体。
public virtual void TestUpdate(object item)
{
var props = item.GetType().GetRuntimeProperties().Where(x => (x.PropertyType.FullName.StartsWith("MIRRA"))||(x.PropertyType.FullName.StartsWith("System.Collections.Generic.ICollection"))).Select(m => m).ToList();
foreach (var prop in props)
{
object value = prop.GetValue(item);
if (value is IEnumerable)
{
foreach (var listitem in value as IEnumerable)
{
TestUpdate(listitem);
}
}else if (value != null)
{
root = value;
TestUpdate(value);
}
}
int id = (int)item.GetType().GetProperties().FirstOrDefault().GetValue(item);
if (id == 0)
{
context.Entry(item).State = EntityState.Added;
//add new child entity to context
}
else
{
context.Entry(item).State = EntityState.Modified;
}
}
最后我保存了这个上下文,但上下文没有添加子实体