本文整理汇总了C#中System.Data.DbContext.AssignStub方法的典型用法代码示例。如果您正苦于以下问题:C# DbContext.AssignStub方法的具体用法?C# DbContext.AssignStub怎么用?C# DbContext.AssignStub使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Data.DbContext
的用法示例。
在下文中一共展示了DbContext.AssignStub方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: AssignStubx
internal static void AssignStubx(object poco, DbContext db)
{
IEnumerable<PropertyMapping> piNeedStub = Mapper.StubsNeeded(poco.GetType());
foreach (PropertyMapping pm in piNeedStub)
{
// pm.PropertyPoco.First(). e.g. Customer of Customer.CustomerId. Customer is PropertyPoco[0], CustomerId is PropertyPoco[1]
PropertyInfo pocoForeign = poco.GetType().GetProperty(pm.PropertyPoco[0], BindingFlags.Public | BindingFlags.Instance);
if (pocoForeign == null) continue;
object val = pocoForeign.GetValue(poco, null);
if (val != null)
{
PropertyInfo pocoForeignId = pocoForeign.PropertyType.GetProperty(pm.PropertyPoco.Last(), BindingFlags.Public | BindingFlags.Instance);
object id = pocoForeignId.GetValue(val, null);
pocoForeign.SetValue(poco, LoadStub(pocoForeign.PropertyType, pm.PropertyPoco.Last(), id, db), null);
}
else
{
// foreign key is null'd
// nullable foreign key association
// http://www.codetuning.net/blog/post/Understanding-Entity-Framework-Associations.aspx
var dummy = pocoForeign.GetValue(poco, null); // work-around
pocoForeign.SetValue(poco, val, null);
}
}
IEnumerable<PropertyInfo> piCollection =
poco.GetType().GetProperties()
.Where(x => x.PropertyType.IsGenericType && typeof(IEnumerable).IsAssignableFrom(x.PropertyType));
foreach (PropertyInfo item in piCollection)
{
PropertyInfo px = poco.GetType().GetProperty(item.Name, BindingFlags.Public | BindingFlags.Instance);
// same property exists from dto to poco
if (px != null)
{
IList col = (IList)px.GetValue(poco, null);
if (col == null) continue;
Type dtoType = item.PropertyType.GetInterfaces().Where(x => x.IsGenericType && x.GetGenericTypeDefinition() == typeof(ICollection<>)).Single().GetGenericArguments()[0];
foreach (object elem in col)
{
db.AssignStub(elem);
}
}
}
}