How to map table names and column name different from model in onmodelcreating method in Entity Framework -6?(如何在实体框架-6的onmodelcreating方法中映射与模型不同的表名和列名?)
问题描述
我的数据库的表名以tbl"前缀开头,列名如ua_id"在项目上下文中是可以理解的,但如果在模型中使用会出现问题,即名称应该是可读或有意义的(不像定义的指示性名称在数据库中).
I have my database with table names starting with "tbl" prefix and column names like "ua_id" which are understandable in context of project but problematic if used in a model i.e names should be readable or meaningful(not like indicative names defined in database).
所以我想将它们映射到我的 onmodelcreating 方法中,但我对此一无所知.我在以下博客中研究过它:
http://weblogs.asp.net/scottgu/entity-framework-4-code-first-custom-database-schema-mapping但这适用于 EF 4.1,方法不适用于 EF 6.(mapsingletype 方法)
So I want to map them in my onmodelcreating method but I have no idea about it. I studied it in following blog:
http://weblogs.asp.net/scottgu/entity-framework-4-code-first-custom-database-schema-mapping
but this is for EF 4.1 and method doesn't work for EF 6.(mapsingletype method)
我想按列将表格映射到我的模型,因为我无法更改列名.我只想要博客中该语法的更新版本.
I want to map my tables by columns to my model as I can't change the column names. I just want the newer version of that syntax in the blog.
谢谢.
推荐答案
如果您使用 Code First,您可以简单地使用 Table 和 Column 属性装饰您的模型,并为其指定数据库名称.
If you are using Code First, you can simply decorate your model with Table and Column attribute and give it the database names.
[Table("tbl_Student")]
public class Student
{
[Column("u_id")]
public int ID { get; set; }
}
这些属性在 System.ComponentModel.DataAnnotations.Schema 命名空间中可用
These attributes are available in the System.ComponentModel.DataAnnotations.Schema namespace
这篇关于如何在实体框架-6的onmodelcreating方法中映射与模型不同的表名和列名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在实体框架-6的onmodelcreating方法中映射与模型不同的表名和列名?


- C#MongoDB使用Builders查找派生对象 2022-09-04
- C# 中多线程网络服务器的模式 2022-01-01
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
- 如何用自己压缩一个 IEnumerable 2022-01-01
- 输入按键事件处理程序 2022-01-01
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- MoreLinq maxBy vs LINQ max + where 2022-01-01
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
- WebMatrix WebSecurity PasswordSalt 2022-01-01