2

Here's my DbContext. As you can see, the tables in database are named differently.

public class DataContext : DbContext {
    public DataContext() : base("name=connString") { }

    public DbSet<Person> Personer;
}

[Table("Users")]
public class Person {
    [Column("UserID")]
    public int ID { get; set; }
    [Column("UserFirstName")]
    public string Fornavn { get; set; }
    [Column("UserLastName")]
    public string Etternavn { get; set; }
}

Here's the code that generates System.ArgumentNullException: Value cannot be null. Parameter name: source.

using (var db = new DataContext()) {
    var x = db.Personer.Count();
}
1
  • Where does Personer get initialized with a value? The exception is telling you that it's null. Commented Jan 27, 2016 at 15:11

1 Answer 1

1

Personer must be a property not a field.

public class DataContext : DbContext {
    public DataContext() : base("name=connString") { }

    public DbSet<Person> Personer { get; set; }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.