locked
The member with identity does not exist in the metadata collection. Parameter name: identity RRS feed

  • Question

  • We are using EF Code First 4.3.1. We are developing an ASP.NET Web Role referring to multiple class libraries. There are two class libraries each containing classes and an individual DBcontext.

    Lets say the Library1 has classes A and B. DBcon1: DbSet and DbSet

    Lets say the Library2 has classes C and D. Class C{ [Key] public int CId{ get; set;}

    [Required]
    public virtual A referencedA {get; set;}
    }
    DBcon2
    : DbSet<C> and DbSet<D>

    When I try to use the DBcon2 as such:

            using (var con = new DBcon2())
           
    {
                C vr
    = new C();
                vr
    .CId= 1;
                vr
    .referencedA = DBCon1.As.First();
                con
    .Cs.Add(vr);
                con
    .SaveChanges();
           
    }

    I get an exception as: "The member with identity does not exist in the metadata collection. Parameter name: identity"

    Both DBCon1 and DBcon2 are using the sane SQL Server Database "SampleDB".

    Please point me in the right direction.

    StackOverflow link: http://stackoverflow.com/questions/9954997/the-member-with-identity-does-not-exist-in-the-metadata-collection-parameter-na


    Manish Kungwani Developer

    Monday, April 2, 2012 8:43 AM

All replies

  • Hi Manish,

    Welcome!

    According to your description, The two dbcontext have the same connection string. My suggestion is merging all Dbsets in one DbContext.

    BTW, you should also check if there is a identity property in you class: http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/770e9db4-b28b-4332-bd62-cb6d4634425b/ 

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, April 3, 2012 7:03 AM
  • Hi Alan,

    Yes there is an identity property in the class, but I am not sure how it is affecting the other context.

    Also, I wish to keep the contexts separate, since DBCon1 should be independent of DBCon2 and they are in different class libraries as well!!


    Manish Kungwani Developer

    Tuesday, April 3, 2012 7:27 AM
  • Hi Manish,

    Thanks for your feedback.

    I just tested two libraries without identity properties, it worked on my computer. I think you can try to delete the Identity properties from your class.

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, April 3, 2012 7:48 AM
  • Hi Alan,

    It so happens, that I do need the Identity property in my class A [for clarification, its Employee class].

    And I do need the identity in class C as well.

    Are you asking me to remove the identity from class A?
    Isn't there any workaround to it?


    Manish Kungwani Developer

    Tuesday, April 3, 2012 9:24 AM