我正在尝试清理一些最初创建时没有任何命名约定的Entity Framework数据上下文。我想保留旧的类,但同时要提供正确命名的新类,这样就不需要立即更改所有其他使用这些数据上下文的项目。我将使用数据注释将旧类标记为“过时”,以便它们不会在任何新开发中使用。仅更改类,数据库将因长期的不良命名而陷入困境。
当我基于一个表创建一个新类时,它会清理所有命名,同时仍然保留代表该表的旧类,我可以创建这些类,但是当我尝试使用其中任何一个时,都会出现以下错误:
实体类型“客户”和“ lu_Clients”不能共享表“ lu_Clients”,因为它们不在同一类型层次结构中,或者没有有效的一对一外键关系且它们之间具有匹配的主键。
我的丑陋课堂:
[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; }
}
我的清理班:
[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; }
}
关于如何使两个类进入同一表概念的任何建议?还是有更好的方法来保持与旧版本和新版本的兼容性?
答案 0 :(得分:2)
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.
答案 1 :(得分:1)
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?