LINQ Entity Properties

by volkanuzun 12/18/2008 4:18:00 PM
Let's assume you create an entity class, that represents a structure in your database, for example:

</div><div>public class Person </div><div>{</div><div>public int PersonID{get;set;}</div><div>public string FirstName{get;set;}</div><div>public string LastName{get;set;}</div><div>...</div><div>}</div><div>

Now after you create this class, you have to tell the compiler that this class represents a table in your database.
Let our table name be PersonsTB. Below is the attribute you have to use:

</div><div>[Table(Name="PersonsTB")]//this tells linq that the table for this entity is PersonsTB</div><div>public class Person</div><div>{</div><div>...</div><div>}</div><div>

If my class name was exactly the same with the actual table name, then i didnt have to specity the name, i could simply write [Table].
However i used a different class name, so i have to specify the table name as an attribute of the class. What else? All the entity objects
must have a primary key (know why? if you dont have a unique primary key in your entity class, they are readonly, 
you cant modify them, cause datacontext cant keep track of them), lets define our primary key name:

</div><div>[Table(Name="PersonsTB")]</div><div>public class Person</div><div>{</div><div>[Column(IsPrimaryKey=true)] public int PersonID{get;set;}</div><div>....</div><div>}</div><div>

Now our entity class has a table name and a primary key, let's define the other 2 columns. FirstName has the same column name in our table so 
i didnt specify Column Name attribute, but, LastName represents the column SurName in the table, and i did specify a name attribute

</div><div>[Table(Name="PersonsTB")]</div><div>public class Person </div><div>{</div><div>[Column(IsPrimaryKey=true)] public int PersonID{get;set;}</div><div>[Column] public string FirstName{get;set;}</div><div>[Column Name="SurName"] public string LastName{get;set;}</div><div>...</div><div>}</div><div>

Next step is, opps stop, we have a primary key, and usually the primary keys are auto incremented, database generated columns, which means
whenever i insert a value into the table, this primary key will be assigned a value. I want that value to be copied to my primary variable
in the class automatically after the insertion. How? simple just add this attribue:

</div><div>[Table(Name="PersonsTB")]</div><div>public class Person </div><div>{</div><div>[Column(IsPrimaryKey=true), IsDBGenerated=true] public int PersonID{get;set;}</div><div>...</div><div>}</div><div>

How about if i assign a value to my entity class's properties and submit the change to the database, however the database has a trigger
or some other way of changing this value before it is submitted!! I also want my entity class properties be synced. Let's assume there is trigger on 
the firstname column, and i want this column to be updated in my entity class after my insertion.

</div><div>[Table(Name="PersonsTB")]</div><div>public class Person </div><div>{</div><div>[Column(IsPrimaryKey=true), IsDBGenerated=true] public int PersonID{get;set;}</div><div>[Column(AutoSync=AutoSync.Insert)] public string FirstName{get;set;}</div><div>...</div><div>}</div><div>

Confused ? dont be...

Tags:

Add comment


(Will show your Gravatar icon)  

  Country flag

biuquote
  • Comment
  • Preview
Loading



About the author

Volkan Uzun




E-mail me Send mail

Twitter

Calendar

<<  July 2009  >>
MoTuWeThFrSaSu
293012345
6789101112
13141516171819
20212223242526
272829303112
3456789

View posts in large calendar

Flickr Badge

www.flickr.com
This is a Flickr badge showing public photos from volkanuzun. Make your own badge here.

Disclaimer

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

© Copyright 2009

Sign in