2011-07-01 86 views
120

我在我的數據庫中有一個名爲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 
+11

對於下一個將花費一些時間來解決這個問題的人,發生了什麼事是在代碼的另一個地方,我有一個繼承自SEntry的類,即使它不是一個類將永遠存儲在數據庫中。所以我需要做的就是添加[NotMapped]作爲該類的一個屬性! –

回答

256

原來,實體框架將假設從被映射到數據庫表中的POCO類繼承任何類需要一個鑑別列,即使派生類將不會保存到數據庫中。

該解決方案非常簡單,您只需將[NotMapped]作爲派生類的屬性添加即可。

例子:

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

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

現在,即使你映射了Person類Person表的數據庫中,「鑑別」一欄將不會創建,因爲派生類[NotMapped]

作爲附加提示,您可以使用[NotMapped]指定您不想映射到數據庫字段的屬性。

+5

沒問題,所以我的生活經歷了3個小時;(但tyvm都是一樣的,我也應該加上一點就是明確的......派生類可以在角落裏一路走下去,而不是以任何方式使用re:persistence和EF仍然會嘗試並將它們繪製在...非常混亂 – rism

+11

如果您沒有找到[NotMapped],請在「Assembly Framework」中爲項目添加對「System.ComponentModel.DataAnnotations」的引用。 – XandrUu

+8

using System。 ComponentModel.DataAnnotations.Schema; – ygaradon

40

以下是Fluent API語法。

http://blogs.msdn.com/b/adonet/archive/2010/12/06/ef-feature-ctp5-fluent-api-samples.aspx

class Person 
{ 
    public string FirstName { get; set; } 
    public string LastName { get; set; } 
    public string FullName { 
     get { 
      return this.FirstName + " " + this.LastName; 
     } 
    } 
} 

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


protected override void OnModelCreating(DbModelBuilder modelBuilder) 
{ 
    // ignore a type that is not mapped to a database table 
    modelBuilder.Ignore<PersonViewModel>(); 

    // ignore a property that is not mapped to a database column 
    modelBuilder.Entity<Person>() 
     .Ignore(p => p.FullName); 

} 
+1

感謝您指出如何忽略某個屬性 – Adi

+0

僅僅添加'[NotMapped]'屬性會更好嗎? – Keith

+1

@Keith我的答案是如何忽略使用Fluent API的列,它不使用[NotMapped] –

6

我只是遇到了這個和我的問題是由兼具與System.ComponentModel.DataAnnotations.Schema.TableAttribute指的是同一臺兩個實體造成的。

例如:

[Table("foo")] 
public class foo 
{ 
    // some stuff here 
} 

[Table("foo")] 
public class fooExtended 
{ 
    // more stuff here 
} 

改變從foo到​​第二個固定這對我來說,現在我使用的表每個類型(TPT)

+0

這對我不起作用:'實體類型'AtencionMedica'和'AtencionMedicaAP'不能共享表'AtencionMedicas',因爲它們不在同一類型層次結構中' –

+0

謝謝,幫助我,使用流暢的API有同樣的問題: 'var entity = modelBuilder.Entity ()。ToTable(「ENTITY_TABLE」)',然後使用相同的'EntityObject'或者同一個'ENTITY_TABLE'的另一行。 –

1

我得到的錯誤在另一種情況下,和這裏的問題和解決方案:

我有一個名爲LevledItem相同的基類派生的2班:

public partial class Team : LeveledItem 
{ 
    //Everything is ok here! 
} 
public partial class Story : LeveledItem 
{ 
    //Everything is ok here! 
} 

但在他們的DbContext,我複製了一些代碼,但忘記改變類名之一:

public class MFCTeamDbContext : DbContext 
{ 
    protected override void OnModelCreating(DbModelBuilder modelBuilder) 
    { 
     //Other codes here 
     modelBuilder.Entity<LeveledItem>() 
      .Map<Team>(m => m.Requires("Type").HasValue(ItemType.Team)); 
    } 

public class ProductBacklogDbContext : DbContext 
{ 
    protected override void OnModelCreating(DbModelBuilder modelBuilder) 
    { 
     //Other codes here 
     modelBuilder.Entity<LeveledItem>() 
      .Map<Team>(m => m.Requires("Type").HasValue(ItemType.Story)); 
    } 

是,第二個地圖<團隊>應該是地圖<故事>。 它花了我半天的時間來搞清楚!

2

這種情況出現是另一種情況,當你有一個基礎類和一個或多個子類,其中的子類中的至少一個引入額外的屬性:如果這些在DbContext像下面映射

class Folder { 
    [key] 
    public string Id { get; set; } 

    public string Name { get; set; } 
} 

// Adds no props, but comes from a different view in the db to Folder: 
class SomeKindOfFolder: Folder { 
} 

// Adds some props, but comes from a different view in the db to Folder: 
class AnotherKindOfFolder: Folder { 
    public string FolderAttributes { get; set; } 
} 

,當基於Folder基本類型的任何類型的訪問「「無效列名‘鑑’」錯誤:

protected override void OnModelCreating(DbModelBuilder modelBuilder) 
{ 
    modelBuilder.Entity<Folder>().ToTable("All_Folders"); 
    modelBuilder.Entity<SomeKindOfFolder>().ToTable("Some_Kind_Of_Folders"); 
    modelBuilder.Entity<AnotherKindOfFolder>().ToTable("Another_Kind_Of_Folders"); 
} 

我發現,要解決這個問題,我們提取的Folder道具基類(未在OnModelCreating()映射),像這樣 - OnModelCreating應該是不變的:

class FolderBase { 
    [key] 
    public string Id { get; set; } 

    public string Name { get; set; } 
} 

class Folder: FolderBase { 
} 

class SomeKindOfFolder: FolderBase { 
} 

class AnotherKindOfFolder: FolderBase { 
    public string FolderAttributes { get; set; } 
} 

這消除了這個問題,但我不知道爲什麼!

+0

這幾乎呈現派生類無用,雖然... – Jerther

1

與我這個錯誤發生,因爲我做了以下

  1. 我在數據庫
  2. 改變表的列名(我沒有用在EDMX Update Model from database)我改名手動屬性名相匹配的變化數據庫模式
  3. 我做了一些重構以改變類的屬性的名稱是相同EDMX數據庫架構和模型

雖然人這個L,我得到這個錯誤

所以what to do

  1. 我刪除了模型從EDMX
  2. 右鍵單擊並Update Model from database

這將重新生成模型,以及實體框架will不是give you this error

h這對你有幫助