entity-framework - EF Code First "Invalid column name ' 鉴别器''但没有继承

标签 entity-framework ef-code-first

我的数据库中有一个名为 SEntries 的表(请参见下面的 CREATE TABLE 语句)。它有一个主键、几个外键,没有什么特别的。我的数据库中有许多与该表类似的表,但由于某种原因,该表最终在 EF 代理类上有一个“鉴别器”列。

这是在 C# 中声明类的方式:

public class SEntry
{
    public long SEntryId { get; set; }

    public long OriginatorId { get; set; }
    public DateTime DatePosted { get; set; }
    public string Message { get; set; }
    public byte DataEntrySource { get; set; }
    public string SourceLink { get; set; }
    public int SourceAppId { get; set; }
    public int? LocationId { get; set; }
    public long? ActivityId { get; set; }
    public short OriginatorObjectTypeId { get; set; }
}

public class EMData : DbContext
{
    public DbSet<SEntry> SEntries { get; set; }
            ...
    }

当我尝试向该表添加新行时,出现错误:

System.Data.SqlClient.SqlException: Invalid column name 'Discriminator'.

仅当您从另一个类继承 C# 类,但 SEntry 不继承任何内容时,才会出现此问题(如上所示)。

除此之外,当我将鼠标悬停在 SEntries 属性的 EMData 实例上时,当我在调试器上看到工具提示时,它会显示:

base {System.Data.Entity.Infrastructure.DbQuery<EM.SEntry>} = {SELECT 
[Extent1].[Discriminator] AS [Discriminator], 
[Extent1].[SEntryId] AS [SEntryId], 
[Extent1].[OriginatorId] AS [OriginatorId], 
[Extent1].[DatePosted] AS [DatePosted], 
[Extent1].[Message] AS [Message], 
[Extent1].[DataEntrySource] AS [DataE...

有什么建议或想法可以深入了解这个问题吗?我尝试重命名表、主键和其他一些东西,但没有任何效果。

SQL 表:

CREATE TABLE [dbo].[SEntries](
[SEntryId] [bigint] IDENTITY(1125899906842624,1) NOT NULL,
[OriginatorId] [bigint] NOT NULL,
[DatePosted] [datetime] NOT NULL,
[Message] [nvarchar](500) NOT NULL,
[DataEntrySource] [tinyint] NOT NULL,
[SourceLink] [nvarchar](100) NULL,
[SourceAppId] [int] NOT NULL,
[LocationId] [int] NULL,
[ActivityId] [bigint] NULL,
[OriginatorObjectTypeId] [smallint] NOT NULL,
CONSTRAINT [PK_SEntries] PRIMARY KEY CLUSTERED 
(
[SEntryId] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF,       ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

ALTER TABLE [dbo].[SEntries]  WITH CHECK ADD  CONSTRAINT [FK_SEntries_ObjectTypes] FOREIGN KEY([OriginatorObjectTypeId])
REFERENCES [dbo].[ObjectTypes] ([ObjectTypeId])
GO

ALTER TABLE [dbo].[SEntries] CHECK CONSTRAINT [FK_SEntries_ObjectTypes]
GO

ALTER TABLE [dbo].[SEntries]  WITH CHECK ADD  CONSTRAINT [FK_SEntries_SourceApps] FOREIGN KEY([SourceAppId])
REFERENCES [dbo].[SourceApps] ([SourceAppId])
GO

ALTER TABLE [dbo].[SEntries] CHECK CONSTRAINT [FK_SEntries_SourceApps]
GO

最佳答案

事实证明, Entity Framework 将假定任何继承自映射到数据库表的 POCO 类的类都需要鉴别器列,即使派生类不会保存到数据库中也是如此。

解决方案非常简单,您只需添加 [NotMapped] 作为派生类的属性即可。

示例:

class Person
{
    public string Name { get; set; }
}

[NotMapped]
class PersonViewModel : Person
{
    public bool UpdateProfile { get; set; }
}

现在,即使将 Person 类映射到数据库上的 Person 表,也不会创建“Discriminator”列,因为派生类具有 [NotMapped]

作为附加提示,您可以使用 [NotMapped] 来映射您不想映射到数据库上的字段的属性。

关于entity-framework - EF Code First "Invalid column name ' 鉴别器''但没有继承,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6553935/

相关文章:

mysql - CurrentUtcDateTime 不存在 - Entity Framework 和 MySql

c# - EF 4 : The specified type member '*' is not supported in LINQ to Entities

c# - EF4 代码优先 - 疯狂嵌套的 UNION ALL 和重复的连接?

entity-framework - 删除 EF 代码第一个外键表列中的表名前缀

c# - 允许为标有 PhoneAttribute 或 UrlAttribute 的字段使用空字符串

c# - 在 Linq 中选择子集

entity-framework - 为什么 DbContext 实现了 IObjectContextAdapter 但没有公共(public) ObjectContext 属性

entity-framework - EF : How do I call SaveChanges twice inside a transaction?

c# - Entity Framework 代码第一个一对多级联错误

asp.net-mvc - 如何查询 Entity Framework Code First 数据的集合