Last active
July 14, 2023 22:53
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | |
} | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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; } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for the suggestion. I'll create a repo for that. Go to https://github.com/DiomedesDominguez/upgraded-enigma