简体   繁体   English

如何使用 Entity Framework Code First Fluent API 指定表名

[英]How to specify table name with Entity Framework Code First Fluent API

I have an Entity and I am to configure Entity Framework to map it to a database table with different name.我有一个实体,我要配置实体框架以将其映射到具有不同名称的数据库表。

I can easily do this with Code First DataAnnotations ( DataAnnotations.Schema.TableAttribute ).我可以使用Code First DataAnnotations ( DataAnnotations.Schema.TableAttribute ) 轻松做到这一点。

But due to limitations now I have to use Code First Fluent API (my domain objects will be used by external clients, so they shouldn't be technology-specific - eg have any references to DataAnnotations)但是由于现在的限制,我必须使用Code First Fluent API (我的域对象将由外部客户端使用,因此它们不应该是特定于技术的 - 例如有任何对 DataAnnotations 的引用)

I've searched on MSDN but found nothing.我在 MSDN 上搜索过,但一无所获。 So is it possible and how?那么有可能吗?

Thank you.谢谢。

You can also use the Table annotation:您还可以使用 Table 注释:

[Table("InternalBlogs")]
public class Blog

See: Code First Data Annotations请参阅: 代码优先数据注释

You'll use the .ToTable() method:您将使用.ToTable()方法:

modelBuilder.Entity<Department>().ToTable("t_Department");   

Source: MSDN: http://msdn.microsoft.com/en-us/data/jj591617.aspx来源:MSDN: http : //msdn.microsoft.com/en-us/data/jj591617.aspx

Use ToTable method:使用 ToTable 方法:

public class MyEntityMap : EntityTypeConfiguration<MyEntity>
{
    public const string TableName = "MyEntity";

    public MyEntityMap()
    {                   
        ToTable(TableName);

        Property(t => t.Id);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 c# - 如何通过实体框架代码的流畅API首先在c#中更改表名? - how can i change table name by fluent API of entity framework code first in c#? 实体框架4代码优先自定义表映射Fluent API问题 - Entity Framework 4 Code First Custom table mapping Fluent API issue 实体框架6代码优先Fluent API表映射 - Entity Framework 6 Code First Fluent API Table Mapping 如何使用Entity Framework Code First Fluent API仅在连接表上级联删除 - How to cascade delete only on join table using Entity Framework Code First Fluent API 实体框架代码第一流畅的api - entity framework code first fluent api 实体框架代码优先:如何指定索引名称 - Entity Framework Code First: How to specify the index name 如何使用 Entity Framework 7 通过流畅的 API 指定外键列名? - How to specify foreign key column name, via fluent API, with Entity Framework 7? 实体框架5.0,代码优先,关系,流利的API和播种数据库 - Entity Framework 5.0, Code First, relationships, fluent API and seeding the database 使用Fluent API的Entity Framework 6代码优先关系 - Entity Framework 6 code-first relations with Fluent API Entity Framework Code First Fluent Api:向列添加索引 - Entity Framework Code First Fluent Api: Adding Indexes to columns
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM