пʼятниця, 6 березня 2009 р.

Troubleshoot with LINQ to SQL

Troubleshoot with LINQ to SQL

Today I want to talk about use innovation in our project. Although LINQ to SQL already have not present in category new technology, but many people use it in their current project and don`t know enough about it.

When I try to develop my web-based project, I decide to use LINQ as base technology for access to MS SQL 2005 database. My solution based on .NET 3.5 Framework. As main programming language I use С#. Before I started I read many literatures about LINQ, about her advantages and disadvantages, but I when you try apply something for your requirement – you understand, that not all is so good as it seems at first.

Picture 1 – Schema of project.

As you can see and first picture, my schema consists of three tables, two of them represent storage for save info about main entity in my system: Company and Category.

Third table just contains pair of key Category to Company with relations - many to many.

I want to create new Company so I`m writing:

//initialize company instance

Company comp = Company.NewCompany();

comp.Name = txtName.Text;

comp.Phone = txtPhone.Text;

comp.Site = txtSite.Text;

comp.Slogan = txtSlogan.Text;

comp.ID_Member = Setting.UserID;

After this I Category and Company_Category and initialize it.

Category cat = new Category();

cat.ID_Category = new Guid("b1f363c6-8343-48d2-afde-22b61f9efb92");

Company_Category cc = new Company_Category();

cc.ID_Company = comp.ID_Company;

cc.ID_Category = cat.ID_Category;

I specially have missed all validation for reduction unde

rstanding code. If we are looking under debug to state of object comp and property comp.Company_Categories, we can see the follow picture

Picture 2 – State of variable before add new element.

As we see the property Company_Category of comp object has not any items. After adding object cc into the collection we can see this:

Picture 3 – State of variable after add new element.

Where are three null items was add from ? It`s a good question, because I don`t do this.

Немає коментарів:

Дописати коментар