How to change the type of the persisted class in a legacy model using the Entityframework

Asked

Viewed 145 times

4

I have the following class scheme with inheritance, as an example:

public class Veiculo
{
    public int Id { set; get; }
    public string Descricao { set; get; }
}

public class Moto : Veiculo { }

public class Carro : Veiculo { }

That generates the database with the table Veiculo and the columns Id, Descricao and Discriminator.

I have a record that your field Discriminator is with the value Moto. I mean, when I registered this record I did it using a Moto.

How do I transfer this record of type Moto for Carro?

  • Delete the bike and register as the data as car!!!

  • 4

    So your question is very pertinent, excellent question inclusive, but, only if you do a manual editing, or do a routine and leave in the system for change, but, practical thing does not have, only if I’m wrong but, never seen

  • 4

    Another factor is that this is a correct inheritance so Moto is different from Car, even the two inheriting by vehicle. So the fields don’t beat, but, sincerity I’ve never seen such doubt I’ll even research

1 answer

2


I briefly resolved in a "manual".

var sql = " Update Veiculo Set Discriminator = @Tipo Where Id = @Id ";

myDbContext.Database.ExecuteSqlCommand(sql,
    new SqlParameter("Tipo", "Carro"),
    new SqlParameter("Id", moto.Id));
  • 4

    Just one more tip, besides changing the discriminator also check which fields are required so you don’t have future problems. But, the way is this very congratulations

Browser other questions tagged

You are not signed in. Login or sign up in order to post.