为什么 Entity Framework 创建一个不映射到任何字段且始终为空的额外列?
Why is Entity Framework creating an extra column that doesn't map to any field and is always null?
我的 POCO 设置如下:
public class Notification : BaseClass
{
public User Receiver { get; set; }
[Required, Index]
public long ReceiverID { get; set; }
public virtual User ContextualUser { get; set; }
public long? ContextualUserID { get; set; }
public virtual User Actor { get; set; }
public long? ActorID { get; set; }
public string Content { get; set; }
public string Uri { get; set; }
[Required]
public string Type { get; set; }
}
我的基地class只有三个属性:ID
、CreateDate
和IsDeleted
、none 与 User
class:
有关
public abstract class BaseClass
{
[Key]
public long ID { get; set; }
[Required]
public DateTime CreateDate { get; set; }
public bool IsDeleted { get; set; }
public BaseClass()
{
CreateDate = DateTime.UtcNow;
}
}
但是,Entity Framework 创建了一个额外的列 User_ID
,它 始终为 null,如下所示:
它显然没有映射到任何东西。 None 我的其他实体(无论是否源自相同的基础 class)都有这个 phantom 列。为什么要创建这个专栏?我在 EF 6.1.3 代码优先上使用 SQL Azure。
更新: 我也在 PM 控制台中 运行 update-database
但没有任何改变,我仍然有这个专栏。
更新 2: 甚至在列上定义了一个索引和一个外键。我试过删除它(它还不是一个实时应用程序)但它被索引引用:
alter table notifications drop column User_ID
Msg 5074, Level 16, State 1, Line 1
The index 'IX_User_ID' is dependent on column 'User_ID'.
Msg 5074, Level 16, State 1, Line 1
The object 'FK_dbo.Notifications_dbo.Users_User_ID' is dependent on column 'User_ID'.
Msg 4922, Level 16, State 9, Line 1
ALTER TABLE DROP COLUMN User_ID failed because one or more objects access this column.
我的 User
class 上的导航 属性 配置不正确。因为 Notification
class 没有名为 User
的 属性 而是 Receiver
,并且因为我忘记添加正确的名称(接收者)作为我的 User
class 上的逆向 属性 有一个 Notifications
collection,这个额外的字段是为了从 从 [=24= 导航而生成的] User
至 Notification
秒。我已经在 User
class 上向我的 collection 添加了注释 [InverseProperty("Receiver")]
,它现在可以正常工作了。
您可以为此使用数据注释:
public abstract class BaseClass
{
[Key]
public long ID { get; set; }
[Required]
public DateTime CreateDate { get; set; }
public bool IsDeleted { get; set; }
public BaseClass()
{
CreateDate = DateTime.UtcNow;
}
}
public class Notification : BaseClass
{
[ForeignKey("ReceiverID")]
[InverseProperty("Receivers")]
public User Receiver { get; set; }
[Required, Index]
public long ReceiverID { get; set; }
[ForeignKey("ContextualUserID")]
[InverseProperty("ContextualUsers")]
public virtual User ContextualUser { get; set; }
public long? ContextualUserID { get; set; }
[ForeignKey("ActorID")]
[InverseProperty("Actors")]
public virtual User Actor { get; set; }
public long? ActorID { get; set; }
public string Content { get; set; }
public string Uri { get; set; }
[Required]
public string Type { get; set; }
}
public class User
{
[InverseProperty("Receiver")]
public ICollection<Notification> Receivers { get; set; }
[InverseProperty("ContextualUser")]
public ICollection<Notification> ContextualUsers { get; set; }
[InverseProperty("Actor")]
public ICollection<Notification> Actors { get; set; }
}
了解更多信息:
我的 POCO 设置如下:
public class Notification : BaseClass
{
public User Receiver { get; set; }
[Required, Index]
public long ReceiverID { get; set; }
public virtual User ContextualUser { get; set; }
public long? ContextualUserID { get; set; }
public virtual User Actor { get; set; }
public long? ActorID { get; set; }
public string Content { get; set; }
public string Uri { get; set; }
[Required]
public string Type { get; set; }
}
我的基地class只有三个属性:ID
、CreateDate
和IsDeleted
、none 与 User
class:
public abstract class BaseClass
{
[Key]
public long ID { get; set; }
[Required]
public DateTime CreateDate { get; set; }
public bool IsDeleted { get; set; }
public BaseClass()
{
CreateDate = DateTime.UtcNow;
}
}
但是,Entity Framework 创建了一个额外的列 User_ID
,它 始终为 null,如下所示:
它显然没有映射到任何东西。 None 我的其他实体(无论是否源自相同的基础 class)都有这个 phantom 列。为什么要创建这个专栏?我在 EF 6.1.3 代码优先上使用 SQL Azure。
更新: 我也在 PM 控制台中 运行 update-database
但没有任何改变,我仍然有这个专栏。
更新 2: 甚至在列上定义了一个索引和一个外键。我试过删除它(它还不是一个实时应用程序)但它被索引引用:
alter table notifications drop column User_ID
Msg 5074, Level 16, State 1, Line 1
The index 'IX_User_ID' is dependent on column 'User_ID'.
Msg 5074, Level 16, State 1, Line 1
The object 'FK_dbo.Notifications_dbo.Users_User_ID' is dependent on column 'User_ID'.
Msg 4922, Level 16, State 9, Line 1
ALTER TABLE DROP COLUMN User_ID failed because one or more objects access this column.
我的 User
class 上的导航 属性 配置不正确。因为 Notification
class 没有名为 User
的 属性 而是 Receiver
,并且因为我忘记添加正确的名称(接收者)作为我的 User
class 上的逆向 属性 有一个 Notifications
collection,这个额外的字段是为了从 从 [=24= 导航而生成的] User
至 Notification
秒。我已经在 User
class 上向我的 collection 添加了注释 [InverseProperty("Receiver")]
,它现在可以正常工作了。
您可以为此使用数据注释:
public abstract class BaseClass
{
[Key]
public long ID { get; set; }
[Required]
public DateTime CreateDate { get; set; }
public bool IsDeleted { get; set; }
public BaseClass()
{
CreateDate = DateTime.UtcNow;
}
}
public class Notification : BaseClass
{
[ForeignKey("ReceiverID")]
[InverseProperty("Receivers")]
public User Receiver { get; set; }
[Required, Index]
public long ReceiverID { get; set; }
[ForeignKey("ContextualUserID")]
[InverseProperty("ContextualUsers")]
public virtual User ContextualUser { get; set; }
public long? ContextualUserID { get; set; }
[ForeignKey("ActorID")]
[InverseProperty("Actors")]
public virtual User Actor { get; set; }
public long? ActorID { get; set; }
public string Content { get; set; }
public string Uri { get; set; }
[Required]
public string Type { get; set; }
}
public class User
{
[InverseProperty("Receiver")]
public ICollection<Notification> Receivers { get; set; }
[InverseProperty("ContextualUser")]
public ICollection<Notification> ContextualUsers { get; set; }
[InverseProperty("Actor")]
public ICollection<Notification> Actors { get; set; }
}
了解更多信息: