Reputation: 8331
I am trying to clean up some Entity Framework data contexts that were originally created without any naming conventions. I would like to keep the old classes but also have the new, properly named class available at the same time so that all the other projects that use these data contexts don't need to be changed right away. I will be marking the old class as Obsolete using data annotations so that they won't be used on any new development. Only the classes will be changed, the database is going to be stuck with the poor naming for eternity.
When I create a new class based on a table that cleans up all the naming while still keeping the old class representing the table I can create the classes but when I try to use either one I get he following error:
The entity types 'Client' and 'lu_Clients' cannot share table 'lu_Clients' because they are not in the same type hierarchy or do not have a valid one to one foreign key relationship with matching primary keys between them.
My ugly class:
[Obsolete("Use Client class instead")]
public class lu_Clients
{
[Key]
[Column("ClientsSeq")]
public int ClientsSeq { get; set; }
[Column("ClientsID")]
public string ClientsID { get; set; }
[Column("ClientsName")]
public string ClientsName { get; set; }
}
My cleaned up class:
[Table("lu_Clients")]
public class Client
{
[Key]
[Column("ClientsSeq")]
public int Sequence { get; set; }
[Column("ClientsID")]
public string Id { get; set; }
[Column("ClientsName")]
public string Name { get; set; }
}
Any suggestions on how to get the two classes to the same table concept to work? Or is there a better way to maintain compatibility with the obsolete and the new going forward?
Upvotes: 1
Views: 606
Reputation: 2282
Could you make the new Class inherit from the table and then change lu_clients to inherit directly from clients class instead, effectively mapping via the new class until the old ugly class is no longer needed?
Upvotes: 1
Reputation: 11891
Why do you need to keep both? I would rename each field in the old class to match the new class (use VS rename feature to make sure all valid fields are renamed), then rename the old class name to new class name (which will generate conflicts), but then delete the old class. Now all code is using the new class and the old class doesn't exist.
Upvotes: 2