Entity Classes

1. What do we need entity classes?

2. What is the Column attribute?

3. Why do we need to set the IsPrimaryKey property?

4. When do we need to set the Storage property?

5. When do we need to set the DBType property?

6. When do we need to set the IsDbGenerated property?

             

Entity classes need to:

            1. To provide metadata to LINQ queries.

            2. To provide storage for data.

            3. To track changes on the database

Example 1:

 

    [Database(Name = "AdventureWorks")]

    public class AdventureDBContext : DataContext

    {

        public AdventureDBContext(string connString) : base(connString) { }       

    }

 

    [Table(Name="Person.Contact")]

    public class Contact

    {

        private string _firstName = String.Empty;

 

        [Column(Name="ContactID", IsPrimaryKey= true, IsDbGenerated=true)]

        public int ContactId

        {

            set; get;

        }

 

        [Column(Name = "FirstName")]

        public string FirstName

        {

            set; get;

        }

 

        [Column]

        public string LastName

        {

            set;

            get;

        }

 

        [Column]

        public string PasswordHash

        {

            set;

            get;

        }

 

        [Column]

        public string PasswordSalt

        {

            set;

            get;

        }

 

    }

 

Example 2:

 

        [Column(Name="ContactID", IsPrimaryKey= true, IsDbGenerated=true)]

        public int ContactId

        {

            set; get;

        }

 

Only properties and fields decorated with the Column attribute are significant to mapping entity classes to a data source.

If the column belongs is the Primary key is better to set  IsPrimaryKey property to the true, otherwise in case of updates the compiler will generate error: "Can’t perform Create, Update, or Delete operations on ‘Table’ because it has no primary key."

If the column name is not the same like property name we can set column attribute property Name according to data source name.

Example 3:

 

         [Column(Name = "FirstName", Storage = "_firstName")]

        public string FirstName

        {

            set

            {

                if (OnPropertyChanged != null)

                    OnPropertyChanging("FirstName");

 

                _firstName = value;

 

                if (OnPropertyChanged != null)

                    OnPropertyChanged("FirstName");

            }

            get

            {

                return _firstName;

            }

        }

 

If we want to call events only when we update data manually, we can set the Storage property thus the Linq2Sql will update only the field of the property.

Example 4:

 

        [Column(DbType = "NVARCHAR(50)")]

        public string LastName

        {

            set;

            get;

        }

 

If we want to set corresponded DB type for .Net type we can use DbType attribute. We need this when we want to create a DB scheme based on a class definition.

Example 5:

 

        [Column(Name="ContactID", IsPrimaryKey= true, IsDbGenerated=true)]

        public int ContactId

        {

            set; get;

        }

 

If the data source has automatically generated columns we have to set IsDbGenerated to the true to retrieve generated values otherwise we get an exception like this “Cannot insert explicit value for identity column in table ‘ColumnName’ when IDENTITY_INSERT is set to OFF”.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s