コード例 #1
0
 /// <summary>
 ///     Returns the foreign key constraint name.
 /// </summary>
 /// <param name="foreignKey"> The foreign key. </param>
 /// <returns> The foreign key constraint name. </returns>
 public static string GetConstraintName([NotNull] this IForeignKey foreignKey)
 => foreignKey.GetConstraintName(
     StoreObjectIdentifier.Create(foreignKey.DeclaringEntityType, StoreObjectType.Table).Value,
     StoreObjectIdentifier.Create(foreignKey.PrincipalKey.IsPrimaryKey()
             ? foreignKey.PrincipalEntityType
             : foreignKey.PrincipalKey.DeclaringEntityType,
                                  StoreObjectType.Table).Value);
コード例 #2
0
        protected override void OnModelCreating(ModelBuilder builder)
        {
            base.OnModelCreating(builder);


            foreach (var entity in builder.Model.GetEntityTypes())
            {
                var tableIdentifier = StoreObjectIdentifier.Create(entity, StoreObjectType.Table);
                entity.SetTableName(entity.GetTableName().ToSnakeCase());

                foreach (var property in entity.GetProperties())
                {
                    property.SetColumnName(property.GetColumnName(tableIdentifier.Value).ToSnakeCase());
                }

                foreach (var key in entity.GetKeys())
                {
                    key.SetName(key.GetName().ToSnakeCase());
                }

                foreach (var key in entity.GetForeignKeys())
                {
                    key.SetConstraintName(key.GetConstraintName().ToSnakeCase());
                }

                foreach (var index in entity.GetIndexes())
                {
                    index.SetDatabaseName(index.GetDatabaseName().ToSnakeCase());
                }
            }


            builder.Entity <User>().HasKey(u => u.UserId);
            builder.Entity <User>().HasIndex(u => u.Username);
        }
コード例 #3
0
        public void TPT_reversed_configuration()
        {
            var model = BuildModel(b =>
            {
                b.Entity <Child>().ToTable("child");
                b.Entity <Parent>().ToTable("parent");
            });

            var parentEntityType = model.FindEntityType(typeof(Parent));
            var childEntityType  = model.FindEntityType(typeof(Child));

            Assert.Equal("parent", parentEntityType.GetTableName());
            Assert.Equal("id", parentEntityType.FindProperty(nameof(Parent.Id))
                         .GetColumnName(StoreObjectIdentifier.Create(parentEntityType, StoreObjectType.Table) !.Value));
            Assert.Equal("parent_property", parentEntityType.FindProperty(nameof(Parent.ParentProperty))
                         .GetColumnName(StoreObjectIdentifier.Create(parentEntityType, StoreObjectType.Table) !.Value));

            Assert.Equal("child", childEntityType.GetTableName());
            Assert.Equal("child_property", childEntityType.FindProperty(nameof(Child.ChildProperty))
                         .GetColumnName(StoreObjectIdentifier.Create(childEntityType, StoreObjectType.Table) !.Value));

            var primaryKey = parentEntityType.FindPrimaryKey();

            Assert.Same(primaryKey, childEntityType.FindPrimaryKey());

            Assert.Equal("PK_parent", primaryKey.GetName());

            // For the following, see #112
            var parentStoreObjectIdentifier = StoreObjectIdentifier.Create(parentEntityType, StoreObjectType.Table).Value;
            var childStoreObjectIdentifier  = StoreObjectIdentifier.Create(childEntityType, StoreObjectType.Table).Value;

            Assert.Equal("PK_parent", primaryKey.GetName(parentStoreObjectIdentifier));
            Assert.Equal("PK_child", primaryKey.GetName(childStoreObjectIdentifier));
        }
コード例 #4
0
        public void TPT()
        {
            var model = BuildModel(b =>
            {
                b.Entity <Parent>().ToTable("parent");
                b.Entity <Child>().ToTable("child");
            });

            var parentEntityType = model.FindEntityType(typeof(Parent));
            var childEntityType  = model.FindEntityType(typeof(Child));

            Assert.Equal("parent", parentEntityType.GetTableName());
            Assert.Equal("id", parentEntityType.FindProperty(nameof(Parent.Id))
                         .GetColumnName(StoreObjectIdentifier.Create(parentEntityType, StoreObjectType.Table) !.Value));
            Assert.Equal("parent_property", parentEntityType.FindProperty(nameof(Parent.ParentProperty))
                         .GetColumnName(StoreObjectIdentifier.Create(parentEntityType, StoreObjectType.Table) !.Value));

            Assert.Equal("child", childEntityType.GetTableName());
            Assert.Equal("child_property", childEntityType.FindProperty(nameof(Child.ChildProperty))
                         .GetColumnName(StoreObjectIdentifier.Create(childEntityType, StoreObjectType.Table) !.Value));

            var parentKey = parentEntityType.FindPrimaryKey();
            var childKey  = childEntityType.FindPrimaryKey();

            Assert.Equal("PK_parent", parentKey.GetName());
            Assert.Equal("PK_parent", childKey.GetName());
        }
コード例 #5
0
        public static EntityTypeBuilder UseCockroachDbInterleaveInParent(
            this EntityTypeBuilder entityTypeBuilder,
            Type parentTableType,
            List <string> interleavePrefix)
        {
            Check.NotNull(entityTypeBuilder, nameof(entityTypeBuilder));
            Check.NotNull(parentTableType, nameof(parentTableType));
            Check.NotNull(interleavePrefix, nameof(interleavePrefix));

            var parentEntity = entityTypeBuilder.Metadata.Model.FindEntityType(parentTableType);

            if (parentEntity == null)
            {
                throw new ArgumentException($"Entity not found in model for type: {parentEntity}", nameof(parentTableType));
            }

            if (StoreObjectIdentifier.Create(parentEntity, StoreObjectType.Table) is not StoreObjectIdentifier tableIdentifier)
            {
                throw new ArgumentException($"Entity {parentEntity.DisplayName()} is not mapped to a database table");
            }

            var interleaveInParent = entityTypeBuilder.Metadata.GetCockroachDbInterleaveInParent();

            interleaveInParent.ParentTableSchema = tableIdentifier.Schema;
            interleaveInParent.ParentTableName   = tableIdentifier.Name;
            interleaveInParent.InterleavePrefix  = interleavePrefix;

            return(entityTypeBuilder);
        }
コード例 #6
0
        public void TPH_with_owned()
        {
            var model = BuildModel(b =>
            {
                b.Entity <Parent>();
                b.Entity <ChildWithOwned>().OwnsOne(c => c.Owned);
            });

            var parentEntityType = model.FindEntityType(typeof(Parent));
            var childEntityType  = model.FindEntityType(typeof(ChildWithOwned));
            var ownedEntityType  = model.FindEntityType(typeof(Owned));

            Assert.Equal("parent", parentEntityType.GetTableName());
            Assert.Equal("id", parentEntityType.FindProperty(nameof(Parent.Id))
                         .GetColumnName(StoreObjectIdentifier.Create(parentEntityType, StoreObjectType.Table) !.Value));
            Assert.Equal("parent_property", parentEntityType.FindProperty(nameof(Parent.ParentProperty))
                         .GetColumnName(StoreObjectIdentifier.Create(childEntityType, StoreObjectType.Table) !.Value));

            Assert.Equal("parent", childEntityType.GetTableName());
            Assert.Equal("child_property", childEntityType.FindProperty(nameof(Child.ChildProperty))
                         .GetColumnName(StoreObjectIdentifier.Create(childEntityType, StoreObjectType.Table) !.Value));

            Assert.Same(parentEntityType.FindPrimaryKey(), childEntityType.FindPrimaryKey());

            Assert.Equal("parent", ownedEntityType.GetTableName());
            Assert.Equal("owned_owned_property", ownedEntityType.FindProperty(nameof(Owned.OwnedProperty))
                         .GetColumnName(StoreObjectIdentifier.Create(ownedEntityType, StoreObjectType.Table) !.Value));
        }
コード例 #7
0
        public void Owned_entity_twice_with_table_splitting_and_explicit_owner_table()
        {
            var model = BuildModel(
                b => b.Entity <Owner>(
                    e =>
            {
                e.OwnsOne("owned1", o => o.Owned);
                e.OwnsOne("owned2", o => o.Owned2);
                e.ToTable("destination_table");
            }));

            var ownerEntityType  = model.FindEntityType(typeof(Owner));
            var owned1EntityType = model.FindEntityType("owned1");
            var owned2EntityType = model.FindEntityType("owned2");

            Assert.Equal("destination_table", ownerEntityType.GetTableName());
            Assert.Equal("destination_table", owned1EntityType.GetTableName());
            Assert.Equal("destination_table", owned2EntityType.GetTableName());
            var table = StoreObjectIdentifier.Create(ownerEntityType, StoreObjectType.Table) !.Value;

            Assert.Equal(table, StoreObjectIdentifier.Create(owned1EntityType, StoreObjectType.Table) !.Value);
            Assert.Equal(table, StoreObjectIdentifier.Create(owned2EntityType, StoreObjectType.Table) !.Value);

            Assert.Equal("owned_owned_property", owned1EntityType.FindProperty(nameof(Owned.OwnedProperty)).GetColumnName(table));
            Assert.Equal("owned2_owned_property", owned2EntityType.FindProperty(nameof(Owned.OwnedProperty)).GetColumnName(table));

            var(ownerKey, owned1Key, owned2Key) =
                (ownerEntityType.FindPrimaryKey(), owned1EntityType.FindPrimaryKey(), owned1EntityType.FindPrimaryKey());
            Assert.Equal("pk_destination_table", ownerKey.GetName());
            Assert.Equal("pk_destination_table", owned1Key.GetName());
            Assert.Equal("pk_destination_table", owned2Key.GetName());
            Assert.Equal("id", ownerKey.Properties.Single().GetColumnName(table));
            Assert.Equal("id", owned1Key.Properties.Single().GetColumnName(table));
            Assert.Equal("id", owned2Key.Properties.Single().GetColumnName(table));
        }
コード例 #8
0
        public void Table_splitting_and_explicit_owner_table()
        {
            var model = BuildModel(b =>
            {
                b.Entity <Split1>().HasOne(s1 => s1.S2).WithOne(s2 => s2.S1).HasForeignKey <Split2>(s2 => s2.Id);
                b.Entity <Split2>().ToTable("split_table");
                b.Entity <Split1>().ToTable("split_table");
            });

            var split1EntityType = model.FindEntityType(typeof(Split1));
            var split2EntityType = model.FindEntityType(typeof(Split2));

            var table = StoreObjectIdentifier.Create(split1EntityType, StoreObjectType.Table) !.Value;

            Assert.Equal(table, StoreObjectIdentifier.Create(split2EntityType, StoreObjectType.Table));

            Assert.Equal("split_table", split1EntityType.GetTableName());
            Assert.Equal("one_prop", split1EntityType.FindProperty(nameof(Split1.OneProp)).GetColumnName(table));

            Assert.Equal("split_table", split2EntityType.GetTableName());
            Assert.Equal("two_prop", split2EntityType.FindProperty(nameof(Split2.TwoProp)).GetColumnName(table));

            Assert.Equal("common", split1EntityType.FindProperty(nameof(Split1.Common)).GetColumnName(table));
            Assert.Equal("split2_common", split2EntityType.FindProperty(nameof(Split2.Common)).GetColumnName(table));

            var foreignKey = split2EntityType.GetForeignKeys().Single();

            Assert.Same(split1EntityType.FindPrimaryKey(), foreignKey.PrincipalKey);
            Assert.Same(split2EntityType.FindPrimaryKey().Properties.Single(), foreignKey.Properties.Single());
            Assert.Equal(split1EntityType.FindPrimaryKey().GetName(), split2EntityType.FindPrimaryKey().GetName());
            Assert.Equal(
                foreignKey.PrincipalKey.Properties.Single().GetColumnName(table),
                foreignKey.Properties.Single().GetColumnName(table));
            Assert.Empty(split1EntityType.GetForeignKeys());
        }
        public static ModelBuilder UseSnakeCase(this ModelBuilder modelBuilder)
        {
            modelBuilder.Model.SetDefaultSchema(modelBuilder.Model.GetDefaultSchema().ToSnakeCase());
            foreach (var entity in modelBuilder.Model.GetEntityTypes())
            {
                entity.SetTableName(entity.GetTableName().ToSnakeCase());
                var storeObjectId = StoreObjectIdentifier.Create(entity, StoreObjectType.Table);

                foreach (var property in entity.GetProperties())
                {
                    property.SetColumnName(property.GetColumnName(storeObjectId !.Value).ToSnakeCase());
                }

                foreach (var key in entity.GetKeys())
                {
                    key.SetName(key.GetName().ToSnakeCase());
                }

                foreach (var key in entity.GetForeignKeys())
                {
                    key.SetConstraintName(key.GetConstraintName().ToSnakeCase());
                }

                foreach (var index in entity.GetIndexes())
                {
                    index.SetDatabaseName(index.GetDatabaseName().ToSnakeCase());
                }
            }
            return(modelBuilder);
        }
コード例 #10
0
        public void Column()
        {
            var entityType = BuildEntityType(b => b.Entity <SampleEntity>());

            Assert.Equal("sample_entity_id", entityType.FindProperty(nameof(SampleEntity.SampleEntityId))
                         .GetColumnName(StoreObjectIdentifier.Create(entityType, StoreObjectType.Table) !.Value));
        }
コード例 #11
0
        public static string GetColumnName(this PropertyEntry propertyEntry)
        {
            var storeObjectId =
                StoreObjectIdentifier.Create(propertyEntry.Metadata.DeclaringEntityType, StoreObjectType.Table);

            return(propertyEntry.Metadata.GetColumnName(storeObjectId.GetValueOrDefault()) ?? propertyEntry.Metadata.Name);
        }
コード例 #12
0
        public static void ApplyToModelBuilder(DatabaseFacade databaseFacade, ModelBuilder modelBuilder)
        {
            switch (DbProvider)
            {
            case DbProvider.SqlServer:
                break;

            case DbProvider.Sqlite:
            default:
                var mapper = new NpgsqlSnakeCaseNameTranslator();
                foreach (var entity in modelBuilder.Model.GetEntityTypes())
                {
                    foreach (var property in entity.GetProperties())
                    {
                        var storeObjectIdentifier = StoreObjectIdentifier.Create(property.DeclaringEntityType, StoreObjectType.Table);
                        if (storeObjectIdentifier.HasValue)
                        {
                            property.SetColumnName(mapper.TranslateMemberName(property.GetColumnName(storeObjectIdentifier.Value)));
                        }
                    }

                    entity.SetTableName(mapper.TranslateTypeName(entity.GetTableName()));
                }
                break;
            }
        }
コード例 #13
0
        private async Task BulkCopy <TEntity>(SqlBulkCopy bulkCopy, IEnumerable <TEntity> entities, CancellationToken cancellationToken)
        {
            var type   = typeof(TEntity);
            var entity = myContext.Model.FindEntityType(type);

            if (StoreObjectIdentifier.Create(entity, StoreObjectType.Table) is not {
            } identifier)
            {
                throw new ArgumentException("Can't get table name", nameof(TEntity));
            }
            bulkCopy.DestinationTableName = $"{identifier.Schema}.{identifier.Name}";
            bulkCopy.ColumnMappings.Clear();
            foreach (var property in entity.GetProperties())
            {
                if (property.IsShadowProperty())
                {
                    continue;
                }
                bulkCopy.ColumnMappings
                .Add(property.Name, property.GetColumnName(identifier));
            }

            var members = new string[bulkCopy.ColumnMappings.Count];

            for (var i = 0; i < bulkCopy.ColumnMappings.Count; i++)
            {
                members[i] = bulkCopy.ColumnMappings[i].SourceColumn;
            }

            await using var objectReader = ObjectReader.Create(entities, members);
            await bulkCopy.WriteToServerAsync(objectReader, cancellationToken);
        }
コード例 #14
0
        public void Column_name()
        {
            var entityType = BuildEntityType("SimpleBlog", e => e.Property <int>("SimpleBlogId"));

            Assert.Equal("simple_blog_id", entityType.FindProperty("SimpleBlogId")
                         .GetColumnName(StoreObjectIdentifier.Create(entityType, StoreObjectType.Table) !.Value));
        }
コード例 #15
0
        public void TPT()
        {
            var model = BuildModel(b =>
            {
                b.Entity("SimpleBlog", e =>
                {
                    e.Property <int>("SimpleBlogId");
                    e.HasKey("SimpleBlogId");
                });
                b.Entity("FancyBlog", e =>
                {
                    e.HasBaseType("SimpleBlog");
                    e.ToTable("fancy_blog");
                    e.Property <int>("FancyProperty");
                });
            });

            var simpleBlogEntityType = model.FindEntityType("SimpleBlog");

            Assert.Equal("simple_blog", simpleBlogEntityType.GetTableName());
            Assert.Equal("simple_blog_id", simpleBlogEntityType.FindProperty("SimpleBlogId")
                         .GetColumnName(StoreObjectIdentifier.Create(simpleBlogEntityType, StoreObjectType.Table) !.Value));

            var fancyBlogEntityType = model.FindEntityType("FancyBlog");

            Assert.Equal("fancy_blog", fancyBlogEntityType.GetTableName());
            Assert.Equal("fancy_property", fancyBlogEntityType.FindProperty("FancyProperty")
                         .GetColumnName(StoreObjectIdentifier.Create(fancyBlogEntityType, StoreObjectType.Table) !.Value));
        }
コード例 #16
0
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            #region TPTConfiguration
            modelBuilder.Entity <Blog>().ToTable("Blogs");
            modelBuilder.Entity <RssBlog>().ToTable("RssBlogs");
            #endregion

            #region Metadata
            foreach (var entityType in modelBuilder.Model.GetEntityTypes())
            {
                var tableIdentifier = StoreObjectIdentifier.Create(entityType, StoreObjectType.Table);

                Console.WriteLine($"{entityType.DisplayName()}\t\t{tableIdentifier}");
                Console.WriteLine(" Property\tColumn");

                foreach (var property in entityType.GetProperties())
                {
                    var columnName = property.GetColumnName(tableIdentifier.Value);
                    Console.WriteLine($" {property.Name,-10}\t{columnName}");
                }

                Console.WriteLine();
            }
            #endregion
        }
コード例 #17
0
        public void Column_with_turkish_culture()
        {
            var entityType = BuildEntityType(
                b => b.Entity <SampleEntity>(),
                CultureInfo.CreateSpecificCulture("tr-TR"));

            Assert.Equal("sample_entity_ıd", entityType.FindProperty(nameof(SampleEntity.SampleEntityId))
                         .GetColumnName(StoreObjectIdentifier.Create(entityType, StoreObjectType.Table) !.Value));
        }
コード例 #18
0
        protected override void OnModelCreating(ModelBuilder builder)
        {
            base.OnModelCreating(builder);

            // Customize the ASP.NET Identity model and override the defaults if needed.
            // For example, you can rename the ASP.NET Identity table names and more.
            // Add your customizations after calling base.OnModelCreating(builder);

            // var userStore = new UserStore<ApplicationUser>(new ApplicationDbContext());
            // var manager = new ApplicationUserManager(userStore);
            // var result = await manager.Create(user, password);

            string sc(string s)
            {
                if (string.IsNullOrEmpty(s))
                {
                    return(s);
                }
                var leadingLD = Regex.Match(s, @"^_+");

                return(leadingLD + Regex.Replace(s, @"([a-z0-9])([A-Z])", "$1_$2").ToLower());
            }

            foreach (var entity in builder.Model.GetEntityTypes())
            {
                // Replace table names
                entity.SetTableName(sc(entity.GetTableName()));

                // Replace column names
                foreach (var property in entity.GetProperties())
                {
                    var id = StoreObjectIdentifier.Create(entity, StoreObjectType.Table);
                    if (id == null)
                    {
                        throw new Exception("Cannot create objectIdentifier.");
                    }

                    property.SetColumnName(sc(property.GetColumnName(id.Value)));
                }

                foreach (var key in entity.GetKeys())
                {
                    key.SetName(sc(key.GetName()));
                }

                foreach (var key in entity.GetForeignKeys())
                {
                    key.SetConstraintName(sc(key.GetConstraintName()));
                }

                foreach (var index in entity.GetIndexes())
                {
                    index.SetDatabaseName(sc(index.GetDatabaseName()));
                }
            }
        }
コード例 #19
0
ファイル: DbContextParser.cs プロジェクト: paillave/Etl.Net
        public static EntitySummary CreateEntitySummary(IEntityType entityType)
        {
            var mapping = entityType.GetEntityEssentials();

            mapping.Comment = entityType.GetComment();
            var storeObject = StoreObjectIdentifier.Create(entityType, mapping.IsView ? StoreObjectType.View : StoreObjectType.Table).GetValueOrDefault();

            mapping.Properties = entityType.GetDeclaredProperties().Where(i => !i.IsShadowProperty()).Select(i => CreatePropertySummary(i, storeObject)).ToList();
            return(mapping);
        }
コード例 #20
0
        public void Column_name_turkish_culture()
        {
            var entityType = BuildEntityType(
                "SimpleBlog",
                e => e.Property <int>("SimpleBlogId"),
                CultureInfo.CreateSpecificCulture("tr-TR"));

            Assert.Equal("simple_blog_ıd", entityType.FindProperty("SimpleBlogId")
                         .GetColumnName(StoreObjectIdentifier.Create(entityType, StoreObjectType.Table) !.Value));
        }
コード例 #21
0
        public void Owned_entity_withs_OwnsMany()
        {
            var model           = BuildModel(b => b.Entity <Blog>().OwnsMany(b => b.Posts));
            var ownedEntityType = model.FindEntityType(typeof(Post));

            Assert.Equal("post", ownedEntityType.GetTableName());
            Assert.Equal("pk_post", ownedEntityType.FindPrimaryKey().GetName());
            Assert.Equal("post_title", ownedEntityType.FindProperty("PostTitle")
                         .GetColumnName(StoreObjectIdentifier.Create(ownedEntityType, StoreObjectType.Table) !.Value));
        }
コード例 #22
0
        public void Owned_entity_without_table_splitting()
        {
            var model = BuildModel(b =>
                                   b.Entity <Owner>().OwnsOne(o => o.Owned).ToTable("another_table"));

            var ownedEntityType = model.FindEntityType(typeof(Owned));

            Assert.Equal("pk_another_table", ownedEntityType.FindPrimaryKey().GetName());
            Assert.Equal("another_table", ownedEntityType.GetTableName());
            Assert.Equal("owned_property", ownedEntityType.FindProperty("OwnedProperty")
                         .GetColumnName(StoreObjectIdentifier.Create(ownedEntityType, StoreObjectType.Table) !.Value));
        }
        /// <summary>
        ///     This is an internal API that supports the Entity Framework Core infrastructure and not subject to
        ///     the same compatibility standards as public APIs. It may be changed or removed without notice in
        ///     any release. You should only use it directly in your code with extreme caution and knowing that
        ///     doing so can result in application failures when updating to a new Entity Framework Core release.
        /// </summary>
        public virtual SqlServerSequenceValueGeneratorState GetOrAddSequenceState(
            IProperty property,
            IRelationalConnection connection)
        {
            var sequence = property.FindHiLoSequence(
                StoreObjectIdentifier.Create(property.DeclaringEntityType, StoreObjectType.Table).Value);

            Check.DebugAssert(sequence != null, "sequence is null");

            return(_sequenceGeneratorCache.GetOrAdd(
                       GetSequenceName(sequence, connection),
                       sequenceName => new SqlServerSequenceValueGeneratorState(sequence)));
        }
コード例 #24
0
        public void Column_on_view()
        {
            var entityType = BuildEntityType(b => b.Entity <SampleEntity>(
                                                 e =>
            {
                e.ToTable("SimpleBlogTable");
                e.ToView("SimpleBlogView");
                e.ToFunction("SimpleBlogFunction");
            }));

            foreach (var type in new[] { StoreObjectType.Table, StoreObjectType.View, StoreObjectType.Function })
            {
                Assert.Equal("sample_entity_id", entityType.FindProperty(nameof(SampleEntity.SampleEntityId))
                             .GetColumnName(StoreObjectIdentifier.Create(entityType, type) !.Value));
            }
        }
コード例 #25
0
        public NpgsqlCopyHelper(DbContext context, bool autoTransactions = true)
        {
            var type = context.Model.FindEntityType(typeof(T));
            var r = type.GetTableName();
            defaultTableName = $"\"{r}\"";
            schema = type.GetSchema();
            var objectIdentifier = StoreObjectIdentifier.Create(type, StoreObjectType.Table);
            var props = type.GetProperties();
            foreach (var prop in props)
            {
                var mi = new MappingInfo(prop, objectIdentifier);
                mappingList.Add(mi);
            }

            this.autoTransactions = autoTransactions;
        }
コード例 #26
0
        public void Column_name_on_view()
        {
            var entityType = BuildEntityType("SimpleBlog", e =>
            {
                e.ToTable("SimpleBlogTable");
                e.ToView("SimpleBlogView");
                e.ToFunction("SimpleBlogFunction");
                e.Property <int>("SimpleBlogId");
            });

            foreach (var type in new[] { StoreObjectType.Table, StoreObjectType.View, StoreObjectType.Function })
            {
                Assert.Equal("simple_blog_id", entityType.FindProperty("SimpleBlogId")
                             .GetColumnName(StoreObjectIdentifier.Create(entityType, type) !.Value));
            }
        }
コード例 #27
0
        /// <summary>
        /// Get column name for passed property type.
        /// </summary>
        /// <param name="memberInfo"></param>
        /// <returns></returns>
        protected virtual string GetColumnName(MemberInfo memberInfo)
        {
            if (!_columnNamesCache.ContainsKey(memberInfo))
            {
                var entityType = Model.FindEntityType(memberInfo.DeclaringType);
                var property   = entityType.FindProperty(memberInfo.Name);
                var identifier = (StoreObjectIdentifier)StoreObjectIdentifier.Create(entityType, StoreObjectType.Table);
                _columnNamesCache.Add(memberInfo, property.GetColumnName(identifier));
            }

            if (!_columnNamesCache.TryGetValue(memberInfo, out var columnName))
            {
                throw new InvalidOperationException($"Column name for member {memberInfo.Name} is not defined in model");
            }
            return(columnName);
        }
コード例 #28
0
        public void Owned_entity_with_table_splitting()
        {
            var model = BuildModel(b =>
            {
                b.Entity("SimpleBlog", e =>
                {
                    e.OwnsOne("OwnedEntity", "Nav", o => o.Property <int>("OwnedProperty"));
                });
            });

            var entityType = model.FindEntityType("OwnedEntity");

            Assert.Equal("pk_simple_blog", entityType.FindPrimaryKey().GetName());
            Assert.Equal("simple_blog", entityType.GetTableName());
            Assert.Equal("owned_property", entityType.FindProperty("OwnedProperty")
                         .GetColumnName(StoreObjectIdentifier.Create(entityType, StoreObjectType.Table) !.Value));
        }
コード例 #29
0
ファイル: TriggerConvention.cs プロジェクト: donhuvy/efcore
    private static bool AreCompatible(IConventionTrigger checkConstraint, IConventionTrigger baseTrigger)
    {
        var baseTable = StoreObjectIdentifier.Create(baseTrigger.EntityType, StoreObjectType.Table);

        if (baseTable == null)
        {
            return(true);
        }

        if (checkConstraint.GetName(baseTable.Value) != baseTrigger.GetName(baseTable.Value) &&
            checkConstraint.GetNameConfigurationSource() is ConfigurationSource nameConfigurationSource &&
            !nameConfigurationSource.OverridesStrictly(baseTrigger.GetNameConfigurationSource()))
        {
            return(false);
        }

        return(true);
    }
        public static string GetEndDateColumnName(this IEntityType entityType)
        {
            if (entityType.HasTemporalTable() == false)
            {
                return(null);
            }

            foreach (var property in entityType.GetProperties())
            {
                var a = property.FindAnnotation(TemporalAnnotationNames.SysEndDate);
                if (a != null)
                {
                    var identifier = StoreObjectIdentifier.Create(entityType, StoreObjectType.Table);
                    return(property.GetColumnName(identifier.Value));
                }
            }

            return(TemporalAnnotationNames.DefaultEndTime);
        }