private IEnumerable <TCommandTree> GenerateAssociation <TCommandTree>(string associationIdentity, EntityState state) where TCommandTree : DbCommandTree { DebugCheck.NotEmpty(associationIdentity); var associationType = _metadataWorkspace .GetItem <AssociationType>(associationIdentity, DataSpace.CSpace); using (var context = CreateContext()) { var sourceEntityType = associationType.SourceEnd.GetEntityType(); var sourceSet = context.Set(sourceEntityType.GetClrType()); var sourceEntity = sourceSet.Create(); InstantiateNullableKeys(sourceEntity, sourceEntityType); InstantiateComplexProperties(sourceEntity, sourceEntityType.Properties); sourceSet.Attach(sourceEntity); var targetEntityType = associationType.TargetEnd.GetEntityType(); var targetSet = context.Set(targetEntityType.GetClrType()); var targetEntity = targetSet.Create(); InstantiateNullableKeys(targetEntity, targetEntityType); InstantiateComplexProperties(targetEntity, targetEntityType.Properties); targetSet.Attach(targetEntity); var objectStateManager = ((IObjectContextAdapter)context) .ObjectContext .ObjectStateManager; objectStateManager .ChangeRelationshipState( sourceEntity, targetEntity, associationType.FullName, associationType.TargetEnd.Name, state == EntityState.Deleted ? state : EntityState.Added ); using (var commandTracer = new CommandTracer(context)) { context.SaveChanges(); foreach (var commandTree in commandTracer.CommandTrees) { yield return((TCommandTree)commandTree); } } } }
/// <summary> /// Retrieves a mapping to CLR type for the given EDM type. Assumes the MetadataWorkspace has no /// </summary> internal static ObjectTypeMapping GetObjectMapping(EdmType type, MetadataWorkspace workspace) { // Check if the workspace has cspace item collection registered with it. If not, then its a case // of public materializer trying to create objects from PODR or EntityDataReader with no context. ItemCollection collection; if (workspace.TryGetItemCollection(DataSpace.CSpace, out collection)) { return (ObjectTypeMapping)workspace.GetMap(type, DataSpace.OCSpace); } else { EdmType ospaceType; EdmType cspaceType; // If its a case of EntityDataReader with no context, the typeUsage which is passed in must contain // a cspace type. We need to look up an OSpace type in the ospace item collection and then create // ocMapping if (type.DataSpace == DataSpace.CSpace) { // if its a primitive type, then the names will be different for CSpace type and OSpace type if (Helper.IsPrimitiveType(type)) { ospaceType = workspace.GetMappedPrimitiveType(((PrimitiveType)type).PrimitiveTypeKind, DataSpace.OSpace); } else { // Metadata will throw if there is no item with this identity present. // Is this exception fine or does object materializer code wants to wrap and throw a new exception ospaceType = workspace.GetItem<EdmType>(type.FullName, DataSpace.OSpace); } cspaceType = type; } else { // In case of PODR, there is no cspace at all. We must create a fake ocmapping, with ospace types // on both the ends ospaceType = type; cspaceType = type; } // This condition must be hit only when someone is trying to materialize a legacy data reader and we // don't have the CSpace metadata. if (!Helper.IsPrimitiveType(ospaceType) && !Helper.IsEntityType(ospaceType) && !Helper.IsComplexType(ospaceType)) { throw EntityUtil.MaterializerUnsupportedType(); } ObjectTypeMapping typeMapping; if (Helper.IsPrimitiveType(ospaceType)) { typeMapping = new ObjectTypeMapping(ospaceType, cspaceType); } else { typeMapping = DefaultObjectMappingItemCollection.LoadObjectMapping(cspaceType, ospaceType, null); } return typeMapping; } }
private static Dictionary <string, string> GetMappings(MetadataWorkspace workspace, string containerName, string entityName) { var mappings = new Dictionary <string, string>(); var storageMapping = workspace.GetItem <GlobalItem>(containerName, DataSpace.CSSpace); dynamic entitySetMaps = storageMapping.GetType().InvokeMember("EntitySetMaps", BindingFlags.GetProperty | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance, null, storageMapping, null); foreach (var entitySetMap in entitySetMaps) { var typeMappings = GetArrayList("TypeMappings", entitySetMap); dynamic typeMapping = typeMappings[0]; dynamic types = GetArrayList("Types", typeMapping); if (types[0].Name == entityName) { var fragments = GetArrayList("MappingFragments", typeMapping); var fragment = fragments[0]; var properties = GetArrayList("AllProperties", fragment); foreach (var property in properties) { var edmProperty = GetProperty("Property", property); var columnProperty = GetProperty("Column", property); mappings.Add(edmProperty.Name, columnProperty.Name); } } } return(mappings); }
internal static ObjectTypeMapping GetObjectMapping( EdmType type, MetadataWorkspace workspace) { ItemCollection collection; if (workspace.TryGetItemCollection(DataSpace.CSpace, out collection)) { return((ObjectTypeMapping)workspace.GetMap((GlobalItem)type, DataSpace.OCSpace)); } EdmType edmType; EdmType cdmType; if (type.DataSpace == DataSpace.CSpace) { edmType = !Helper.IsPrimitiveType(type) ? workspace.GetItem <EdmType>(type.FullName, DataSpace.OSpace) : (EdmType)workspace.GetMappedPrimitiveType(((PrimitiveType)type).PrimitiveTypeKind, DataSpace.OSpace); cdmType = type; } else { edmType = type; cdmType = type; } if (!Helper.IsPrimitiveType(edmType) && !Helper.IsEntityType(edmType) && !Helper.IsComplexType(edmType)) { throw new NotSupportedException(Strings.Materializer_UnsupportedType); } return(!Helper.IsPrimitiveType(edmType) ? DefaultObjectMappingItemCollection.LoadObjectMapping(cdmType, edmType, (DefaultObjectMappingItemCollection)null) : new ObjectTypeMapping(edmType, cdmType)); }
private EntityType GetTableEntityType(Type DBType) { ObjectContext objContext = ((IObjectContextAdapter)DB).ObjectContext; MetadataWorkspace workspace = objContext.MetadataWorkspace; EntityType table = workspace.GetEdmSpaceType((StructuralType)workspace.GetItem <EntityType>(DBType.FullName, DataSpace.OSpace)) as EntityType; return(table); }
public static AssociationType ForeignKeyForNavigationProperty(this DbContext context, Type type, PropertyInfo navigationProperty) { MetadataWorkspace metadata = ((IObjectContextAdapter)context).ObjectContext.MetadataWorkspace; ObjectItemCollection objectItemCollection = metadata.GetItemCollection(DataSpace.OSpace) as ObjectItemCollection; EntityType entityType = metadata.GetItems <EntityType>(DataSpace.OSpace).SingleOrDefault(e => objectItemCollection.GetClrType(e) == GetBaseTypeFor(type)); EntitySet entitySet = metadata.GetItems <EntityContainer>(DataSpace.CSpace).Single().EntitySets.Single(s => s.ElementType.Name == entityType.Name); EntitySetMapping mapping = metadata.GetItems <EntityContainerMapping>(DataSpace.CSSpace).Single().EntitySetMappings.Single(s => s.EntitySet == entitySet); string entityIdentity = mapping.EntityTypeMappings.First().EntityType.ToString(); entityType = metadata.GetItem <EntityType>(entityIdentity, DataSpace.CSpace); return(entityType.NavigationProperties.Single(p => p.Name.Equals(navigationProperty.Name)).ToEndMember.DeclaringType as AssociationType); }
private static Type GetClrType( MetadataWorkspace workspace, StructuralType edmType) { StructuralType oSpaceType = workspace .GetItem <StructuralType>(edmType.FullName, DataSpace.OSpace); ObjectItemCollection objectItems = workspace.GetItemCollection(DataSpace.OSpace) as ObjectItemCollection; return(objectItems.GetClrType(oSpaceType)); }
public static PropertyEntryMetadata ValidateNameAndGetMetadata( InternalContext internalContext, Type declaringType, Type requestedType, string propertyName) { Type type; DbHelpers.GetPropertyTypes(declaringType).TryGetValue(propertyName, out type); MetadataWorkspace metadataWorkspace = internalContext.ObjectContext.MetadataWorkspace; StructuralType structuralType = metadataWorkspace.GetItem <StructuralType>(declaringType.FullNameWithNesting(), DataSpace.OSpace); bool isMapped = false; bool isComplex = false; EdmMember edmMember; structuralType.Members.TryGetValue(propertyName, false, out edmMember); if (edmMember != null) { EdmProperty edmProperty = edmMember as EdmProperty; if (edmProperty == null) { return((PropertyEntryMetadata)null); } if (type == (Type)null) { PrimitiveType edmType = edmProperty.TypeUsage.EdmType as PrimitiveType; type = edmType == null ? ((ObjectItemCollection)metadataWorkspace.GetItemCollection(DataSpace.OSpace)).GetClrType((StructuralType)edmProperty.TypeUsage.EdmType) : edmType.ClrEquivalentType; } isMapped = true; isComplex = edmProperty.TypeUsage.EdmType.BuiltInTypeKind == BuiltInTypeKind.ComplexType; } else { IDictionary <string, Func <object, object> > propertyGetters = DbHelpers.GetPropertyGetters(declaringType); IDictionary <string, Action <object, object> > propertySetters = DbHelpers.GetPropertySetters(declaringType); if (!propertyGetters.ContainsKey(propertyName) && !propertySetters.ContainsKey(propertyName)) { return((PropertyEntryMetadata)null); } } if (!requestedType.IsAssignableFrom(type)) { throw Error.DbEntityEntry_WrongGenericForProp((object)propertyName, (object)declaringType.Name, (object)requestedType.Name, (object)type.Name); } return(new PropertyEntryMetadata(declaringType, type, propertyName, isMapped, isComplex)); }
// <summary> // Retrieves a mapping to CLR type for the given EDM type. Assumes the MetadataWorkspace has no // </summary> internal static ObjectTypeMapping GetObjectMapping(EdmType type, MetadataWorkspace workspace) { // Check if the workspace has cspace item collection registered with it. If not, then its a case // of public materializer trying to create objects from PODR or EntityDataReader with no context. ItemCollection collection; if (workspace.TryGetItemCollection(DataSpace.CSpace, out collection)) { return((ObjectTypeMapping)workspace.GetMap(type, DataSpace.OCSpace)); } else { EdmType ospaceType; EdmType cspaceType; // If its a case of EntityDataReader with no context, the typeUsage which is passed in must contain // a cspace type. We need to look up an OSpace type in the ospace item collection and then create // ocMapping if (type.DataSpace == DataSpace.CSpace) { // if its a primitive type, then the names will be different for CSpace type and OSpace type if (Helper.IsPrimitiveType(type)) { ospaceType = workspace.GetMappedPrimitiveType(((PrimitiveType)type).PrimitiveTypeKind, DataSpace.OSpace); } else { // Metadata will throw if there is no item with this identity present. // Is this exception fine or does object materializer code wants to wrap and throw a new exception ospaceType = workspace.GetItem <EdmType>(type.FullName, DataSpace.OSpace); } cspaceType = type; } else { // In case of PODR, there is no cspace at all. We must create a fake ocmapping, with ospace types // on both the ends ospaceType = type; cspaceType = type; } // This condition must be hit only when someone is trying to materialize a legacy data reader and we // don't have the CSpace metadata. if (!Helper.IsPrimitiveType(ospaceType) && !Helper.IsEntityType(ospaceType) && !Helper.IsComplexType(ospaceType)) { throw new NotSupportedException(Strings.Materializer_UnsupportedType); } ObjectTypeMapping typeMapping; if (Helper.IsPrimitiveType(ospaceType)) { typeMapping = new ObjectTypeMapping(ospaceType, cspaceType); } else { typeMapping = DefaultObjectMappingItemCollection.LoadObjectMapping(cspaceType, ospaceType, null); } return(typeMapping); } }
public WSJDE() : base("name=WSJDE") { ObjectContext context = (this as IObjectContextAdapter).ObjectContext; string environment = ConfigurationManager.AppSettings.Get("Environment"); const string devCTL = "TESTCTL"; const string devDTA = "TESTDTA"; const string qaCTL = "CRPCTL"; const string qaDTA = "CRPDTA"; const string prodCTL = "PRODCTL"; const string prodDTA = "PRODDTA"; var x = Assembly.GetExecutingAssembly().GetManifestResourceStream("WSJDEData.WSJDE.ssdl"); XmlReader[] sReaders = new XmlReader[] { XmlReader.Create(Assembly.GetExecutingAssembly().GetManifestResourceStream("WSJDEData.WSJDE.ssdl")) }; XmlReader[] mReaders = new XmlReader[] { XmlReader.Create(Assembly.GetExecutingAssembly().GetManifestResourceStream("WSJDEData.WSJDE.msl")) }; StoreItemCollection sCollection = new StoreItemCollection(sReaders); ObjectContext objContext = ((IObjectContextAdapter)context).ObjectContext; MetadataWorkspace workspace = objContext.MetadataWorkspace; EdmItemCollection cCollection = workspace.GetItemCollection(DataSpace.CSpace) as EdmItemCollection; StorageMappingItemCollection csCollection = new StorageMappingItemCollection(cCollection, sCollection, mReaders); workspace.RegisterItemCollection(sCollection); workspace.RegisterItemCollection(csCollection); EntityContainer container = workspace.GetItem <EntityContainer>("WSJDEModelStoreContainer", DataSpace.SSpace); foreach (EntitySetBase entitySetBase in container.BaseEntitySets) { string schema = entitySetBase.Schema; if (schema != null) { string name = schema.Substring(schema.Length - 3); if (name == "CTL") { switch (environment) { case "Dev": typeof(EntitySetBase).GetField("_schema", BindingFlags.NonPublic | BindingFlags.Instance) .SetValue(entitySetBase, devCTL); break; case "QA": typeof(EntitySetBase).GetField("_schema", BindingFlags.NonPublic | BindingFlags.Instance) .SetValue(entitySetBase, qaCTL); break; case "Prod": typeof(EntitySetBase).GetField("_schema", BindingFlags.NonPublic | BindingFlags.Instance) .SetValue(entitySetBase, prodCTL); break; } } if (name == "DTA") { switch (environment) { case "Dev": typeof(EntitySetBase).GetField("_schema", BindingFlags.NonPublic | BindingFlags.Instance) .SetValue(entitySetBase, devDTA); break; case "QA": typeof(EntitySetBase).GetField("_schema", BindingFlags.NonPublic | BindingFlags.Instance) .SetValue(entitySetBase, qaDTA); break; case "Prod": typeof(EntitySetBase).GetField("_schema", BindingFlags.NonPublic | BindingFlags.Instance) .SetValue(entitySetBase, prodDTA); break; } } } } }