WAQS: Implement your own identity logic

7 reasons to use WAQS

WAQS documentation


Imagine that you don’t have identity key column in your DB and you use your own way to define your DB sequence.

So you need an identity logic on the client but on the server you have to add your own code to define your way to define the identity property on entity insertion.


The way to do it with WAQS is to only set StoreGeneratedPattern in the CSDL but not in the SSDL.

With this way, WAQS generated code calls a method that is not defined so you have to do it in partial class to specify your own way to get the value.


Now the bad point is the fact that people didn’t undestand why after setting the StoreGeneratedPattern it does not have any impact on EF logic. In fact, the reason was the fact that only csdl (entities description) was impacted and ssdl (DB description) was not.

So from VS2012, the edmx designer now impacts both which is a good thing, except in our case because any time we save our edmx, it copies StoreGeneratedPattern value from csdl to ssdl.

So the way to do it with WAQS is to use our changes.xml file as I explained in my last post.

<?xml version="1.0" encoding="utf-8"?>
<edmx:Edmx xmlns:edmx="http://schemas.microsoft.com/ado/2009/11/edmx">
      <Schema xmlns="http://schemas.microsoft.com/ado/2009/11/edm" xmlns:annotation="http://schemas.microsoft.com/ado/2009/02/edm/annotation">
        <EntityType Name="Order">
          <Property Name="Id" annotation:StoreGeneratedPattern="Identity" />

With this way, we can have different StoreGeneratedPattern attribute value in csdl and in ssdl.


Then, WAQS generates this code:

public void ApplyChanges(ConsoleApplication25.Order entity)
    ApplyingChanges(ref entity);
    if (entity.ChangeTracker.State == ObjectState.Added)
    _context.ApplyChanges(_context.Orders, entity);
}<?xml:namespace prefix = "o" />

As I explained above, the SetOrderId method is not defined in generated code so you have to implement it yourself.


Note that it is exactly the same with StoreGeneratedPattern Computed.

This entry was posted in 16868, 7674, 7675. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *