Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why was Relational() extention method removed in .net core 3?

The EF Core 2.0 had an extension method called Relational in the IMutableEntityTypeinterface.

Pluralizer pluralizer = new Pluralizer();
foreach (IMutableEntityType entityType in modelBuilder.Model.GetEntityTypes())
{
    string tableName = entityType.Relational().TableName;
    entityType.Relational().TableName = pluralizer.Pluralize(tableName);
} 

I was able to pluralize table names using it and with the help of the Pluralizer library.

But in .NET Core 3.0, this method does not exist.

Can anyone help me out and give me a brief explanation?

like image 652
Hamed Hajiloo Avatar asked Nov 30 '19 07:11

Hamed Hajiloo


People also ask

When would you use EF6 vs EF core?

Keep using EF6 if the data access code is stable and not likely to evolve or need new features. Port to EF Core if the data access code is evolving or if the app needs new features only available in EF Core. Porting to EF Core is also often done for performance.

What is the latest version of entity Framework Core?

The most recent Entity Framework Core 6.0 (EF Core 6) was released on 10 November 2021.

Is an optional dependent using table sharing without any required non shared property that could be used to identify whether the entity exists?

The entity type 'ProductStockRequirements' is an optional dependent using table sharing without any required non shared property that could be used to identify whether the entity exists. If all nullable properties contain a null value in database then an object instance won't be created in the query.


2 Answers

The syntax has been changed a little bit in EF Core 3 according to this issue, here is the new version:

Pluralizer pluralizer = new Pluralizer();
foreach (IMutableEntityType entityType in modelBuilder.Model.GetEntityTypes())
{
    string tableName = entityType.GetTableName();
    entityType.SetTableName(pluralizer.Pluralize(tableName));
}
like image 187
Moien Tajik Avatar answered Nov 03 '22 03:11

Moien Tajik


foreach (var entity in modelBuilder.Model.GetEntityTypes())
        {
            // Replace table names
            //entity.Relational().TableName = entity.Relational().TableName.ToSnakeCase();
            entity.SetTableName(entity.GetTableName().ToSnakeCase());

            // Replace column names            
            foreach (var property in entity.GetProperties())
            {
                property.SetColumnName(property.Name.ToSnakeCase());
            }

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

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

            foreach (var index in entity.GetIndexes())
            {
                index.SetName(index.GetName().ToSnakeCase());
            }
        }
like image 38
Fokiruna Avatar answered Nov 03 '22 05:11

Fokiruna