I'm trying to define some column names using code-first in my entities. I'm using .NET Core 3.1, EF Core 3.1.15 and MySQL server.
I have a relation between entities similar to this situation
I have a City
that belongs to a State
. So it is a one-to-one relation from City
to State
.
Here's an example
public class City
{
[Column("IdCity")]
public short IdCity { get; set; }
[Column("IdState")]
public State State { get; set; }
}
public class State
{
[Column("IdState")]
public byte IdState { get; set; }
}
The issue is that if I run a migration and then persist the database change, it generates the following table
Table City
:
IdCity | StateIdState | ....
Table State
:
IdState | ... | ...
I need the column StateIdState
to be defined as IdState
in the physical database.
I tried using different annotations like FK, column, etc but It did not work, it always generates that name.
Is there a way that I can omit the name of the entity attached to the first part of the column name?
I'm doing this because I already have these two tables created in a server so I need to match their schema in order to interact with them.
From the previous solution I added the following annotation to keep Entities and Foreign keys in sync.
public class City {
[Column("IdCity")]
public short IdCity {get;set;}
[ForeignKey("State")]
public int IdState {get;set;}
[ForeignKey("IdState")]
public virtual State State {get;set;}
}
public class State
{
[Column("IdState")]
public int IdState { get; set; }
}
Now my db schema is correct.