EF5 Code First identity column error

dbcontext entity-framework identity

Question

I have class hierarchy like following:

class BaseType
{
  [Key]
  [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
  public int Id { get; set; }

  [DataType(DataType.Text)]
  [StringLength(100)]
  public string CreatedBy { get; set; }

  [DataType(DataType.DateTime)]
  public DateTime? CreatedDate { get; set; }
....
}

class Group : BaseType
{
  public string Name { get; set; }
}

Here is OnModelCreating method of context:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<MCQGroup>()
        .HasKey(t0 => t0.Id)
        .Map(m => m.ToTable("groups"))
        .HasMany(t1 => t1.Sections)
        .WithMany(t2 => t2.Groups)
        .Map(m =>
          m.MapLeftKey("group_id")
          .MapRightKey("section_id")
          .ToTable("groups_to_sections"));
  ...........
  }

I am not using automatic table generations or migrations. Instead I have number of scripts to create database (actually, I used autogeneration for the first run and after that script created tables include webdata_* tables). This caused by multiple contexts used and in this case there are number of issues with table autogeneration.

Here is SQL scripts:

-- Tables creation

CREATE TABLE [groups]
(
    [id]           INT NOT NULL IDENTITY(1, 1),
    [name]         NVARCHAR(300) NOT NULL,
    [createdby]    NVARCHAR(4000) NULL,
    [createddate]  DATETIME NULL,
    [modifiedby]   NVARCHAR(4000) NULL,
    [modifieddate] DATETIME NULL
);

GO
......
-- Intitial data population

SET IDENTITY_INSERT [user_profiles] ON;
GO
INSERT INTO [user_profiles] ...........

SET IDENTITY_INSERT [user_profiles] OFF;
GO

INSERT INTO [webpages_Membership] .............
GO

SET IDENTITY_INSERT [webpages_Roles] ON;
GO

INSERT INTO [webpages_Roles] ..............
GO
SET IDENTITY_INSERT [webpages_Roles] OFF;
GO

INSERT INTO [webpages_UsersInRoles] ...........
GO

NOTE0: I have not any table like EdmMetadata or something like that (it was not created upon start, so I have not script for it). I am not guru of EF and I assume that Code First approach does not required any metadata (am I right?).

NOTE1: Of course all other classes/tables required by context created =)

NOTE2: May be I did something wrong with IDENTITY during data population? All string touchs the IDENTITY are here.

Problem: When I trying to save Group I get an exception "The member with identity 'id' does not exist in the metadata collection. Parameter name: identity" How to fix it?

1
3
12/25/2012 2:52:29 PM

Accepted Answer

I found it! Solution was simple: never mix attribute-way metadata declaration and fluent-api-way. I just removed

  [Key]
  [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]

from Id declaration and changed all model declarations in OnModelChanging like:

modelBuilder.Entity<MCQGroup>()
    .HasKey(t0 => t0.Id)
    .Map(m => m.ToTable("groups"))
    .Property(x => x.Id)
    .HasColumnName("id")
    .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity)
    .IsRequired();
modelBuilder.Entity<MCQGroup>()
    .HasMany(t1 => t1.Sections)
    .WithMany(t2 => t2.Groups)
    .Map(m => m.MapLeftKey("group_id").MapRightKey("section_id").ToTable("groups_to_sections"));
5
12/26/2012 6:05:42 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow