EF Code First“无效的列名称”鉴别符“”但没有继承 [英] EF Code First "Invalid column name 'Discriminator'" but no inheritance

查看:167
本文介绍了EF Code First“无效的列名称”鉴别符“”但没有继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的数据库中有一个名为SEntries的表(见下面的CREATE TABLE语句)。它有一个主键,一对外键,没有什么特别的。我的数据库中有很多表与我相似,但是由于某种原因,该表最后在EF Proxy Class上有一个Discriminator列。



如何在C#中声明类:

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

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

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

当我尝试添加一个新行对于该表,我收到错误:

  System.Data.SqlClient.SqlException:无效的列名称鉴别符。 

仅当您从另一个类继承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 = $ {$ {



ALTER TABLE [dbo]。[SEntries ] WITH CHECK ADD CONSTRAINT [FK_SEntries_ObjectTypes] FOREIGN KEY([OriginatorObjectTypeId])
参考文献[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


解决方案

事实证明,实体框架将假设从映射到数据库表上的POCO类继承的任何类都需要一个Discriminator列,即使派生类不会被保存到数据库中。



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



示例:

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

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

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



作为额外的提示,您可以使用 [NotMapped] 将您不想映射到的属性DB上的字段。


I have a table in my database called SEntries (see below the CREATE TABLE statement). It has a primary key, a couple of foreign keys and nothing special about it. I have many tables in my database similar to that one, but for some reason, this table ended up with a "Discriminator" column on the EF Proxy Class.

This is how the class is declared in 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; }
            ...
    }

When I try to add a new row to that table, I get the error:

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

This problem only occurs if you are inheriting your C# class from another class, but SEntry is not inheriting from anything (as you can see above).

In addition to that, once I get the tool-tip on the debugger when I mouse over the EMData instance for the SEntries property, it displays:

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...

Any suggestions or ideas where to get to the bottom of this issue? I tried renaming the table, the primary key and a few other things, but nothing works.

SQL-Table:

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

解决方案

Turns out that Entity Framework will assume that any class that inherits from a POCO class that is mapped to a table on the database requires a Discriminator column, even if the derived class will not be saved to the DB.

The solution is quite simple and you just need to add [NotMapped] as an attribute of the derived class.

Example:

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

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

Now, even if you map the Person class to the Person table on the database, a "Discriminator" column will not be created because the derived class has [NotMapped].

As an additional tip, you can use [NotMapped] to properties you don't want to map to a field on the DB.

这篇关于EF Code First“无效的列名称”鉴别符“”但没有继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆