我在我的數據庫中有一個名爲SEntries的表(見下面的CREATE TABLE語句)。它有一個主鍵,一對外鍵,沒有什麼特別的。我的數據庫中有許多表與此類似,但由於某種原因,此表以EF代理類中的「Discriminator」列爲結尾。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不能從任何東西繼承(如你上面看到的)。
除此之外,一旦我得到調試器上的工具提示,當我將鼠標放在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]作爲該類的一個屬性! –