如何在Entity Framework -6中的onmodelcreating方法中映射与模型不同的表名和列名? [英] How to map table names and column name different from model in onmodelcreating method in Entity Framework -6?

查看:376
本文介绍了如何在Entity Framework -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

这篇关于如何在Entity Framework -6中的onmodelcreating方法中映射与模型不同的表名和列名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆