UseSnakeCaseNamingConvention() Does not Handle Owned Entities Table Names

See original GitHub issue

For example, for a context like this

    public class Account
    {
        public Guid Id { get; set; }
        public string Email { get; set; }
        public byte[] PasswordHash { get; set; }
        public List<RefreshToken> RefreshTokens { get; set; }

        public bool OwnsToken(string token) 
        {
            return this.RefreshTokens?.Find(x => x.Token == token) != null;
        }
    }

    [Owned]
    public class RefreshToken
    {
        [Key]
        public Guid Id { get; set; }
        public Account Account { get; set; }
        public string Token { get; set; }
        public DateTime Expires { get; set; }
    }

    public class DataContext : DbContext
    {
        public DbSet<Account> Accounts { get; set; }

        public DataContext(DbContextOptions<DataContext> options) : base(options)
        {

        }

        protected override void OnConfiguring(DbContextOptionsBuilder options)
        {
            options.UseSnakeCaseNamingConvention();
        }
    }

I get initial migration with table names like this:

        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.CreateTable(
                name: "accounts",
                columns: table => new
                {
                    id = table.Column<Guid>(nullable: false),      
                    email = table.Column<string>(nullable: true),
                    password_hash = table.Column<byte[]>(nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("pk_accounts", x => x.id);
                });

            migrationBuilder.CreateTable(
                name: "RefreshToken",
                columns: table => new
                {
                    id = table.Column<Guid>(nullable: false),
                    account_id = table.Column<Guid>(nullable: false),
                    token = table.Column<string>(nullable: true),
                    expires = table.Column<DateTime>(nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("pk_refresh_token", x => x.id);
                    table.ForeignKey(
                        name: "fk_refresh_token_accounts_account_id",
                        column: x => x.account_id,
                        principalTable: "accounts",
                        principalColumn: "id",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateIndex(
                name: "ix_refresh_token_account_id",
                table: "RefreshToken",
                column: "account_id");
        }

I would expect table name to be refresh_tokens for entity RefreshToken. And it would be generated correctly if I remove [Owned] attribute on RefreshToken entity.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:3
  • Comments:12 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
andrecastelianocommented, Mar 10, 2021

The issue still exists on release 5.0.2 if you put [Owned] on a type, the naming convention does not get applied to the table.

1reaction
rojicommented, Jul 12, 2020

Thanks @twinmind, I’ll try take a look soon. There have been many changes from 3.1 to 5.0, so unfortunately I’m not sure I’ll have time to release a fixed version that works with 3.1 - but let’s see.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Naming convention not applied to tables in ASP.NET Identity
UseSnakeCaseNamingConventions () the result of Migrations shows: all columns have changed to snaked mode but table names keep the CamelCase.
Read more >
net core entity framework (EF Core) table naming convention
This is a NuGet package that handles the naming convention as simple as ... UseSnakeCaseNamingConvention: FullName becomes full_name ...
Read more >
Customising ASP.NET Core Identity EF Core naming ...
You can look through all the entities in EF Core's model, and change the table names, column names, keys, and indexes to use...
Read more >
Table and Column Naming
Table and Column Naming. By default, EF Core will map to tables and columns named exactly after your .NET classes and properties, so...
Read more >
How to use Postgres SQL, NoSql and Entity Framework Core
I'll show you step by step how to work with relational and non-relational database at the same time with Postgres and support for...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found