private object InstantiateEntity( EntityType entityType, DbContext context, Type clrType, DbSet set) { object structuralObject; if (!clrType.IsAbstract()) { structuralObject = set.Create(); } else { EntityType entityType1 = this._metadataWorkspace.GetItems <EntityType>(DataSpace.CSpace).First <EntityType>((Func <EntityType, bool>)(et => { if (entityType.IsAncestorOf(et)) { return(!et.Abstract); } return(false); })); structuralObject = context.Set(EntityTypeExtensions.GetClrType(entityType1)).Create(); } ModificationCommandTreeGenerator.InstantiateComplexProperties(structuralObject, (IEnumerable <EdmProperty>)entityType.Properties); return(structuralObject); }
private static void InstantiateComplexProperties( object structuralObject, IEnumerable <EdmProperty> properties) { foreach (EdmProperty property in properties) { if (property.IsComplexType) { PropertyInfo clrPropertyInfo = property.GetClrPropertyInfo(); object instance = Activator.CreateInstance(clrPropertyInfo.PropertyType); ModificationCommandTreeGenerator.InstantiateComplexProperties(instance, (IEnumerable <EdmProperty>)property.ComplexType.Properties); clrPropertyInfo.GetPropertyInfoForSet().SetValue(structuralObject, instance, (object[])null); } } }