Author Archive

Team Work with CodeFluent Entities

Working on your own and working along a whole developers team are obviously not the same. As a team, you will experience issues that you would never encounter working alone.

In order to help you work in a harmless way with CodeFluent Entities in that context, we wrote a new paper about every little things you can configure while you are:

  • Designing your model collaboratively
  • Generating your own database
  • Handling work station custom connection strings
  • Using source control

You can get this paper here.

Happy Configurating,

The R&D Team.

CodeFluent Entities vs Entity Framework: Referential Integrity

April 16, 2015 Leave a comment

Referential Integrity is about guarantee that database constraints are not violated. Databases has different concepts to ensure referential integrity such as foreign keys.

Let’s see what CodeFluent Entities and Entity Framework provides to ensure data and referential integrity!

Entity Framework

Entity Framework supports the following concepts:

  • Primary keys

  • Foreign keys (association)

  • On Delete Rule

CodeFluent Entities

CodeFluent Entities supports :

  • Primary Keys

  • Foreign Keys

You can enforce or not foreign key constraints in the database:

Cascade Save and Cascade Delete can be configured on the save screen and when needed you can set custom rules:

We also support unique constraints:

Note that you can also have compound unique constraints.


Entity Framework and CodeFluent Entities provide mechanisms about referential integrity. Foreign key support is limited with Entity Framework and unique constraint only exists with Code-First

CodeFluent Entities supports unique constraints and it is more flexible thanks to delete and update rules. It allows to set data integrity enforcement.


CodeFluent Entities vs Entity Framework: Cascade save

April 15, 2015 Leave a comment

To avoid handling every relations in your data model, you may want to ask your access layer to synchronize update operations across related entities.

Entity Framework

With Entity Framework you will not have any feature available to automate a cascade save treatment. This Framework only offers you the way to save entities in your entire context. This is not really a cascading operation as it can be with the SQL Server “cascade delete” but it can do the tricks.

CodeFluent Entities

With CodeFluent Entities you can add a relation:

Then, you can configure the « save » cascading operations:


Cascade Save


Unlike the Cascade Delete feature which modify the generated stored procedure, it will only change the C# code:

public partial class Client : // ... 

        public virtual bool Save()
            if ((this._orders != null))
            bool localSave = this.BaseSave(false);
            return localSave;

        // ...

Simple and clean!


Entity Framework doesn’t include a cascade save feature.

CodeFluent Entities asks you for every relation  to configure an optional operation mode for your save related operations.


Table-Valued Parameters: Basics

March 24, 2015 Leave a comment

You can use table-valued parameters (TVP) to send multiple rows of data to a Transact-SQL statement or a routine, such as a stored procedure or function, without creating a temporary table or many parameters.

Great news, you can use TVP with CodeFluent Entities! Let’s see how you can use TVP to bulk load rows based on a list of id.

Create a new CFQL method:

Note the usage of “[]” after the type name. The generated method is:

public static CustomerCollection LoadByIds(System.Guid[] ids)

And the generated stored procedure:

CREATE TYPE [dbo].[cf_type_Customer_LoadByIds_0] AS TABLE (
 [Item] [uniqueidentifier] NULL)
CREATE PROCEDURE [dbo].[Customer_LoadByIds]
 @ids [dbo].[cf_type_Customer_LoadByIds_0] READONLY
DECLARE @_c_ids int; SELECT @_c_ids= COUNT(*) FROM @ids
SELECT DISTINCT [Customer].[Customer_Id], [Customer].[Customer_FirstName], [Customer].[Customer_LastName], [Customer].[Customer_DateOfBirth]
    FROM [Customer]
    WHERE [Customer].[Customer_Id] IN (((SELECT * FROM @ids)))


Table-Valued Parameters require SQL Server 2008. Don’t forget to change the target of the SQL Server producer to use at least this version.

Additionally set Legacy String Array Mode to false :

Happy Coding,

The R&D Team.


CodeFluent Entities vs Entity Framework : Cascade Delete

February 25, 2015 Leave a comment

Ensuring referential integrity is something very important in RDBMS. When you delete a customer you may want to delete all related information, this what is called Cascade Delete.

Let’s see how CodeFluent Entities and Entity Framework (with Model-First) handle this feature.

Entity Framework

First of all you have to add a navigation property:

Then you can select the association (the line between entities) and configure it:

The generated SQL contains:

-- --------------------------------------------------
-- Creating all FOREIGN KEY constraints
-- --------------------------------------------------

-- Creating foreign key on [CustomerId] in table 'OrderSet'
ALTER TABLE [dbo].[OrderSet]
    FOREIGN KEY ([CustomerId])
    REFERENCES [dbo].[CustomerSet]

Entity Framework Model First does not allow cascade delete on many to many association:

CodeFluent Entities

As with EF, we also need to create a relation:

All parameters are available on the same screen. Instead of EF, it’s all English so everyone can understand options.

When using this option, the cascade delete is done by the generated stored procedure looks like:

DELETE [Order] FROM [Order]
    INNER JOIN [Customer] ON ([Order].[Order_Customer_Id] = [Customer].[Customer_Id])
    WHERE ([Customer].[Customer_Id] = @Customer_Id)
DELETE FROM [Customer]
    WHERE ([Customer].[Customer_Id] = @Customer_Id)

Maybe you prefer to use the SQL instruction “ON DELETE CASCADE”. This option is also available with CodeFluent Entities:

Using this setting the generated constraint now uses ON DELETE CASCADE:

) REFERENCES [dbo].[Customer](


Both solutions support cascade delete functionality. CodeFluent Entities has two ways to support this feature and is understandable by non-technical people.

CodeFluent Entities vs Entity Framework: Computed property

February 16, 2015 Leave a comment

Computed properties are properties which are computed from other properties. For instance the full name is the concatenation of the first name and the last name. This value can be computed by the database or by the .NET code.

Let’s see how CodeFluent Entities and Entity Framework handle those properties.

Entity Framework


There is no magic, you have to add a partial class with a property FullName:

partial class Customer
    public string FullName
        get { return string.Format("{0} {1}", this.FirstName, this.LastName); }

This property is not part of the model so you cannot use it in T4 templates.


Querying the database should be easy with EF, but in fact there are some subtleties. Here’s the Linq to Entities query:

var customers = from customer in context.Customer
                select new
                    Id = customer.Id,
                    FullName = customer.FirstName + " " + customer.LastName

If you look in details the result is not a list of Customer but a list of an anonymous type. In fact EF does not allows to instantiate a custom Entity object:

You can also define SQL function in the model, but you have to edit the Edmx file with an xml editor (without designer).

CodeFluent Entities


There are many possibilities:

1. As with Entity Framework we can add a new property in a partial class
2. We can also add a computed property directly to the model and code the OnGetFullName method in a partial class. This way we can benefit from the property when generating code from the model

private void OnGetFullName()
    this._fullName = string.Format("{0} {1}", this.FirstName, this.LastName);

3. When the computed property is a “string.Format” you can specify the format in the model so the code of the OnGetFullName method is automatically generated:


The idea is to create a view which compute the full name.

Finally, you can load data from the SQL view using one line of code:



Both solutions allow to use computed properties. CodeFluent Entities provides some shorthand’s so you don’t always have to write code.


CodeFluent Entities vs Entity Framework: Naming convention

February 12, 2015 Leave a comment

This is the first of many in a series of post covering the difference between Entity Framework 6 and CodeFluent Entities. Today we’re talking about naming convention.

Naming convention allows to transform the name of a concept in the model (entity, property, constraints, etc.) for generating database. For instance if you have an entity “Product” in your model, the generated table name may be “Product”, or “PRODUCT”, “prod” or maybe something else according to you naming convention. So naming convention is about standardization and also about making your DBA happy with the generating codeJ.

Entity Framework

Entity Framework Model-First doesn’t provide a simple way to apply a naming convention for the model. Instead you have to manually define names:

To automate the process you have to change default T4 templates located in Visual Studio installation folder, and the default generation workflow (TablePerTypeStrategy.xaml). Be sure to keep in sync the SQL template and the mapping (MSL) template.

Figure 1- Extract from the default T4 file

Then you can change model settings:

Entity Framework Code First has the notion of convention (does not work with Model First). This notion allows to set name of tables or properties:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
     modelBuilder.Types().Configure(type => type.ToTable("EF_" + type.ClrType.Name));
     modelBuilder.Properties().Configure(prop => prop.HasColumnName("EF_" + prop.ClrPropertyInfo.Name));

Setting constraint names (foreign key names, index names) looks more complicated and requires more than one line of code.

CodeFluent Entities

CodeFluent Entities provides out of the box six naming conventions, and allows you to create your own naming convention.


To implement a custom naming convention you have to create a class that implements INamingConvention or inherits from BaseNamingConvention:

public class MyCustomConvention: BaseNamingConvention
 public override string GetName(INamedObject obj, IDictionary context)
  Column column = obj as Column;
  if (column != null)
   string name = (string)context["name"];
   Table table = (Table)context["table"];
   if (table != null)
     column.DefaultName = "Default_" + table.Name + "_" + name;
  return base.GetName(obj, context);

 public override string GetShortName(IShortNamedObject obj, IDictionary context)
  Constraint constraint = obj as Constraint;
  if (constraint == null)
   return base.GetShortName(obj, context);

  return "myx_" + constraint.ConstraintType + "_" + constraint.Table.Name + "_" + constraint.ReferencedTable.Name;


Entity Framework Model-First does not provide a simple way to define a naming convention. The Code-First approach is better for table name and column name, but it’s harder to name indices or foreign keys.

CodeFluent Entities provides out of the box six naming conventions, and let you write your own.

References :