I am extending this DataContext entity, which looks sort'a like this:
namespace Entities
{
public class User
{
public Int32 Id { get; set; }
public String Username { get; set; }
}
}
.. Like so:
public class User : Entities.User
{
new public Int32 Id
{
get { return base.Id; }
}
public void Insert()
{
using (var dc = new DataContext())
{
/*
The "this" keyword should match the type that InsertOnSubmit() expects.
And it does. But I get the following error:
System.NullReferenceException: {"Object reference not set to an instance
of an object."}
*/
dc.Users.InsertOnSubmit(this); // Exception occurs here
dc.SubmitChanges();
}
}
}
I am using the custom User class like so:
var u = new User { Username = "Test" };
u.Insert();
What I don't get is this: I have instantiated the class, so why am I getting a NullReferenceException?
Update:
Extending entity class: overriding a property with an enumerator while still being able to use the "this" keyword on the Insert
/Update
and DeleteOnSubmit
methods on a DataContext instance
enum AccessLevels
{
Basic,
Administrator
}
namespace Entities
{
public class User
{
public Int32 Id { get; set; }
public String Username { get; set; }
public Int32 AccessLevel { get; set; }
}
}
How would I extend or alter the above entity class and implement the AcessLevels
enumerator, replacing the AccessLevel
property?--this without altering the signature of the entity class, so I'm able to use the "this" keyword on Insert
/Update
and DeleteOnSubmit
methods on a DataContexts.