Home > Data access, Developing Using CodeFluent Entities, Producers > Using the LINQ to SQL Sub-Producer

Using the LINQ to SQL Sub-Producer


 
CodeFluent Entities gives you the possibility to make your generated Business Object Model (BOM) compatible with the LINQ to SQL data access infrastructure.
 
CodeFluent Entities ships a LINQ to SQL producer, that is in fact a Sub-Producer of the Business Object Model (BOM) Producer.
 
Let’s start by setting a model to work with.
 
For this post I will create a very simple model. A “Person” entity who has zero or more “Pets” of a given kind.
 

Pet management model

Pet management model


 
I will also add Class Library project to my solution so I can store the generated code by CodeFluent Entities.
 
I will add a Persistence producer (SQL Server) and a Business Object Model Producer. I will set the Target Directory to my Class Library project for both producers.
 
Now I can add my Linq-To-Sql producer. As said before, this producer is a Sub-Producer of the BOM Producer, so I have to add it to the BOM Producer.
 
Add Sub-Producer

Add Sub-Producer


 
LINQ to SQL Sub-Producer

LINQ to SQL Sub-Producer


 
As you can see, there is no configuration required for this Sub-Producer.
 
We need to set the “Default Entity Tracking Mode” to “None” at the project level to avoid any problem with the LINQ to SQL query provider.
 
Default Entity Tracking Modes

Default Entity Tracking Modes


 
Let’s build our CodeFluent Entities project, and open a BOM class so we can see what has been generated.
 
LINQ to SQL attributes

LINQ to SQL attributes


 
We now see that our class has been decorated with LINQ to SQL attributes. We also see that the “System.Data.Linq” namespace is not defined. To fix that we need to add a reference to the “System.Data.Linq” assembly in our BOM project.
 
We have now a LINQ to SQL compliant BOM.
 
Let’s test it using LINQ to SQL.
 
For that I will add a simple Console application. Then I add a reference to my BOM project, to the CodeFluent.Runtime assembly and to the System.Data.Linq assembly. And add the right application configuration.
 
We will use the DataContext class provided by the LINQ to SQL infrastructure to manipulate data (insert, delete, update and read). And we can retrieve the connection string from the CodeFluentContext class.
 

string connectionString = CodeFluentContext.Get(PetsManagement.Constants.PetsManagementStoreName).Persistence.ConnectionString;
using (DataContext context = new DataContext(connectionString))
{
}

 
Let’s start by adding some data to our database:
 

string connectionString = CodeFluentContext.Get(PetsManagement.Constants.PetsManagementStoreName).Persistence.ConnectionString;
using (DataContext context = new DataContext(connectionString))
{

    Person bart = new Person { Name = "Bart" };
    Pet bartsDog = new Pet { Name = "Santa's Little Helper", Age = 2, Kind = PetKind.Dog };
    bartsDog.Owner = bart;
    Pet bartsCat = new Pet { Name = "Snowball II", Age = 3, Kind = PetKind.Cat };
    bartsCat.Owner = bart;//not true, is Lisa

    Person pablo = new Person { Name = "Pablo" };
    Pet pablosDog = new Pet { Name = "Shiny", Age = 17, Kind = PetKind.Dog };
    pablosDog.Owner = pablo;
    Pet pablosDog2 = new Pet { Name = "Vanilla", Age = 10, Kind = PetKind.Dog };
    pablosDog2.Owner = pablo;

    Person ferb = new Person { Name = "Ferb" };
    Pet ferbsPet = new Pet { Name = "Perry", Age = 32, Kind = PetKind.Platypus };
    ferbsPet.Owner = ferb;

    context.GetTable<Person>().InsertAllOnSubmit(new[] { bart, pablo, ferb });
    context.GetTable<Pet>().InsertAllOnSubmit(new[] { bartsDog, bartsCat, pablosDog, pablosDog2, ferbsPet });

    context.SubmitChanges();
}

 
Now let’s retrieve all the pets grouped by “Pet kind”.
 

string connectionString = CodeFluentContext.Get(PetsManagement.Constants.PetsManagementStoreName).Persistence.ConnectionString;
using (DataContext context = new DataContext(connectionString))
{
    var petsByKind = from pet in context.GetTable<Pet>()
                        group pet by pet.Kind into gr
                        select gr;

    foreach (IGrouping<PetKind, Pet> kindGroup in petsByKind)
    {
        Console.WriteLine("----" + kindGroup.Key + "----");
        foreach (Pet pet in kindGroup)
        {
            Console.WriteLine(pet.Name + " : " + pet.Owner.Name);
        }
    }
}

 
The result is:

Pets grouped by kind

Pets grouped by kind


 
Let’s now delete a cat named “Snowball II” which owners name is “Bart”.
 

string connectionString = CodeFluentContext.Get(PetsManagement.Constants.PetsManagementStoreName).Persistence.ConnectionString;
using (DataContext context = new DataContext(connectionString))
{
    Pet petToDelete = (
                        from person in context.GetTable<Person>()
                        where
                            person.Name == "Bart"
                        from pet in person.Pets
                        where
                            pet.Kind == PetKind.Cat &&
                            pet.Name == "Snowball II"
                        select pet
                        )
                        .FirstOrDefault();

    if (petToDelete != null)
    {
        context.GetTable<Pet>().DeleteOnSubmit(petToDelete);
        context.SubmitChanges();
    }

}

 
Thanks to the LINQ to SQL Sub-Producer we can use the LINQ to SQL infrastructure with our BOM, but there are several limitations and several concepts/features that can be done by CodeFluent Entities and not supported by LINQ to SQL: The CodeFluent Entities model is richer than LINQ to SQL (rules, validation, aspects, advanced relations, automatic tracking, cache, circular dependencies, multi-databases…).
  

Regards,
Pablo Fernandez Duran

  1. No comments yet.
  1. No trackbacks yet.

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 )

Google+ photo

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

Connecting to %s