private static string GetWhereCondition(Relationship relation, bool isParentRelations, bool isOneToOne) { var whereCondition = string.Empty; for (int index = 0, total = relation.ParentCols.Count; index < total; index++) { if (!string.IsNullOrEmpty(whereCondition)) { whereCondition += " && "; } if (isParentRelations) { whereCondition = "x." + relation.ParentCols[0].PropertyName + " == this." + relation.ChildCols[0].PropertyName; } else { whereCondition = "x." + relation.ChildCols[0].PropertyName + " == this." + relation.ParentCols[0].PropertyName; } } if (string.IsNullOrEmpty(whereCondition)) { return "false"; } return whereCondition; }
private void WriteRelatedEntityId(Relationship relation, bool isParentRelations) { if (isParentRelations) { Write(GetEntityClassName(relation.ParentTable)); } else { Write(GetEntityClassName(relation.ChildTable)); } }
private void WriteMemberName(Relationship relation, bool isParentRelations) { if (isParentRelations) { Write(relation.PropNameForChild); } else { Write(relation.PropNameForParent); } }
private void WriteKeyMembers(Relationship relation, bool isParentRelations) { List<Column> columns; if (isParentRelations) { columns = relation.ChildCols; } else { columns = relation.ParentCols; } Write(string.Join(",", columns.Select(x => x.PropertyName).ToArray())); }
private string GetPropertyType(Relationship relation, bool isParentRelations, bool isOneToOne) { var propertyType = string.Empty; if (!isOneToOne) { propertyType += "List<"; } if (isParentRelations) { propertyType += GetEntityClassName(relation.ParentTable); } else { propertyType += GetEntityClassName(relation.ChildTable); } if (!isOneToOne) { propertyType += ">"; } return propertyType; }
private static string GetPropertyName(Relationship relation, bool isParentRelations) { return isParentRelations ? relation.PropNameForChild : relation.PropNameForParent; }
private static string GetDataContextPropertyName(Relationship relation, bool isParentRelations) { return isParentRelations ? relation.ParentTable.PropertyName : relation.ChildTable.PropertyName; }
private Relationship GetRelationship(IDictionary<string, Relationship> existingRelationships, ColumnAssociation ca) { Relationship relationship; if (!existingRelationships.TryGetValue(ca.RelationshipName, out relationship)) { SchemaObject obj2; SchemaObject obj3; if (!(((this.Objects.TryGetValue(ca.ChildSchema + "." + ca.ChildTable, out obj2) && this.Objects.TryGetValue(ca.ParentSchema + "." + ca.ParentTable, out obj3)) && obj2.HasKey) && obj3.HasKey)) { return null; } if (!((obj3 is Table) && (obj2 is Table))) { return null; } relationship = new Relationship(ca.RelationshipName, (Table) obj3, (Table) obj2, Program.AllowOneToOne); existingRelationships.Add(ca.RelationshipName, relationship); } Column item = relationship.ChildTable.Columns.Values.FirstOrDefault<Column>(c => c.ColumnName == ca.ChildColumn); Column column2 = relationship.ParentTable.Columns.Values.FirstOrDefault<Column>(c => c.ColumnName == ca.ParentColumn); if ((item != null) && (column2 != null)) { relationship.ChildCols.Add(item); relationship.ParentCols.Add(column2); } return relationship; }
private void EmitChildRelation(ILGenerator constructorGen, TypeBuilder t, Relationship r) { string parentFieldName = r.ParentFieldName; TypeBuilder builder = this._tableTypes[r.ChildTable.DotNetName]; Type type = typeof(EntitySet<>).MakeGenericType(new Type[] { builder }); Type type2 = typeof(Action<>).MakeGenericType(new Type[] { builder }); FieldBuilder field = t.DefineField(parentFieldName, type, FieldAttributes.Private); MethodBuilder meth = t.DefineMethod("attach" + parentFieldName, MethodAttributes.Private, typeof(void), new TypeBuilder[] { builder }); ILGenerator iLGenerator = meth.GetILGenerator(); iLGenerator.Emit(OpCodes.Ldarg_1); iLGenerator.Emit(OpCodes.Ldarg_0); iLGenerator.Emit(OpCodes.Callvirt, this._entityRefPropSetMethods[r]); iLGenerator.Emit(OpCodes.Ret); MethodBuilder builder4 = t.DefineMethod("detach" + parentFieldName, MethodAttributes.Private, typeof(void), new TypeBuilder[] { builder }); ILGenerator generator2 = builder4.GetILGenerator(); generator2.Emit(OpCodes.Ldarg_1); generator2.Emit(OpCodes.Ldnull); generator2.Emit(OpCodes.Callvirt, this._entityRefPropSetMethods[r]); generator2.Emit(OpCodes.Ret); ConstructorInfo constructor = TypeBuilder.GetConstructor(type2, this._actionTConstructorOpen); ConstructorInfo con = TypeBuilder.GetConstructor(type, _esConstructorOpen); constructorGen.Emit(OpCodes.Ldarg_0); constructorGen.Emit(OpCodes.Ldarg_0); constructorGen.Emit(OpCodes.Ldftn, meth); constructorGen.Emit(OpCodes.Newobj, constructor); constructorGen.Emit(OpCodes.Ldarg_0); constructorGen.Emit(OpCodes.Ldftn, builder4); constructorGen.Emit(OpCodes.Newobj, constructor); constructorGen.Emit(OpCodes.Newobj, con); constructorGen.Emit(OpCodes.Stfld, field); PropertyBuilder builder5 = t.DefineProperty(r.PropNameForParent, PropertyAttributes.HasDefault, type, null); MethodBuilder mdBuilder = t.DefineMethod("get_" + r.PropNameForParent, MethodAttributes.SpecialName | MethodAttributes.HideBySig | MethodAttributes.Public, type, new Type[0]); ILGenerator generator3 = mdBuilder.GetILGenerator(); generator3.Emit(OpCodes.Ldarg_0); generator3.Emit(OpCodes.Ldfld, field); generator3.Emit(OpCodes.Ret); MethodBuilder builder7 = t.DefineMethod("set_" + r.PropNameForParent, MethodAttributes.SpecialName | MethodAttributes.HideBySig | MethodAttributes.Public, typeof(void), new Type[] { type }); ILGenerator generator4 = builder7.GetILGenerator(); generator4.Emit(OpCodes.Ldarg_0); generator4.Emit(OpCodes.Ldfld, field); generator4.Emit(OpCodes.Ldarg_1); MethodInfo method = typeof(EntitySet<>).GetMethod("Assign"); MethodInfo methodInfo = TypeBuilder.GetMethod(type, method); generator4.EmitCall(OpCodes.Callvirt, methodInfo, null); generator4.Emit(OpCodes.Ret); builder5.SetGetMethod(mdBuilder); builder5.SetSetMethod(builder7); object[] propertyValues = new object[] { r.Name, parentFieldName, string.Join(",", (from c in r.ChildCols select c.PropertyName).ToArray<string>()), string.Join(",", (from c in r.ParentCols select c.PropertyName).ToArray<string>()) }; CustomAttributeBuilder customBuilder = new CustomAttributeBuilder(typeof(AssociationAttribute).GetConstructor(new Type[0]), new object[0], new PropertyInfo[] { typeof(AssociationAttribute).GetProperty("Name"), typeof(AssociationAttribute).GetProperty("Storage"), typeof(AssociationAttribute).GetProperty("OtherKey"), typeof(AssociationAttribute).GetProperty("ThisKey") }, propertyValues); builder5.SetCustomAttribute(customBuilder); }
private void EmitParentRelation(ILGenerator constructorGen, TypeBuilder t, Relationship r) { string childFieldName = r.ChildFieldName; TypeBuilder returnType = this._tableTypes[r.ParentTable.DotNetName]; Type type = typeof(EntityRef<>).MakeGenericType(new Type[] { returnType }); FieldBuilder field = t.DefineField(childFieldName, type, FieldAttributes.Private); PropertyBuilder builder3 = this._entityRefProps[r]; MethodBuilder mdBuilder = t.DefineMethod("get_" + r.PropNameForChild, MethodAttributes.SpecialName | MethodAttributes.HideBySig | MethodAttributes.Public, returnType, new Type[0]); ILGenerator iLGenerator = mdBuilder.GetILGenerator(); iLGenerator.Emit(OpCodes.Ldarg_0); iLGenerator.Emit(OpCodes.Ldflda, field); MethodInfo getMethod = typeof(EntityRef<>).GetProperty("Entity").GetGetMethod(); MethodInfo method = TypeBuilder.GetMethod(type, getMethod); iLGenerator.EmitCall(OpCodes.Call, method, null); iLGenerator.Emit(OpCodes.Ret); MethodBuilder builder5 = this._entityRefPropSetMethods[r]; ILGenerator generator2 = builder5.GetILGenerator(); generator2.Emit(OpCodes.Ldarg_0); generator2.Emit(OpCodes.Ldflda, field); generator2.Emit(OpCodes.Ldarg_1); MethodInfo setMethod = typeof(EntityRef<>).GetProperty("Entity").GetSetMethod(); MethodInfo methodInfo = TypeBuilder.GetMethod(type, setMethod); generator2.EmitCall(OpCodes.Call, methodInfo, null); generator2.Emit(OpCodes.Ret); builder3.SetGetMethod(mdBuilder); builder3.SetSetMethod(builder5); object[] propertyValues = new object[] { r.Name, childFieldName, string.Join(",", (from c in r.ChildCols select c.PropertyName).ToArray<string>()), string.Join(",", (from c in r.ParentCols select c.PropertyName).ToArray<string>()), true }; CustomAttributeBuilder customBuilder = new CustomAttributeBuilder(typeof(AssociationAttribute).GetConstructor(new Type[0]), new object[0], new PropertyInfo[] { typeof(AssociationAttribute).GetProperty("Name"), typeof(AssociationAttribute).GetProperty("Storage"), typeof(AssociationAttribute).GetProperty("ThisKey"), typeof(AssociationAttribute).GetProperty("OtherKey"), typeof(AssociationAttribute).GetProperty("IsForeignKey") }, propertyValues); builder3.SetCustomAttribute(customBuilder); }