entity framework - EF Code First "Invalid column name 'Discriminator'" but no inheritance -


i have table in database called sentries (see below create table statement). has primary key, couple of foreign keys , nothing special it. have many tables in database similar one, reason, table ended "discriminator" column on ef proxy class.

this how class 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 try add new row table, error:

system.data.sqlclient.sqlexception: invalid column name 'discriminator'. 

this problem occurs if inheriting c# class class, sentry not inheriting (as can see above).

in addition that, once tool-tip on debugger when mouse on emdata instance sentries property, displays:

base {system.data.entity.infrastructure.dbquery<em.sentry>} = {select  [extent1].[discriminator] [discriminator],  [extent1].[sentryid] [sentryid],  [extent1].[originatorid] [originatorid],  [extent1].[dateposted] [dateposted],  [extent1].[message] [message],  [extent1].[dataentrysource] [datae... 

any suggestions or ideas bottom of issue? tried renaming table, primary key , few other things, 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]  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]  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 entity framework assume class inherits poco class mapped table on database requires discriminator column, if derived class not saved db.

the solution quite simple , need add [notmapped] attribute of derived class.

example:

class person {     public string name { get; set; } }  [notmapped] class personviewmodel : person {     public bool updateprofile { get; set; } } 

now, if map person class person table on database, "discriminator" column not created because derived class has [notmapped].

as additional tip, can use [notmapped] properties don't want map field on db.


Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -