EF5 Code First identity column error

dbcontext entity-framework identity

Question

I have the following class hierarchy:

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; }
}

OnModelCreating method of context is as follows:

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 don't make use of automatic migrations or table generation. As an alternative, I have a variety of scripts that I can use to generate databases (in actuality, I used autogeneration for the first run then scripts to create tables after that included webdata_* tables). There are several concerns with table autogeneration in this situation as a result of the use of different contexts.

the following 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

NOTE 0: I don't have a table like EdmMetadata or anything comparable (it was not created upon start, so I have not script for it). Although I am not an expert in EF, I believe that the Code First approach does not need any information (are I correct?).

NOTE1: Naturally, the context required the creation of all other classes and tables.

NOTE2: Maybe I made a mistake when populating the data with the IDENTITY field? Here, every string that touches IDENTITY is present.

I receive an issue when I try to save a group "The metadata collection does not contain the member with identification "id." Name of the parameter: identity" How do you solve it?

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

Accepted Answer

I located it! It was easy to solve: Never combine the metadata declaration methods for attributes and fluent apis. I just eliminated

  [Key]
  [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]

modified every model declaration in OnModelChanging starting from the Id declaration, such as:

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