我使用table属性来更改表名,因此它不是查询Books表(默认情况下),而是查找BookList表。
<Table(Name:="BookList")>
Public Class Book
<Key()>
Public Property ID() As Integer
End Class
但是,当我运行程序并尝试获取第一条记录时,会产生错误:
Invalid object name 'dbo.Books'
如何将Book类映射到BookList表?
答案 0 :(得分:1)
您可以使用EntityTypeConfiguration类来映射表。
public class Book_Mapping : EntityTypeConfiguration<Book>
{
public Book_Mapping()
{
HasKey(x => x.Id);
ToTable("BookList");
// etc.
}
}
public class EntitiesContext : DbContext
{
public EntitiesContext()
: base("defaultConnection")
{
}
public DbSet<Book> Books { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Configurations.Add(new Book_Mapping());
}
}