private void GenerateClrTypes(Uri serviceUri) { var contextGenerator = new DataServiceContextGenerator(serviceUri); foreach (EdmTableProvider table in Tables) { table.GenerateEntitySet(contextGenerator); _entityTypeToClrType[table.EdmEntityType] = table.EntityType; } foreach (ComplexType complexType in _complexTypes) { var clrType = contextGenerator.AddComplexType(complexType.Name); _entityTypeToClrType[complexType] = clrType; } foreach (EdmTableProvider table in Tables) { table.GenerateProperties(contextGenerator); } foreach (ComplexType complexType in _complexTypes) { foreach (EdmProperty prop in complexType.Properties) { Type propertyType = ((PrimitiveType)prop.TypeUsage.EdmType).ClrEquivalentType; if (propertyType.IsValueType) { propertyType = typeof(Nullable<>).MakeGenericType(propertyType); } contextGenerator.AddColumnProperty( complexType.Name, propertyType, prop.Name); } } Dictionary<Type, Type> typeBuilderToRealTypeMapping = contextGenerator.GenerateContextType(); ContextType = contextGenerator.GeneratedType; // Fix up the types to now be the real ones foreach (EdmTableProvider table in Tables) { Type realEntityType = typeBuilderToRealTypeMapping[table.EntityType]; _entityTypeToClrType[table.EdmEntityType] = realEntityType; table.SetEntityType(realEntityType); foreach (EdmColumnProvider column in table.Columns) { column.ResetColumnType(); } } ContextFactory = () => Activator.CreateInstance(ContextType); }