Does the Table attribute not work in Entity Framework Core 1.1?

Asked 2 months ago, Updated 2 months ago, 8 views

If the table name of the DB already exists is "mytable", I attribute it as follows, but it doesn't recognize it.
What should I do?

Hoge.cs

 [Table("mytable")]
public class Hoge {
...
}

MyDbContext.cs

public class MyDbContext:DbContext{
public DbSet<Hoge>Hoge {get;set;}
...
}

Startup.cs

services.AddDbContext<MyDbContext>();

Internal Exception

Table'mydatabase.hoge'doesn't exist

c# entity-framework

2022-09-30 12:08

1 Answers

I can only think of two things, but

The first is DbContext's OnModelCreating method.

modelBuilder.Entity<Hoge>().ToTable("Hoge");

You may have a code similar to .
This takes precedence, so you need to delete the ToTable.

Second, you may be using the wrong Table attribute on Hoge.cs.

using System.ComponentModel.DataAnnotations.Schema;

Do you use ↑ as the table attribute?


2022-09-30 12:08

If you have any answers or tips


© 2022 OneMinuteCode. All rights reserved.