win7user10 / Laraue.EfCoreTriggers

Library to write triggers in C# with EF.Core
MIT License
112 stars 20 forks source link

Is it possible to change column name in trigger script to snake case? #8

Closed praiwann closed 3 years ago

praiwann commented 3 years ago

Hi,

As mention in topic, can we configure the library or is there a way to use snake case as column name?

I have run migration and its seems to working fine but there is an error when trigger occur as I have use snake case column name in my database as default.

for example, if I have these column name in my DB

id, some_id, some_value

and in my code like so

public class SomeClass
{
   public Guid Id { get; set; }
   public Guid SomeId { get; set; }
   public string SomeValue { get; set; }
}

It could not map column name because the trigger script generate the column as

Id, SomeId, SomeValue

which will translate to

id, someid, somevalue

BTW, I have use PostgreSQL as my database.

win7user10 commented 3 years ago

Hi, @praiwann, column names should be generated using EF. In my tests https://github.com/win7user10/Laraue.EfCoreTriggers/blob/master/tests/Laraue.EfCoreTriggers.PostgreSqlTests/PostgreSqlGeneratingExpressionsTests.cs you can see that the property is correctly translated to snake case convention. How do you describe to EF, how will be named a column?

praiwann commented 3 years ago

Hi @win7user10,

Unfortunately, I have use npgsql library and its snake case tranlsator NpgsqlSnakeCaseNameTranslator

We have implemented this to our core module and use them in many projects. BTW, from the test which you mention, it seems that we are working on different extension, in your case directly using EFCore.NamingConventions. So I will check that first and see if it is working as aspect then better try to switch to this extension for more EFCore compatible.

I will post the result here later. Thank you :)

win7user10 commented 3 years ago

Hi, @praiwann, the latest version of the library have the opportunity to customize any part of a code generation.

private class CustomPostgreSqlProvider : PostgreSqlProvider
{
    public MySqlProvider(IModel model) : base(model)
    {
    }

    protected override string GetColumnName(MemberInfo memberInfo)
    {
        return base.GetColumnName(memberInfo).ToSnakeCase();
    }
}
var options = new DbContextOptionsBuilder<TestDbContext>()
    .UseNpgsql("User ID=test;Password=test;Host=localhost;Port=5432;Database=test;")
    .UseTriggers<CustomPostgreSqlProvider>()
    .Options;

var dbContext = new TestDbContext(options);

It can be useful in your case.

praiwann commented 3 years ago

Hi, @praiwann, the latest version of the library have the opportunity to customize any part of a code generation.

private class CustomPostgreSqlProvider : PostgreSqlProvider
{
    public MySqlProvider(IModel model) : base(model)
    {
    }

    protected override string GetColumnName(MemberInfo memberInfo)
    {
        return base.GetColumnName(memberInfo).ToSnakeCase();
    }
}
var options = new DbContextOptionsBuilder<TestDbContext>()
    .UseNpgsql("User ID=test;Password=test;Host=localhost;Port=5432;Database=test;")
    .UseTriggers<CustomPostgreSqlProvider>()
    .Options;

var dbContext = new TestDbContext(options);

It can be useful in your case.

@win7user10 Appreciate your works!!