Rename the table in Sql and, accordingly, in the Entity Framework (Db First)

The database has tables x and y in the database, with X being a higher level group and Y being a lower level group. Unfortunately, X objects are called "Y" in the software user interface, and Y objects are called "X". I need to rename x to y and y to x in code. What steps should I follow. I even want to change the column names if necessary, I mean, if table1 has table1ID, now it should be Table2ID, because the name of Table1 will be table2. So what do I need to do to make it successful?

PS: I know, maybe I can get downvoted, but I don’t think it is really as simple as it looks, at least for me.

+4
source share
1 answer

If you want to rename them only to code and leave db the same, just change the names and names of entity objects:

What you describe sounds as if your classes and properties are called differently from your tables:

[Table("X", Schema = "MYSCHEMA")]
public class Y
{
    [Column("X_ID"), Key]
    public int Y_ID { get; set; }

    public virtual List<X> X { get; set; }
}

[Table("Y", Schema = "MYSCHEMA")]
public class X
{
    [Column("Y_ID"), Key]
    public int X_ID { get; set; }

    [Column("X_ID"), Key]
    public int Y_ID { get; set; }

    [ForeignKey("X_ID")]
    public virtual Y Y { get; set; }
}

You can simply rename your classes and properties according to the names of tables and columns as follows:

[Table("X", Schema = "MYSCHEMA")]
public class X
{
    [Column("X_ID"), Key]
    public int X_ID { get; set; }

    public virtual List<X> Y { get; set; }
}

[Table("Y", Schema = "MYSCHEMA")]
public class Y
{
    [Column("Y_ID"), Key]
    public int Y_ID { get; set; }

    [Column("X_ID"), Key]
    public int X_ID { get; set; }

    [ForeignKey("X_ID")]
    public virtual X X { get; set; }
}
0
source

All Articles