public void Apply(EntityMetadataBuilder entity)
        {
            foreach (var item in entity.Properties)
            {
                var propertyInfo = entity.TypeInfo.ClrType.GetProperty(item.Name);
                var maxLength    = propertyInfo.GetCustomAttribute <MaxLengthAttribute>();
                var stringLength = propertyInfo.GetCustomAttribute <StringLengthAttribute>();

                int?length = null;

                if (maxLength != null)
                {
                    length = maxLength.Length;
                }

                if (stringLength != null)
                {
                    length = stringLength.MaximumLength;
                }

                if (length.HasValue)
                {
                    switch (item)
                    {
                    case TextPropertyBuilder textPropertyBuilder:
                        textPropertyBuilder.MaxLength = length;
                        break;

                    case BlobPropertyBuilder blobPropertyBuilder:
                        blobPropertyBuilder.MaxLength = length;
                        break;
                    }
                }
            }
        }
Exemple #2
0
 protected RootNavigationBuilder(EntityMetadataBuilder entityBuilder, Type targetType, string propertyName)
     : base(propertyName)
 {
     this.EntityBuilder             = entityBuilder;
     this.TargetType                = targetType;
     this.NavigationPropertyBuilder = entityBuilder.Navigation(propertyName);
 }
Exemple #3
0
 public void Apply(EntityMetadataBuilder entity)
 {
     if (!entity.PrimaryKey.Any())
     {
         var key = entity.Properties.Where(p => p.Name.Equals("Id", StringComparison.OrdinalIgnoreCase) || p.Name.Equals($"{entity.Name}Id", StringComparison.OrdinalIgnoreCase)).FirstOrDefault();
         if (key != null)
         {
             entity.PrimaryKey.Add(key.Name);
         }
     }
 }
        public void OneToOneWithConfigDefaultStructureConventionTest()
        {
            var builder = new MetadataModelBuilder();

            builder.Entity <Test1>().HasOne(p => p.Test2).WithPrincipal(p => p.Test1);
            builder.ApplyConventions();
            EntityMetadataBuilder entity = builder.Entity <Test1>();

            Assert.Equal(1, entity.PrimaryKey.Count);
            Assert.Equal("Id", entity.PrimaryKey.First());

            entity = builder.Entity <Test2>();

            Assert.Equal(1, entity.PrimaryKey.Count);
            Assert.Equal("Test2ID", entity.PrimaryKey.First());
        }
Exemple #5
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 #6
0
 public ManyNavigationBuilder(EntityMetadataBuilder entityBuilder, Type targetType, string propertyName)
     : base(entityBuilder, targetType, propertyName)
 {
     NavigationPropertyBuilder.Nullable = false;
 }
Exemple #7
0
 public OneNavigationBuilder(EntityMetadataBuilder entityBuilder, Type targetType, string propertyName)
     : base(entityBuilder, targetType, propertyName)
 {
 }
Exemple #8
0
 public OneNavigationBuilder(EntityMetadataBuilder entityBuilder, string propertyName = null)
     : base(entityBuilder, typeof(TTarget), propertyName)
 {
 }