Custom Entity Classes Using LINQ to SQL Part 2 - Data

by Ira 22. February 2008 19:16
Multi-Part Series
Custom Entity Classes Using LINQ to SQL Part 1 - DataObjects
Custom Entity Classes Using LINQ to SQL Part 2 - Data
Custom Entity Classes Using LINQ to SQL Part 3 - Queries

In my last post, Part 1 I showed the overview of how to set up your own entity classes or "DataObjects". This time, we are going to set up our inheritence mapping on the Animal class to inform LINQ that we have the child classes Penguin and Monkey. We already put the "Table" attribute on the Animal class and have gone through and set the "Column" attribute on all the public properties that will be persisted back to our database throughout all three classes.

 

The next thing to do, is inform LINQ that the Animal class has child classes. In order to do this, first we will add a discriminator property. We can call it "AnimalType". This will be what LINQ checks to see what type of DataObject it is handling. This will have the "Column" attribute, but it will also hold an argument of "IsDiscriminator".

Now we can tell LINQ how to handle the inheritance by putting "InheritanceMapping" attributes on the base class, in this case our "Animal" class.

The "IsDefault" argument of the "InheritanceMapping" attribute tells LINQ that by default, everything is an Animal unless specified otherwise.


Ok, now we have it all set up for LINQ to understand how to handle our classes and the inheritance within them. This is where we can take all the public properties in all the classes that have the "Column" attribute and make them into a single table in our database. We can create a table called "Animals" and then put all of our class columns in it. It is important to remember that any insert that LINQ does to our database will only fill the columns that are in the class definition, therefore only items in the base class could not allow null values.



Keep in mind, our "AnimalID" column has special arguments in the "Column" attribute because it is an identity column. See more

 

Now is the time to build our DataContext. This is a custom class we will create that will inherit the "DataContext" class located in the System.Data.Linq namespace. All we will have to do is give it a constructor accepting a connection string that we will in turn pass to the base constructor, and then put in public fields that will represent our database tables.


There we go! The "MyDataContext" class has a public field of type "Table<>". The "Table<>" type accepts a strongly typed class that has the "Table" attribute for LINQ visibility. Now all we have to do is build an instance of our custom DataContext and then we can begin to use LINQ to insert, update, delete, and query our data. When a row is inserted it will show up in the database with an "AnimalType" value of the hard-coded values of the "Code" argument in our "InheritenceMapping" attribute. So if you create a new instance of the Penguin class and you insert it, the AnimalType in that row of the database will be "Penguin" and an Animal will be an AnimalType of "Animal" and so on.


Now just use the "db" variable as you would any other LINQ DataContext. You will find that only db.Animals is exposed because that is the only table we told LINQ we have.

 

Next time I will show you how LINQ handles distinguishing your DataObjects in query scenarios.

kick it on DotNetKicks.com

Tags: , , , , ,

ASP.NET | ASP.NET | C# | LINQ | C# | LINQ

Comments are closed

Powered by BlogEngine.NET 1.5.0.7
Theme by Mads Kristensen

About Me

IraIra
I'm just another developer from Florida.

 

Sponsors

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

All code samples on this website are free for download, use, and modification with no warranty nor any implied warranty. Liscensed by:

Creative Commons License
This work is licensed under a Creative Commons Attribution 3.0 United States License.