Skip to content

Instantly share code, notes, and snippets.

@szymczakk
Created January 8, 2021 16:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save szymczakk/5d1b56b4e797ad94f4b462e0868872d4 to your computer and use it in GitHub Desktop.
Save szymczakk/5d1b56b4e797ad94f4b462e0868872d4 to your computer and use it in GitHub Desktop.
[EF Core] Set column type, if not defined in Column attribute
private void SetColumnTypeIfNoColumnAttribute(ModelBuilder builder, string sqlType, params Type[] types)
{
foreach (var property in builder.Model.GetEntityTypes()
.SelectMany(t => t.GetProperties())
.Where(p => types.Contains(p.ClrType))
.Where(p => !Attribute.IsDefined(p.PropertyInfo, typeof(ColumnAttribute))))
{
property.SetColumnType(sqlType);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment