Skip to content

Instantly share code, notes, and snippets.

@DiomedesDominguez
Last active July 14, 2023 22:53
Show Gist options
  • Save DiomedesDominguez/797711e093fd15eff4265ae9d5dc27b9 to your computer and use it in GitHub Desktop.
Save DiomedesDominguez/797711e093fd15eff4265ae9d5dc27b9 to your computer and use it in GitHub Desktop.
The main goal of ModelBuilderExtentions.cs is to use DataAnnotations more in our tables instead of using Fluent. The other files are mere examples.
namespace MyDotNetCore.App
{
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
public class ApplicationDbContext : IdentityDbContext
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.SetDefaultValuesTableName();
}
}
}
namespace Microsoft.EntityFrameworkCore
{
using System;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Reflection;
public static class ModelBuilderExtensions
{
public static void SetDefaultValuesTableName(this ModelBuilder modelBuilder)
{
foreach (var entity in modelBuilder.Model.GetEntityTypes().Where(x => x.ClrType.GetCustomAttribute(typeof(TableAttribute)) != null))
{
var entityClass = entity.ClrType;
var tAttr = entityClass.GetCustomAttribute(typeof(TableAttribute)) as TableAttribute;
modelBuilder.Entity(entityClass).ToTable(tAttr.Name);
foreach (var property in entityClass.GetProperties().Where(p => (p.PropertyType == typeof(DateTime) || p.PropertyType == typeof(Guid)) && p.CustomAttributes.Any(a => a.AttributeType == typeof(DatabaseGeneratedAttribute) && !p.CustomAttributes.Any(c=>c.AttributeType == typeof(DefaultValueAttribute)))))
{
var defaultValueSql = "GetDate()";
if (property.PropertyType == typeof(Guid))
{
defaultValueSql = "newsequentialid()";
}
modelBuilder.Entity(entityClass).Property(property.Name).HasDefaultValueSql(defaultValueSql);
}
foreach (var property in entityClass.GetProperties().Where(p => p.CustomAttributes.Any(a => a.AttributeType == typeof(DefaultValueAttribute))))
{
var pAttr = property.GetCustomAttribute(typeof(DefaultValueAttribute)) as DefaultValueAttribute;
modelBuilder.Entity(entityClass).Property(property.Name).HasDefaultValueSql(pAttr.Value.ToString());
}
}
}
}
}
//Use DataAnnotations for your properties in your classes.
[Table("CustomTableName")]
public class MyTable
{
[Key, DefaultValue("NEWSEQUENTIALID()")]
[Column(Order = 0)]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid Id { get; set; }
[Column(TypeName = "datetime2(7)"), DefaultValue("SYSTEMDATIME()")]
public DateTime LastUpdated { get; set; }
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public DateTime Created {get; set;}
[MaxLength(15), Column(TypeName = "varchar(15)")]
[DefaultValue("'127.0.0.1'")]
public string ClientIp { get; set; }
}
@SF-Simon
Copy link

SF-Simon commented Mar 1, 2023

Thank you.
This writing is very comfortable and I like it very much. If you can, please consider nullable types and other database support, such as Sqlite.

@DiomedesDominguez
Copy link
Author

DiomedesDominguez commented Mar 1, 2023

Thank you. This writing is very comfortable and I like it very much. If you can, please consider nullable types and other database support, such as Sqlite.

Thanks for the suggestion. I'll create a repo for that. Go to https://github.com/DiomedesDominguez/upgraded-enigma

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment