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

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

在使用.NET CORE 进行 Web 开发的时候会考虑到使用不同数据库的情况,并且在每种数据库建立表结构的时候会采用不同的命名规则。之前的解决方法是使用 [ColumnAttribute] 或者 [TableAttribute] 这种特性来显式标注不同的列名。

[Table("bas_stock_address")]public class BasStockAddress : FullAuditedEntity
{ ///
/// 仓库ID /// [Column("stock_id")] public int StockId { get; set; } ///
/// 备注 /// [Column("remark")] [StringLength(200)] public string Remark { get; set; }}

这种情况的话就很尴尬,如果实体一多,就要对每个属性进行标注的话,工作量确实会很大。

这个时候就可以使用 Conventions 来处理这种情况,如果是 EntityFramework 6.x 的话可以使用:

提到的方法来进行操作。

如果是 EntityFrameworkCore 的话可以通过 ModelBuilder.Model.GetEntityTypes() 获得所有实体对象的类型以及他的相关属性,并且使用 Relational() 方法来获得实体对象的 Annotation ,这样就可以对生成的表结构进行统一的配置。

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 这个扩展方法,他将 NameSnake 这种形式的名称替换为 name_snake。

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

你可能感兴趣的文章
No 'Access-Control-Allow-Origin' header is present on the requested resource.
查看>>
No 'Access-Control-Allow-Origin' header is present on the requested resource.
查看>>
NO 157 去掉禅道访问地址中的zentao
查看>>
no available service ‘default‘ found, please make sure registry config corre seata
查看>>
No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
查看>>
no connection could be made because the target machine actively refused it.问题解决
查看>>
No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
查看>>
No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
查看>>
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 'pandads'
查看>>
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 qualifying bean of type ‘com.netflix.discovery.AbstractDiscoveryClientOptionalArgs<?>‘ available
查看>>
No resource identifier found for attribute 'srcCompat' in package的解决办法
查看>>