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; } } } }
protected RootNavigationBuilder(EntityMetadataBuilder entityBuilder, Type targetType, string propertyName) : base(propertyName) { this.EntityBuilder = entityBuilder; this.TargetType = targetType; this.NavigationPropertyBuilder = entityBuilder.Navigation(propertyName); }
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()); }
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(); }
public ManyNavigationBuilder(EntityMetadataBuilder entityBuilder, Type targetType, string propertyName) : base(entityBuilder, targetType, propertyName) { NavigationPropertyBuilder.Nullable = false; }
public OneNavigationBuilder(EntityMetadataBuilder entityBuilder, Type targetType, string propertyName) : base(entityBuilder, targetType, propertyName) { }
public OneNavigationBuilder(EntityMetadataBuilder entityBuilder, string propertyName = null) : base(entityBuilder, typeof(TTarget), propertyName) { }