【发布时间】:2011-09-27 02:11:16
【问题描述】:
我的数据库中有一个名为 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
【问题讨论】:
-
对于下一个会花一些时间试图弄清楚这一点的人来说,发生的事情是在代码的另一个地方,我有一个继承自 SEntry 的类,即使它不是一个类那将永远存储在数据库中。所以我需要做的就是添加 [NotMapped] 作为该类的属性!
-
如果我没有将 [NotMapped] 放在 Identitymodel.cs 中的 ApplicationUser 类上,我会收到此错误
标签: entity-framework ef-code-first