Entity Framework - Capitalizing First Property Name Letter

Is first letter capitalization in entity framework models the proper way?

The first two are properties, so they should start with a capital letter, that is called Pascal Case.

Check this link for Capitalization Conventions

Also, if you want to keep your tables and column names with lowercase first letter, EF 6 introduces a new feature called Custom Conventions, so there's no need to add Table attribute to all you classes.

Check this Answer that explains how to set up a "first letter lowecase convention".

Changing default column name for navigation property

Try using MapKey():

modelBuilder.Entity<TreeItem>()
.HasRequired(it => it.Tree)
.WithMany()
.Map(m => m.MapKey("tree_id"));

EDIT

For EF Core, you might have to use a work around as mentioned here:

modelBuilder.Entity<TreeItem>()
.Property<long>("TreeId")
.HasColumnName("tree_id");

Lower-casing the query generated by Entity Framework

I have never tried this but it looks promising for your case and i happen to be outside watching the maple leafs right now. EF allows interception so you can intercept the query and perhaps call ToLower() on the command.CommandText:

class EFCommandInterceptor: IDbCommandInterceptor
{
public void NonQueryExecuted(System.Data.Common.DbCommand command, DbCommandInterceptionContext<int> interceptionContext)
{
LogInfo("NonQueryExecuted", String.Format(" IsAsync: {0}, Command Text: {1}", interceptionContext.IsAsync, command.CommandText));
}

public void NonQueryExecuting(System.Data.Common.DbCommand command, DbCommandInterceptionContext<int> interceptionContext)
{
LogInfo("NonQueryExecuting", String.Format(" IsAsync: {0}, Command Text: {1}", interceptionContext.IsAsync, command.CommandText));
}

public void ReaderExecuted(System.Data.Common.DbCommand command, DbCommandInterceptionContextt<System.Data.Common.DbDataReader> interceptionContext)
{
LogInfo("ReaderExecuted", String.Format(" IsAsync: {0}, Command Text: {1}", interceptionContext.IsAsync, command.CommandText));
}

public void ReaderExecuting(System.Data.Common.DbCommand command, DbCommandInterceptionContext<System.Data.Common.DbDataReader> interceptionContext)
{
LogInfo("ReaderExecuting", String.Format(" IsAsync: {0}, Command Text: {1}", interceptionContext.IsAsync, command.CommandText));
}

public void ScalarExecuted(System.Data.Common.DbCommand command, DbCommandInterceptionContext<object> interceptionContext)
{
LogInfo("ScalarExecuted", String.Format(" IsAsync: {0}, Command Text: {1}", interceptionContext.IsAsync, command.CommandText));
}

public void ScalarExecuting(System.Data.Common.DbCommand command, DbCommandInterceptionContext<object> interceptionContext)
{
LogInfo("ScalarExecuting", String.Format(" IsAsync: {0}, Command Text: {1}", interceptionContext.IsAsync, command.CommandText));
}

private void LogInfo(string command, string commandText)
{
Console.WriteLine("Intercepted on: {0} :- {1} ", command, commandText);
}
}

You can read more here.

API Json response to C# Object with capital case properties first letter

By default, ASP.NET Core encodes all JSON properties names in camel case, to match JSON conventions (see the announcement of the change on GitHub).

If you want to keep the C# conventions, you need to change the default JSON serializer.

In your Startup.cs, configure the MVC part like this (ASP.Net Core 3.0):

services
.AddMvc()
.AddNewtonsoftJson(options =>
{
// don't serialize with CamelCase (see https://github.com/aspnet/Announcements/issues/194)
jsonSettings.ContractResolver = new JsonContractResolver();
});

For ASP.NET Core 2.0 :

services
.AddMvc()
.AddJsonOptions(options =>
{
// don't serialize with CamelCase (see https://github.com/aspnet/Announcements/issues/194)
jsonSettings.ContractResolver = new DefaultContractResolver();
});


Related Topics



Leave a reply



Submit