Inserting…

Using the "previous” example the code to insert records would be:

Linq to SQL

LTSModelDataContext lts = new LTSModelDataContext();
Person person = new Person()
{
FirstName = "Juan",
LastName = "Perez",
MiddleName = "LTS", //Linq to SQL
};
lts.Persons.InsertOnSubmit(person);
lts.SubmitChanges();

Linq to Entities

LTEEntities lte = new LTEEntities();
Persons person = new Persons()
{
FirstName = "Juan",
LastName = "Perez",
MiddleName = "LTE", //Linq to Entities
};
lte.AddToPersons(person);
lte.SaveChanges();

Nothing to complicated.

If we want to insert contact information for a person, based on our model, we need to insert a record on the ContactInfos table, and then link the ContactInfo key with the Person key on the PersonContactInfos table.

Link to Entities is actually very simple, since the Model created from the DB only has two classes, we just need to create a ContactInfos object use the AddToContactInfos() method on our entity model, add it to the list of our Person object and save changes.

The code looks like this:

LTEEntities lte = new LTEEntities();
Persons person = new Persons()
{
FirstName = "Juan",
LastName = "Perez",
MiddleName = "LTE", //Linq to Entities
};
ContactInfos contactInfo = new ContactInfos()
{
ContactInfo = "15 Yemen Road, Yemen",
ContactType = 0 // Assuming 0 is Address
};
person.ContactInfos.Add(contactInfo);
lte.AddToPersons(person);
lte.SaveChanges();

Link to SQL needs an extra step, linking ContactInfo to the Person is done “manually”, the code looks like this:

LTSModelDataContext lts = new LTSModelDataContext();
Person person = new Person()
{
FirstName = "Juan",
LastName = "Perez",
MiddleName = "LTS", //Linq to SQL
};
ContactInfo contactInfo = new ContactInfo()
{
ContactInfo1 = "15 Yemen Road, Yemen",
ContactType = 0 // Assuming 0 is Address
};
PersonContactInfo personContactInfo = new PersonContactInfo()
{
ContactInfo = contactInfo,
Person = person
};
lts.Persons.InsertOnSubmit(person);
lts.SubmitChanges();

Please note that since we are using many to many, it shouldn’t matter if we add the person to the contactInfo or the other way around.

Also, is not necessary to add all the objects to their add methods (or SubmitOnInsert methods) the model “figures” what needs to be added and gets added.

Also, remember this is only inserting new objects, edit and delete will come soon.

Many to many relationship

Given this diagram:

Database diagram

Linq to SQL

Linq to SQL

Models are generated in singular. A table with Persons, gets represented as a class “Person”.

The Many to Many table PersonContactInfo, gets also generated as a Class. Also, the ContactInfo class, has a ContactInfo1 property, (apparently a class and a property cant have the same name?).

Accessing the list from code looks something like this:

LTSModelDataContext lts = new LTSModelDataContext();
List<Person> listPersons = lts.Persons.ToList();

Accessing the contact information for a person would look like this:

Person p = new Person();
var a = p.PersonContactInfos[0].ContactInfo.ContactInfo1;

Linq to Entities:

image

Models names are generated as they are in the DB, so table Persons, gets created as a class Persons.

The model doesn’t create the PersonContactInfo table it adds a Navigation Property (a more OOP approach, rather than a Relational approach).

Accessing the list from code looks something like this:

LTEEntities lte = new LTEEntities();
List <Persons> listPersons = lte.Persons.ToList();

Accessing the contact information for a person would look like this:

Persons p = new Persons();
var a = p.ContactInfos.First().ContactInfo;

Conclusions

Linq to SQL made a more comprehensive set of models from the DB, realizing the table names are plural and that a Class is singular is very neat. But the addition of the PersonContactInfo table to the model and its use to access the contact information for a person seems too much.

Linq to Entities, created a “perfect” model from the DB (prefect OOP Model), and accessing the contact information for a Person is more intuitive, but unfortunately the generation of names is not as perfect as Linq to SQL.

Next, inserting, updating and deleting…

First Post (since last stand-up)

Last week, as we were starting a new project, instead of selecting

ADO.NET Entity Data Model

, which is what we usually do, we selected:

Linq to SQL Classes

and that’s how this blog started…

I will try to show as much as possible, the differences between these two models in a series of posts, which will, hopefully, help you to make a decision.

I’ll also use this space to post other tech related stuff, and maybe rant about the geeks…