Code-first: Mapping entities to existing database tables

元气小坏坏 提交于 2019-12-03 05:25:07
AlexC

You can use the

[Table("Project")] 
public class Project {
....
}

annotation against the Project entity, or in the OnModelCreating(DbModelBuilder modelBuilder) you can call modelBuilder.Entity<Project>().ToTable("Project");.

Both would do the same thing.

user3041160

You should define a class (ie:ProjectMap) that inherits from the generic class EntityTypeConfiguration(T) where T is here your Project class. In this ProjectMap class, you can define explicitly a table mapping :

this.ToTable("Project", "dbo");

The class ProjectMap should be called in the following method of your DbContext class

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Configurations.Add(new ProjectMap());
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!