[英]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.