mardi 31 mai 2016

What to do if DbSet

I am pretty new with entity framework, i have db first approach and i am setting the values of my model into the DbSet MyClassName. But when i am running it is giving exception that "myContext.MyClassName is null Additional information: "Object reference not set to an instance of an object.", i tried many solution but unable to create object of my DbSet.

**1st Tried**

MyClass myObj = new MyClass();
myObj.Id = stud.Id;
myObj.StudId = stud.StudId;
myObj.CourseId = stud.CourseId;

myContext.MyClass.Add(myObj) //Here exception starts with messages.


**2nd Tried**
MyClass langTest = new MyClass();
langTest.Id = 1;
langTest.StudentId = 500;
foreach(MyClass entity in myContext.MyClass)// Here getting exception because MyClass instance is null.
{
   var entry = _context.Entry(langTest);
   if (entry.State == EntityState.Detached)
   {
      myContext.MyClass.Add(entity);
   }
}

If any solution or suggestion please put comment, highly appreciated.

Aucun commentaire:

Enregistrer un commentaire