C# 通过自定义特性 实现根据实体类自动创建数据库表
Id字段上的DbKey就是自定义特性
/// <summary>
/// 用户信息
/// </summary>
public class User
{
[DbKey]
public string Id { get; set; }
public string Name { get; set; }
}
继承Attribute,实现自定义特性DbKey
namespace CustomerAttribute
{
/// <summary>
/// 数据库主键
/// </summary>
public class DbKey : Attribute
{
public string Description { get; set; } public DbKey()
{ } public DbKey(string description)
{
this.Description = description;
}
}
}
namespace CustomerAttribute
{
/// <summary>
/// 用户信息
/// </summary>
public class User
{
[DbKey]
public string Id { get; set; }
public string Name { get; set; }
} /// <summary>
/// 用户角色
/// </summary>
public class UserRole
{
[DbKey("用户ID")]
public string UserId { get; set; }
[DbKey("角色ID")]
public string RoleId { get; set; }
}
}
namespace CustomerAttribute
{
class Program
{
/// <summary>
/// 获取数据库主键字段
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
private static IEnumerable<PropertyInfo> getDbKeyFields<T>()
{
// 获取当前类中的公共字段
var fields = typeof(T).GetProperties(); // 查找有DbKey特性的字段
var keyFields = fields.Where(field => (DbKey)Attribute.GetCustomAttribute(field, typeof(DbKey)) != null); return keyFields;
} private static string getDescription(PropertyInfo field)
{
string result = string.Empty;
var dbKey = (DbKey)Attribute.GetCustomAttribute(field, typeof(DbKey));
if (dbKey != null) result = dbKey.Description;
return result;
} static void Main(string[] args)
{
try
{
var userKeyFields = getDbKeyFields<User>();
Console.WriteLine("User表的主键为:" + string.Join(",", userKeyFields.Select(field => field.Name))); var userRoleKeyFields = getDbKeyFields<UserRole>();
Console.WriteLine("UserRole表的主键为:" + string.Join(",", userRoleKeyFields.Select(field => field.Name))); foreach (PropertyInfo field in userRoleKeyFields)
{
string description = getDescription(field);
Console.WriteLine(string.Format("{0}字段的描述信息为:{1}", field.Name, description));
}
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
finally
{
Console.ReadLine();
}
}
}
}
一些Orm的实现,就是通过解析特性信息,动态生成数据库表
C# 通过自定义特性 实现根据实体类自动创建数据库表的更多相关文章
- EntityFrameworkCore 根据实体类自动创建数据库
1.首先新建 Asp.Net Core WebApi 项目 2.添加一下引用 : 2.1 Pomelo.EntityFrameworkCore.MySql(我用的Mysql 根据自己情况引用就行) ...
- 学习MVC之租房网站(三)-编写实体类并创建数据库
在上一篇<学习MVC之租房网站(二)-框架搭建及准备工作>中,搭建好了项目框架,并配置了EF.Log4Net和进程外Session.接下来会编写Eneity类并采用CodeFirst的方式 ...
- Hibrenate实现根据实体类自动创建表或添加字段
Hibernate支持自动建表,在开发阶段很方便,可以保证hbm与数据库表结构的自动同步. 实现: 在配置hibernate的配置文件中将hbm2ddl.auto设置为update,如:Xml代码&l ...
- 阶段3 1.Mybatis_12.Mybatis注解开发_5 mybatis注解建立实体类属性和数据库表中列的对应关系
创建新项目,一对多 复制刚才关闭的项目的文件 复制到们的新项目里面 复制包的依赖 删减相关代码.只保留这三个查询的方法 模糊查询改成传统的占位符的方式 之前是可以自定义实体类的属性字段,和数据库的字典 ...
- Do You Kown Asp.Net Core - 根据实体类自动创建Razor Page CURD页面模板
Scaffolding Template Intro 我们知道在Asp.Net MVC中,如果你使用的EF的DBContext的话,你可以在vs中通过右键解决方案-添加控制器-添加包含视图的控制器,然 ...
- IntelliJ IDEA 2017版 spring-boot 实现jpa基本部署,通过实体类自动建立数据库
一.添加Spring Boot JPA-Hibernate步骤 1.在pom.xml添加mysql,spring-data-jpa依赖 2.在application.properties文件 ...
- java工具类–自动将数据库表生成javabean
最近和数据库的表打交道挺多的,因为暂时做的是接口活. 在这过程中发现要把表转换成对应的javabean类型,字段少的表还行,如果不小心碰到几十个字段的他妈的写起来就有点麻烦了,万一碰到几百个的呢,那不 ...
- Hibernate由model类自动同步数据库表结构
在开发中遇到了个问题,每次测试数据库增加表结构的时候,本地pull下最新代码导致启动报错,上网搜了快速解决办法---->hibernate 配置属性中,hibernate.hbm2ddl.aut ...
- Hibernate根据实体类自动创建表
Hibernate支持自动建表,在开发阶段很方便,可以保证hbm与数据库表结构的自动同步. 如何使用呢?很简单,只要在hibernate.cfg.xml里加上如下代码 Xml代码<propert ...
随机推荐
- parallels无法启动之大乌龙-流水账版
欢迎访问我的blog:blog.thinkinside.me 早上到公司,像往日一样,开电脑倒茶喝水. 回到座位打开parallels desktop,发现不对,打开PD非常的慢.显示正在初始 ...
- BSS Audio® Introduces Full-Bandwidth Acoustic Echo Cancellation Algorithm for Soundweb London Conferencing Processors
BSS Audio® Introduces Full-Bandwidth Acoustic Echo Cancellation Algorithm for Soundweb London Confer ...
- 【面经】Epic: 数据库去重
题目是:有2个10G的数据库,存储了一些string. 2者之间有一些重复的数据.请把它们合并为一个数据库,并且去除重复. 限制:内存是4G 例如: DB1: cmu, ucb, stanford, ...
- 架设laravel
用laravel 架设的用户单点登录授权系统,git clone项目文件后,需要下面的方法初始化,纪录以供项目成员参考 错误信息:`Warning: require(/http/www.mywakav ...
- X509证书中RSA公钥的提取与载入
原文链接: http://blog.chinaunix.net/uid-16515626-id-2741894.html 由于项目需要,我计划利用openssl开发一个基本的CA,实现证书的发放等 ...
- 关于MySQL redo log,挖些坑,慢慢填
1. 为什么可以设置为多个redo log ? (innodb_log_files_in_group,默认值和推荐值都是2,我们线上设的统一为4): 2. 什么条件下会触发刷脏?除了master_th ...
- 转 如何理解 重要性采样(importance sampling)
分类: 我叫学术帖2011-03-25 13:22 3232人阅读 评论(4) 收藏 举报 图形 重要性采样是非常有意 思的一个方法.我们首先需要明确,这个方法是基于采样的,也就是基于所谓的蒙特卡洛法 ...
- POJ 2078 Matrix
Matrix Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 3239 Accepted: 1680 Descriptio ...
- Can't get WebApplicationContext object from ContextRegistry.GetContext(): Resource handler for the 'web' protocol is not defined
I'm stucked in configuring my web.config file under a web forms project in order to get an instance ...
- CentOS安装epel
Centos5安装 rpm -ivh http://dl.fedoraproject.org/pub/epel/5/i386/epel-release-5-4.noarch.rpm rpm ...