internal NavigationPropertyMetadata(ModelCreationScope scope, EntityMetadata entity, NavigationPropertyBuilder builder)
            : base(entity, builder)
        {
            scope.AddNavigationProperty(entity, builder.Name, this);

            this.Multiplicity       = builder.Multiplicity;
            this.TargetMultiplicity = builder.TargetMultiplicity;
            this.TargetEntity       = scope.GetEntity(builder.Target.ClrType);
            if (builder.TargetProperty != null)
            {
                this.TargetNavigationPropertyName = builder.TargetProperty;
                ////this.TargetNavigationProperty = scope.NavigationProperties[this.TargetEntity][builder.TargetProperty];
            }

            var foreignKeyBuilder = ImmutableList.CreateBuilder <ForeignKey>();

            var principalKeys = this.TargetEntity.GetProperties().Where(p => p.IsPrimaryKey).ToList();

            for (int i = 0; i < builder.ForeignKey.Count; i++)
            {
                var fk = builder.ForeignKey[i];
                foreignKeyBuilder.Add(new ForeignKey(principalKeys[i], entity.GetProperties().First(p => p.Name == fk)));
            }

            ForeignKeyProperties = foreignKeyBuilder.ToImmutable();
        }
Exemple #2
0
        internal EntityMetadata(ModelCreationScope scope, EntityMetadataBuilder builder)
            : base(builder.Name)
        {
            scope.AddEntity(builder.TypeInfo.ClrType, this);
            this.ClrType = builder.TypeInfo.ClrType;

            var childListBuilder      = ImmutableList.CreateBuilder <EntityMetadata>();
            var propertyListBuilder   = ImmutableList.CreateBuilder <ScalarProperty>();
            var navigationListBuilder = ImmutableList.CreateBuilder <NavigationPropertyMetadata>();

            var properties = builder.Properties.OrderBy(p => builder.PrimaryKey.Contains(p.Name) ? builder.PrimaryKey.ToList().IndexOf(p.Name) : builder.PrimaryKey.Count).ThenBy(p => p.Name).ToList();

            foreach (var item in properties.Where(p => !p.IsExcluded))
            {
                propertyListBuilder.Add(item.ToProperty(this, builder.PrimaryKey.Contains(item.Name)));
            }

            Properties = propertyListBuilder.ToImmutable();

            if (builder.BaseEntity != null)
            {
                BaseEntity = scope.GetEntity(builder.BaseEntity.TypeInfo.ClrType);
            }

            childListBuilder.AddRange(scope.GetChildEntities(builder.TypeInfo.ClrType));
            ChildEntities = childListBuilder.ToImmutable();

            foreach (var item in builder.Navigations.Where(p => !p.IsExcluded))
            {
                navigationListBuilder.Add(item.ToNavigation(scope, this));
            }

            Navigations = navigationListBuilder.ToImmutable();

            var primaryKeys = this.GetProperties().Where(p => p.IsPrimaryKey);

            _primaryKeys    = primaryKeys.ToImmutableList();
            PrimaryKeyCount = _primaryKeys.Count;

            _entityValueAccesssor = scope.GetEntityValueAccessor(this);

            PrimaryKeyType = _entityValueAccesssor.GetPrimaryKeyType();
        }
Exemple #3
0
        internal MetadataModel(MetadataModelBuilder modelBuilder, IValueAccessorFactory valueAccessorFactory)
        {
            var scope      = new ModelCreationScope(modelBuilder, valueAccessorFactory);
            var unordered  = GetSorted(modelBuilder.Entities).Where(p => !p.IsExcluded).Select(p => scope.GetEntity(p.TypeInfo.ClrType)).ToList();
            var targetList = unordered.Where(p => p.BaseEntity == null).OrderBy(p => p.Name).ToList();

            targetList.ForEach(p => unordered.Remove(p));

            while (unordered.Any())
            {
                var nextLayer = unordered.Where(p => targetList.Contains(p.BaseEntity)).ToList();
                foreach (var item in nextLayer.OrderByDescending(p => p.Name))
                {
                    unordered.Remove(item);
                    targetList.Insert(0, item);
                }
            }

            Entities = ImmutableList.CreateRange(targetList);
        }