c# - Extending DataContext entities - using InsertOnSubmit(this) inside "child" class -
I am expanding this DataContext unit, which looks sortaa like this:
< Code> Namespace units {public class user {public IT32 ID (received; set;} public string username {get} set;}}} .. is similar:
Public class users: Institutions. Users {New public IT 32 ID ({return base.Id;}} Public Zero Insert () {(var dc = new DataContext ()) {/ * "This" should match the keyword that looks like InsertOnSubmit () and it does. But I get the following error: System.NullReferenceException: {"Object Reference is an example of an object "} * / Dc.Users.InsertOnSubmit (this); // The exception is here dc.SubmitChanges ();}}} I like this custom user class I am using:
var u = new user {user name = "test"}; u.Insert (); I'm not I get it: I have begun the class, so why am I getting a NullReferenceException?
Update:
Extended entity classes: while still being able to override an asset with a calculator "This" on Insert / update and DeleteOnSubmit on a data contact example
Use keywords. enum AccessLevels {Basic, Administrator} Nomination Entities {Public Class User {Public Int32 ID {get; Set; } Public String Username {get; Set; } Public Int32 AccessLevel {get; Set; }}}} How do I extend or change the above mentioned unit class and by implementing AccessLevel property, implement the AcessLevels calculator ? Without changing the entity class signature, I'm able to use the keyword "this" on insert / updates and DeleteOnSubmit . Methods on a DataContexts
enum editing again (added as second answer to keep things simple).
First of all - is there a direct mapping between 1: 1 enum and values? For example, if the original is 7 and the administrator is 12 then:
enum AccessLevels {basic = 7, admin = 12} then change the type In DBML (through the Designer), enter your (fully qualified) em: Entities.AccessLevel LINQ-to-SQL directly from the property as integer mapping or straight mapping of string Supports enums in form.
If this is not possible (more complex scanners), then you can separate the storage (object) and object-oriented (ANUM) model; Rename the property to AccessLevelStorage (or whatever you like), and mapping in partial orbit:
partial class user {public AccessLevel AccessLevel { Get {switch (AccessLevelStorage) {Case 1: Return AccessLevelStorage.Foo; ... etc Default: ... throw an exception? }} Set {switch (value) {case AccessLevel.Foo: AccessLevelStorage = 1; break; ... etc Default: ... throw an exception? There is only one warning here that LINQ queries will only work against storage properties - custom-mapped assets if you do not have your questions in that level You can change the use of storage property on internal - but if you ask questions outside this assembly then you have to leave the public. To stop appearing [browsable (false)] , but that's about it.
Comments
Post a Comment