The metadata collection does not include the member with identification. name of parameter: identity

asp.net code-first entity-framework sql-server

Question

EF Code First 4.3.1 is what we're utilizing. We are creating an ASP.NET Web Role that makes use of many class libraries. Each of the two class libraries has classes and its own DBcontext.

Say courses A and B are available at Library 1. DbSet and DbSet in DBcon1

Say courses C and D are available at Library 2. Get; set; Class C [Key] public int CId;

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

If I attempt to utilize the DBcon2 in this manner:

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

I'm given a pass because: "The metadata collection does not include the member with identification. Name of the parameter: identity"

The same SQL Server Database "SampleDB" is used by both DBCon1 and DBcon2.

Please advise where I should go.

1
4
3/31/2012 10:12:54 AM

Popular Answer

I encountered this problem and was able to resolve it by just setting the foreign key id rather than attempting to change the navigation property in the linked table.

eg

public class Student()
{
    public int StudentId { get; set; }
    public string StudentName { get; set; }
    public int CourseId { get; set; }
    public virtual Course Course { get; set; }
}

public class Course()
{
    public int CourseId { get; set; }
    public string CourseName { get; set; }
    public virtual ICollection<Student> Students { get; set; }
}

primary code

var myCourse = new Course();
var myCourseId = 1;

var student = new Student() {
    CourseId = myCourseId
    // Course = myCourse <-- this would cause the error
}

It may not be a problem for you, but perhaps it will assist someone else and put you in the right path.

4
12/31/2013 3:02:28 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