博客
关于我
[EFCore]EntityFrameworkCore Code First 当中批量自定义列名
阅读量:442 次
发布时间:2019-03-06

本文共 1783 字,大约阅读时间需要 5 分钟。

在使用.NET Core进行Web开发时,处理不同数据库的命名规则是一个常见的任务。传统的做法是通过显式标注,如[ColumnAttribute]或[TableAttribute],来定义列名和表名。然而,这种方法在面对多个实体时,会导致大量重复工作,效率显然不高。

对于Entity Framework Core开发者来说,可以通过Conventions来自动化这种配置。这种方法允许我们统一管理实体的表结构,而不必为每个属性手动添加特性。以下是一个典型的实现示例:

public class ApplicationDbContext : IdentityDbContext{    public ApplicationDbContext(DbContextOptions options) : base(options) { }    protected override void OnModelCreating(ModelBuilder builder)    {        base.OnModelCreating(builder);        foreach (var entity in builder.Model.GetEntityTypes())        {            entity.Relational().TableName = entity.Relational().TableName.ToSnakeCase();            foreach (var property in entity.GetProperties())            {                property.Relational().ColumnName = property.Name.ToSnakeCase();            }            foreach (var key in entity.GetKeys())            {                key.Relational().Name = key.Relational().Name.ToSnakeCase();            }            foreach (var key in entity.GetForeignKeys())            {                key.Relational().Name = key.Relational().Name.ToSnakeCase();            }            foreach (var index in entity.GetIndexes())            {                index.Relational().Name = index.Relational().Name.ToSnakeCase();            }        }    }}public static class StringExtensions{    public static string ToSnakeCase(this string input)    {        if (string.IsNullOrEmpty(input)) return input;        return Regex.Replace(input, @"([a-z0-9])([A-Z])", "$1_$2").ToLower();    }}

这个实现中,ToSnakeCase方法用于将camelCase命名转换为snake_case格式。这对于数据库系统中常见的命名习惯尤为重要。通过这种方式,可以确保实体类中的属性、键和索引名称与数据库中实际使用的命名规则保持一致。

这种方法的优势在于,它能够统一管理所有实体的表结构配置,减少了手动操作的复杂性。同时,OnModelCreating方法中的循环遍历所有实体类型,确保每个实体都按照预期的命名规则进行配置。

这种方法不仅提高了开发效率,还可以减少命名冲突和数据一致性的问题。对于大型项目而言,这种自动化配置方法尤为实用。

转载地址:http://ekryz.baihongyu.com/

你可能感兴趣的文章
No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
查看>>
No mapping found for HTTP request with URI [/...] in DispatcherServlet with name ...的解决方法
查看>>
No mapping found for HTTP request with URI [/logout.do] in DispatcherServlet with name 'springmvc'
查看>>
No module named 'crispy_forms'等使用pycharm开发
查看>>
No module named cv2
查看>>
No module named tensorboard.main在安装tensorboardX的时候遇到的问题
查看>>
No module named ‘MySQLdb‘错误解决No module named ‘MySQLdb‘错误解决
查看>>
No new migrations found. Your system is up-to-date.
查看>>
No qualifying bean of type XXX found for dependency XXX.
查看>>
No resource identifier found for attribute 'srcCompat' in package的解决办法
查看>>
no session found for current thread
查看>>
No toolchains found in the NDK toolchains folder for ABI with prefix: mips64el-linux-android
查看>>
NO.23 ZenTaoPHP目录结构
查看>>
no1
查看>>
NO32 网络层次及OSI7层模型--TCP三次握手四次断开--子网划分
查看>>
NoClassDefFoundError: org/springframework/boot/context/properties/ConfigurationBeanFactoryMetadata
查看>>
Node JS: < 一> 初识Node JS
查看>>
Node Sass does not yet support your current environment: Windows 64-bit with Unsupported runtime(72)
查看>>
node, nvm, npm,pnpm,以前简单的前端环境为什么越来越复杂
查看>>
Node-RED中使用JSON数据建立web网站
查看>>