public IEntityQueryable <TEntity> GetContext <TEntity>() where TEntity : EntityBase, new() { if (!EntityTypes.Contains(typeof(TEntity))) { throw new ArgumentException("TEntity不属于该Context。"); } return((IEntityQueryable <TEntity>)Activator.CreateInstance(typeof(EntityQueryable <>).MakeGenericType(typeof(TEntity)), DbContext)); }
private void CreateForeignKeyRelationships() { // Find FK relationships that this entity is the dependent of var fkRelationships = Pass .EntityType .NavigationProperties .Where(np => np.DeclaringType == Pass.EntityType && ((AssociationType)np.RelationshipType).IsForeignKey && ((AssociationType)np.RelationshipType).ReferentialConstraints.Single().ToRole == np.FromEndMember && EntityTypes.Contains(Pass.EntityType) && EntityTypes.Contains(np.ToEndMember.GetEntityType())); if (fkRelationships.Any()) { fkRelationships.Each(r => { var otherNavProperty = r.ToEndMember.GetEntityType().NavigationProperties.Where(n => n.RelationshipType == r.RelationshipType && n != r).Single(); var association = (AssociationType)r.RelationshipType; if (r.ToEndMember.RelationshipMultiplicity == RelationshipMultiplicity.One) { Add(Tab(3), string.Format("builder.HasOne(t => t.{0})", Pass.Code.Escape(r))); } else { Add(Tab(3), string.Format("builder.HasOne(t => t.{0})", Pass.Code.Escape(r))); } if (r.FromEndMember.RelationshipMultiplicity == RelationshipMultiplicity.Many) { Add(Tab(4), string.Format(".WithMany(t =>t.{0})", Pass.Code.Escape(otherNavProperty))); if (association.ReferentialConstraints.Single().ToProperties.Count == 1) { Add(Tab(4), string.Format(".HasForeignKey(d => d.{0})", association.ReferentialConstraints.Single().ToProperties.Single().Name)); } else { var toProperties = association.ReferentialConstraints.Single().ToProperties.OrderByDescending(o => o.Name); Add(Tab(4), string.Format(".HasForeignKey(d => new {{ {0} }})", string.Join(", ", toProperties.Select(p => "d." + p.Name)))); } } else { Add(Tab(4), string.Format(".WithOne(t => t.{0})", Pass.Code.Escape(otherNavProperty))); } if (Settings.IsCascadeOnDelete) { Add(Tab(4), ".Metadata.DeleteBehavior = DeleteBehavior.Cascade;"); } else { Add(Tab(4), ".Metadata.DeleteBehavior = DeleteBehavior.Restrict;"); } }); } }
private void CreateForeignKeyRelationships() { // Find FK relationships that this entity is the dependent of var fkRelationships = Pass .EntityType .NavigationProperties .Where(np => np.DeclaringType == Pass.EntityType && ((AssociationType)np.RelationshipType).IsForeignKey && ((AssociationType)np.RelationshipType).ReferentialConstraints.Single().ToRole == np.FromEndMember && EntityTypes.Contains(Pass.EntityType) && EntityTypes.Contains(np.ToEndMember.GetEntityType())); if (fkRelationships.Any()) { fkRelationships.Each(r => { var otherNavProperty = r.ToEndMember.GetEntityType().NavigationProperties.Where(n => n.RelationshipType == r.RelationshipType && n != r).Single(); var association = (AssociationType)r.RelationshipType; if (r.ToEndMember.RelationshipMultiplicity == RelationshipMultiplicity.One) { Add(Tab(3), string.Format("this.HasRequired(t => t.{0})", Pass.Code.Escape(r))); } else { Add(Tab(3), string.Format("this.HasOptional(t => t.{0})", Pass.Code.Escape(r))); } if (r.FromEndMember.RelationshipMultiplicity == RelationshipMultiplicity.Many) { Add(Tab(4), string.Format(".WithMany(t =>t.{0})", Pass.Code.Escape(otherNavProperty))); if (association.ReferentialConstraints.Single().ToProperties.Count == 1) { Add(Tab(4), string.Format(".HasForeignKey(d => d.{0})", association.ReferentialConstraints.Single().ToProperties.Single().Name)); } else { var toProperties = association.ReferentialConstraints.Single().ToProperties.OrderByDescending(o => o.Name); Add(Tab(4), string.Format(".HasForeignKey(d => new {{ {0} }})", string.Join(", ", toProperties.Select(p => "d." + p.Name)))); } } else { Add(Tab(4), "// NOTE: We can assume that this is a required:optional relationship"); Add(Tab(4), "// as EDMGen will never create an optional:optional relationship"); Add(Tab(4), "// because everything is one:many except PK-PK relationships which must be required"); Add(Tab(4), string.Format(".WithOptional(t => t.{0})", Pass.Code.Escape(otherNavProperty))); } Add(Tab(4), string.Format(".WillCascadeOnDelete({0});", Settings.IsCascadeOnDelete.ToString().LowerCaseFirstCharacter())); }); } }
private void CreateManyManyRelationships() { // Find m:m relationshipsto configure var manyManyRelationships = Pass .EntityType.NavigationProperties .Where(np => np.DeclaringType == Pass.EntityType && np.ToEndMember.RelationshipMultiplicity == RelationshipMultiplicity.Many && np.FromEndMember.RelationshipMultiplicity == RelationshipMultiplicity.Many && np.RelationshipType.RelationshipEndMembers.First() == np.FromEndMember && // <- ensures we only configure from one end EntityTypes.Contains(Pass.EntityType) && EntityTypes.Contains(np.ToEndMember.GetEntityType())); if (manyManyRelationships.Any()) { manyManyRelationships.Each(r => { var otherNavProperty = r.ToEndMember.GetEntityType().NavigationProperties.Where(n => n.RelationshipType == r.RelationshipType && n != r).Single(); var association = (AssociationType)r.RelationshipType; var mapping = Pass.EntityHelper.Mappings.ManyToManyMappings[association]; var item1 = mapping.Item1; var mappingTableName = (string)mapping.Item1.MetadataProperties["Table"].Value ?? item1.Name; var mappingSchemaName = (string)item1.MetadataProperties["Schema"].Value; // Need to ensure that FKs are declared in the same order as the PK properties on each principal type var leftType = (EntityType)r.DeclaringType; var leftKeyMappings = mapping.Item2[r.FromEndMember]; var leftColumns = string.Join(", ", leftType.KeyMembers.Select(m => "\"" + leftKeyMappings[m] + "\"")); var rightType = (EntityType)otherNavProperty.DeclaringType; var rightKeyMappings = mapping.Item2[otherNavProperty.FromEndMember]; var rightColumns = string.Join(", ", rightType.KeyMembers.Select(m => "\"" + rightKeyMappings[m] + "\"")); Add(Tab(3), string.Format("builder.HasMany(t => t.{0})", Pass.Code.Escape(r))); Add(Tab(4), string.Format(".WithMany(t => t.{0})", Pass.Code.Escape(otherNavProperty))); Add(Tab(4), ".Map(m =>"); Add(Tab(4), "{"); if (mappingSchemaName == "dbo" || string.IsNullOrWhiteSpace(mappingSchemaName)) { Add(Tab(5), string.Format("m.ToTable(\"{0}\");", mappingTableName)); } else { Add(Tab(5), string.Format("m.ToTable(\"{0}\", \"{1}\");", mappingTableName, mappingSchemaName)); } Add(Tab(5), string.Format("m.MapLeftKey({0});", leftColumns)); Add(Tab(5), string.Format("m.MapRightKey({0});", rightColumns)); Add(Tab(4), "});"); }); } }
/// <summary> /// Get entity context. /// </summary> /// <typeparam name="TEntity">Type of entity.</typeparam> /// <returns>Return entity context.</returns> /// <exception cref="ArgumentException">Type of entity doesn't support.</exception> public IEntityQueryable <TEntity> GetContext <TEntity>() where TEntity : class, IEntity, new() { if (disposed) { throw new ObjectDisposedException("EntityContextBuilder"); } if (!EntityTypes.Contains(typeof(TEntity))) { throw new ArgumentException("TEntity不属于该Context。"); } Type type = typeof(TEntity); if (!cache.ContainsKey(type)) { IEntityQueryable <TEntity> result = new EntityQueryable <TEntity>(DbContext); cache.Add(type, result); return(result); } return((IEntityQueryable <TEntity>)cache[type]); }