2

I'm using entity framework to access an exsisting database. I can access the data but when I try to add new data I get a NullReferenceException "Object reference not set to an instance of an object", but it is?

The DB connection is fine, I can access the data just fine: List<log> logs = db.log.ToList();

The exception is thrown when using Add or Create:

db.log.Add(new log());
db.log.Create();

StackTrace:

at System.Data.Entity.DbSet`1.Create()

UPDATE:

The Error only occurs OUTSIDE the namespace containing the DB Context. I can work around it by wrapping the classes to have their 'Add to DB context'-methods in the DBHandler namespace. But I would like an explanation to why this happens. Is it a bug or am I violating some sacred .net law?

Thanks for your time!

user1638662
  • 443
  • 1
  • 4
  • 7

1 Answers1

0

Try this :

using (db = new xxxxxEntities())
        {
           List<log> logs = db.log.ToList();
           db.log.Add(new log());
           db.log.Create();
        }
Đức Bùi
  • 517
  • 1
  • 6
  • 22