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方法中映射与模型不同的表名和列名?
基础教程推荐
- 禁止输入少量字符,例如'<'、'&a 2022-01-01
- 如果有人提交恶意软件Nuget包怎么办? 2022-01-01
- 当值可以是对象或空数组时反序列化 JSON 2022-01-01
- Azure Functions:CosmosDBTrigger 未在 Visual Studio 中触发 2022-01-01
- 我应该在后面的代码中直接使用 Linq To SQL 还是使 2022-01-01
- 如何使用 .Net 检查 Active Directory 服务器是否已启动并正在运行? 2022-01-01
- WPF 模态进度窗口 2022-01-01
- Moq It.Is<>不匹配 2022-01-01
- C# 从 List<List<int>> 中删除重 2022-01-01
- .NET SerialPort DataReceived 事件未触发 2022-01-01
