Dealing with Enumerations and Entity framework 4 – code first

UPDATE! There’s a June CTP with new support for enums!

If you have an entity with an enumeration and you wan’t to store it using Entity framework 4, Code-only, you might run into problem. The convention-based mapping is sufficent for generating the schema but you will run into problem when trying to create an objectset.

The solution that I will present here is kind of simple: Make a custom-type that you map as a complex-type using ComplexTypeConfiguration, then you implement implicit operator overloading to provide implicit-conversion between this custom-type and the enumeration.

The Entity

public class Tifi : ICamyinEntity
{
    public Guid Id { get; set; }

    public byte[] Version { get; set; }

    public TifiType Type { get; set; }

    public string Title { get; set; }

    public string Description { get; set; }

    public string Source { get; set; }
}

The Enumeration

public enum TifiTypes : int
{
    Undefined = 0,
    Blog = 1,
    Video = 2
}

The Custom-type

public class TifiType
{
    public int Value { get; private set; }

    private TifiType()
        : this(default(TifiTypes))
    {}

    public TifiType(int value)
    {
        Value = value;
    }

    public TifiType(TifiTypes type)
    {
        Value = (int)type;
    }

    public static implicit operator int(TifiType type)
    {
        return type.Value;
    }

    public static implicit operator TifiType(int value)
    {
        return new TifiType(value);
    }

    public static implicit operator TifiTypes(TifiType type)
    {
        return (TifiTypes)type.Value;
    }

    public static implicit operator TifiType(TifiTypes type)
    {
        return new TifiType(type);
    }
}

The Mappings

public class TifiTypeMapping : ComplexTypeConfiguration<TifiType>, ICamyinMapping
{
    public TifiTypeMapping()
    {
        Property(o => o.Value);
    }
}

public class TifiMapping : EntityConfiguration<Tifi>, ICamyinMapping
{
    public TifiMapping()
    {
        HasKey(o => o.Id);
        Property(o => o.Id)
            .StoreGeneratedPattern = StoreGeneratedPattern.Identity;

        Property(o => o.Version)
            .IsConcurrencyToken()
            .IsRequired()
            .HasStoreType("timestamp")
            .StoreGeneratedPattern = StoreGeneratedPattern.Computed;

        Property(o => o.Source)
            .HasMaxLength(200)
            .IsUnicode()
            .IsRequired();

        Property(o => o.Title)
            .HasMaxLength(100)
            .IsUnicode();

        Property(o => o.Description)
            .HasMaxLength(1000)
            .IsUnicode()
            .IsOptional();
    }
}

That’s it. You will now be able to assign enumeration values to an instance of Tifi and it’s member Type.

var tifi = new Tifi
{
    Title = "My first Tifi",
    Description = "This works",
    Source = "http://daniel.wertheim.se",
    Type = TifiTypes.Blog
};

//Daniel

Entity framework 4 – CTP3 – Complex type mapping

I got a question on my last post of how complex type mapping should be dealt with so I fired up Visual Studio and put together something.

What you need to do as a consumer of the lib

Lets say my Customer has a property Address and I want to store it in the Customer-table, or more correctly, “together with the Customer entity” as an embedded object.

Step 1 – Create the Address class

[Serializable]
public class Address
{
    public string Street { get; set; }
    public int? Zip { get; set; }
}

Step 2 – Add it to the Customer

[Serializable]
public class Customer
    : IEntity
{
    public int Id { get; set; }

    public byte[] Version { get; set; }

    public string Firstname { get; set; }

    public string Lastname { get; set; }

    public EfAddress Address { get; set; }

    public string Name
    {
        get { return string.Format("{0} {1}", Firstname, Lastname).Trim(); }
    }

    public Customer()
    {
        Address = new Address();
    }
}

Note! You must set-up an instance of the Address, otherwise you will get an exception, complaining about an null-address.

Step 3 – Map the Complex type

[Serializable]
public class AddressMapping 
    : ComplexTypeConfiguration<Address>
{
    public AddressMapping()
    {
        Property(o => o.Street)
            .HasMaxLength(50)
            .IsUnicode()
            .IsOptional();
        Property(o => o.Zip);
    }
}

Instead of extending EntityConfiguration<T> you extend ComplexTypeConfiguration<T>, then just provide the mappings just as you would do with an entity.

That’s it! You are done. The generated table will now contain fields like: Address_Street; Address_Zip. If you like to control the namings of the fields: use MapSingeType inside of the AddressMapping-class.

The changes to get it to work

To get it to work I had to add one method to the EfObjectContextBuilder, namely: RegisterComplexType as well as tweak RegisterMappings.

/// <summary>
/// Registers a complex-type.
/// </summary>
/// <typeparam name="TComplex">The complext-type being mapped.</typeparam>
/// <param name="mapping">The mapping-configuration.</param>
public void RegisterComplexType<TComplex>(ComplexTypeConfiguration<TComplex> mapping)
{
    Configurations.Add(mapping);
}

The Assembly resolvers (EfMappingsResolverByAssemblyScan, EfMappingsResolverByInterfaceFilterType) will locate the AddressMapping-type and I have updated EfObjectContextBuilder public void RegisterMappings(IEfMappingsResolver mappingsResolver) so that it will handle both EntityConfiguration<T> as well as CompexTypeConfiguration<T>

/// <summary>
/// Registers a pluraized Entity-Set as well as the mappings for an Entity by
/// traversing the Types of <see cref="EntityConfiguration{TEntity}"/> returned
/// by the Resolver.
/// </summary>
/// <param name="mappingsResolver">The mappings resolver.</param>
public void RegisterMappings(IEfMappingsResolver mappingsResolver)
{
    var mappingTypes = mappingsResolver.Resolve().ToList();

    if(mappingTypes.Count < 1)
        throw  new DataException(string.Format(StorageExceptions.EfObjectContextBuilder_RegisterMappings, typeof(IEfMappingsResolver).Name));

    var registerMapping = GetType().GetMethod("RegisterMapping");
    var registerComplexType = GetType().GetMethod("RegisterComplexType");

    foreach (var mappingType in mappingTypes)
    {
        var configurationType = mappingType.BaseType;

        var registrationMethod = (configurationType.Name.StartsWith("ComplexTypeConfiguration"))
                                        ? registerComplexType
                                        : registerMapping;

        var typeBeingConfigured = configurationType.GetGenericArguments()[0];
        var generic = registrationMethod.MakeGenericMethod(typeBeingConfigured);
        generic.Invoke(this, new[] { Activator.CreateInstance(mappingType) });
    }
}

If you are consuming the internals of the ContextBuilder you can use ComplexType<T>() which will return a ComplexTypeConfiguration instance that’s allready hooked up to the ContextBuilder.

The code can be downloaded here. Note!, I have provided the complete solution of my Core-lib. The code you are interested in is located in the project: Pls.Core.Storage under the namespace Ef. If you look at the IntegrationTestsOf-project, you will find EfCustomer and EfAddress etc. The naming is due to the fact that Entity framework can’t handle two classes having the same name within one assembly. Since I have a Linq to SQL implementation in there as well, I had to name them “L2SqlCustomer” and “EfCustomer“.

//Daniel

Entity framework 4 – CTP3 – Code first vs Linq to Sql

I just started doing some architectural changes to my old code in my previous article for Entity framework 4 (EF4) and at the same time updating it to use the new CTP3 add-on as well as writing the same code for Linq to Sql (L2Sql). I didn’t find any incompatibilities when updating from CTP2 to CTP3 but I did find something annoying. The Entity framework team is aware of this bug and will try to resolve this issue in the future.

This article shows you a solution where you DON’T use a designer or wizard for designing your model, it focuses on “Code first” where the entities are designed manually in code, as pure POCOs.

1 – The design

The design of my solution for dealing with storage of entities, has the following main components: Mappings-resolver, Storage-provider and Unit-of-work. A part from these there’s an ObjectContext and an ObjectContextBuilder, which you don’t have to deal with explicitly, since they are consumed by the Storage-provider and the Unit-of-work. The interface definition of a Storage-provider and the Unit-o-work, looks like this:


Interface for Storage-provider & Unit-of-work

1.1 – Mappings-resolver

The Mappings-resolver is responsible for finding the types that are to be handled by the Storage-provider; in other words, it locates your entities which are provided to the ObjectContextBuilder so that it can register mapping-information for each found entity and later on pass this information on to each created ObjectContext. For L2Sql, the mappings are described using attributes in the entity itself and the mappings are contained in an AttributeMappingSource. For EF4, the mappings are described using the fluent-API of the EntityConfiguration<T> class (more examples of this in the code section below).


L2Sql – Mappings-resolver


EF4 – Mappings-resolver

Both L2Sql and EF4 has two different Mapping-resolver implementations. One that scans for types in a specified assembly, and one that scans for types in the assembly that contains the interface that is provided via generics. With the second approach, the types also need to implement this interface, hence it acts as a filter, indicating if a type should be included or not. No matter which mappings-resolver you choose, all types needs to be a non-abstract class.

1.1.1 – L2SqlMappingsResolver

The L2Sql implementation looks for types that are classes that isn’t abstract and that are decorated with the attribute: TableAttribute. Each found type is then registered with the MappingSource’s model.

1.1.2 – EFMappingsResolver

The EF implementation looks for types that are classes that: isn’t abstract; extends EntityConfiguration<T>;where the name ends with Mapping.

1.2 – Storage-provider

The Storage-provider is designed to be long lived whilst the Unit-of-work is designed to be short-lived. The Storage-provider is tied to a certain implementation like: EF4 or L2Sql. It’s main responsibility is to create new instances of Unit-of-works, which then is used to-do CRUD-operations against the data source specified in the connection string passed to the Storage-provider. If you are familiar with NHibernate, you can look at the storage-provider as the Session factory.

1.3 – Unit-of-work

The Unit-of-work is designed to be short lived. It wraps the ObjectContext and provides a common API independent of the actual implementation (EF4 or L2Sql). It implements IDisposable so that you can use it in conjunction with an using-block. You use it to perform inserts, updates and deletes as well as for querying your model. When performing insert, updates and deletes, you have to flush your changes. You can see the Flush-operation as a sort of commit. Note! It’s not using transactions. If you want to use database transactions, you can i.e use the TransactionScope, or extend the Storage-provider to have a member: CreateUnitOfWorkWithTransaction() : ITransactionalUnitOfWork; or something like that.

L2Sql & EF4, Storage-provider and Unit-of-work

1.4 – ObjectContextBuilder & ObjectContext

The ObjectContextBuilder is responsible for holding mappings (added by passing a mappings-resolver to RegisterMappings) and for creating new instances of ObjectContexts, which are injected to the UnitOfWork instances.

1.4.1 – L2SqlObjectContext

As of right now this class just extends the DataContext class. It’s there to bring some consistency in the namings of all the components involved in the L2Sql-implementation. It might get extended in the future, but for now, it’s only implemented to bring consistency, so that L2SqlObjectContextBuilder and L2SqlUnitOfWork can work with L2SqlObjectContext rather than DataContext.

1.4.2 – EfObjectContextBuilder & ObjectContext

The EF4-implementation of ObjectContextBuilder, it extends the allready existing ContextBuilder. Also note that the EF4 ObjectContextBuilder holds a couple of alternative ways to register mappings, but as long as you use the Storage-provider, you don’t have to care about these. But if you want to extend something or use the builder directly, these members are there to let you add mappings.


L2Sql & EF4 – ObjectContextBuilder & ObjectContext

2 – How to consume the lib

That was a very brief and abstract explanation of the parts involved, now lets take a look at how to consume the lib.

2.1 – L2Sql

Step 1 – Define the model & the mappings

The entity can be placed freely in your assembly and doesn’t have to be generated by the DBML-designer. The example we are dealing with a very simple Customer class. Although I can specify mappings with XML in L2Sql, I tend to like the attribute-decoration approach more easygoing and clear, since I don’t have to locate a certain xml-file for managing the mappings.

[Serializable]
[Table(Name = "Customers")]
public class Customer
{
    [Column(Name = "CustomerId", IsPrimaryKey = true, IsDbGenerated = true, CanBeNull = false, AutoSync = AutoSync.OnInsert)]
    public int Id { get; set; }

    [Column(IsVersion = true, IsDbGenerated = true, CanBeNull = false, AutoSync = AutoSync.Always)]
    public byte[] Version { get; set; }

    [Column(CanBeNull = true, DbType = "NVARCHAR(100)")]
    public string Firstname { get; set; }

    [Column(CanBeNull = true, DbType = "NVARCHAR(100)")]
    public string Lastname { get; set; }

    public string Name
    {
        get { return string.Format("{0} {1}", Firstname, Lastname).Trim(); }
    }
}

Nothing spectacular. I have chosen to have an Id-field that is set-up as an identity as well as a Version-field for concurrency detection.

Step 2 – Extract the mappings

The ObjectContextBuilder needs to know where the data source is as well as what mappings to be used, hence we need to provide a connection string as well as a MappingsResolver.

Alternative 1

var mappingsResolver = new L2SqlMappingsResolverByAssemblyScan(assemblyWithEntities);

In the code above, I have chosen to go with a resolver that extracts the mappings from an assembly that contains my Linq-entities and the attribute-mappings (the Customer).

If I would like to I could let all my entities implement a certain interface and use it to extract the entities. The resolver will then look at the assembly containing the interface, and will look for the classes that implements this interface. To use this solution, I just need to use another resolver:

Alternative 2

var mappingsResolver = new L2SqlMappingsResolverByInterfaceFilterType<IEntity>();
Step 3 – Create a Storage-provider

After having defined my mappings and selected a strategy for fetching them (Step 1 & 2), I can now create my long-lived Storage-provider. Just give it a valid connection string for a Ms-Sql database and the mappings-resolver.

var storageProvider = new L2SqlStorageProvider(connectionString, mappingsResolver);

The rest of the Steps are common between the L2Sql and EF4 implementation, so before proceeding with Step 4, lets look at the EF4-implementation of Step 1 – 3.

2.2 – EF4

Step 1 – Define the model & the mappings

With the EF4 solution, the class doesn’t contain the attributes, instead the mappings are configured in a separate class, using the fluent-API of the EntityConfiguration-class.

public interface IEntity
{
    int Id { get; set; }
    byte[] Version { get; set; }
}

[Serializable]
public class Customer
    : IEntity
{
    public int Id { get; set; }

    public byte[] Version { get; set; }

    public string Firstname { get; set; }

    public string Lastname { get; set; }

    public string Name
    {
        get { return string.Format("{0} {1}", Firstname, Lastname).Trim(); }
    }
}

I have chosen to let my entities implement a certain interface so that I can re-use some mappingconfiguration by having a base-class for my mappings (EntityMapping<T>)

[Serializable]
public class EntityMapping<T> : EntityConfiguration<EfCustomer>
    where T : IEntity
{
    protected EntityMapping()
    {
        HasKey(o => o.Id);
        Property(o => o.Id)
            .IsIdentity();
        Property(o => o.Version)
            .IsConcurrencyToken()
            .IsRequired()
            .HasStoreType("timestamp")
            .StoreGeneratedPattern = StoreGeneratedPattern.Computed;   
    }
}

[Serializable]
public class CustomerMapping
    : EntityMapping<EfCustomer>
{
    public CustomerMapping()
    {
        Property(o => o.Firstname)
            .HasMaxLength(100)
            .IsUnicode()
            .IsOptional();
        Property(o => o.Lastname)
            .HasMaxLength(100)
            .IsUnicode()
            .IsOptional();
    }
}
Step 2 – Extract the mappings

When dealing with the EF4-solution the types to be extracted by the MappingsResolver isn’t the entities but instead the Mapping-classes. This is done using one of two alternatives:

Alternative 1

var mappingsResolver = new EfMappingsResolverByAssemblyScan(assemblyWithEntities);

Alternative 2

var mappingsResolver = new EfMappingsResolverByInterfaceFilterType<IEntityMapping>();

In the Alternative 2 the IEntityMapping could be an empty interface definition that is implemented by the mapping-classes.

Step 3 – Create a Storage-provider

After having defined my mappings and selected a strategy for fetching them (Step 1 & 2), I can now create my long-lived Storage-provider. Just give it a valid connection string, a providername (System.Data.SqlClient used by the DbProviderFactory) and the mappings resolver.

var storageProvider = new EfStorageProvider(connectionString, providerName, mappingsResolver);

That’s it. Now we have gone through all the implementation-specific scenarios. The rest of the steps are common between the L2Sql and EF4 implementation.

2.3 – The Unit-of-work

So the steps up until now has been specific depending on the selected solution: L2Sql or EF4. How you consume the Unit-of-work isn’t solution specific, so from now on, there will only be one set off code examples.

Create the data source

When doing integration testing it’s very useful to be able to teardown and set-up a new database.

storageProvider.SetupDataStore();
Consume the Unit-of-work

Remember that the Unit-of-work is supposed to be short-lived. It contains the instance of the underlying DataContext and provides CRUD-operations.

Insert single

var customer = new Customer {Firstname = "Daniel", Lastname = "Wertheim"};

using(var unitOfWork = storageProvider.CreateUnitOfWork())
{
    unitOfWork.Insert(customer);
    unitOfWork.Flush();
}

Insert many

var customers = new List<Customer>
                    {
                        new Customer {Firstname = "C", Lastname = "1"},
                        new Customer {Firstname = "C", Lastname = "2"}
                    };

using(var unitOfWork = storageProvider.CreateUnitOfWork())
{
    unitOfWork.InsertMany(customers);
    unitOfWork.Flush();
}

Update
There’s no real Update method, the way to see it is that either an entity belongs to the context (unit-of-work) or it doesn’t. If it where created in another context then the one you are using for updating it, you have to attach it to the new context. Sort of “letting the context be aware of the existence of the entity“. For this I use AttachAsModified.

var customer = new Customer { Firstname = "Daniel", Lastname = "Wertheim" };
            
using (var unitOfWork = storageProvider.CreateUnitOfWork())
{
    unitOfWork.Insert(customer);
    unitOfWork.Flush();
}

using (var unitOfWork = storageProvider.CreateUnitOfWork())
{
    customer.Firstname = "Scott";

    unitOfWork.AttachAsModified(customer);
    unitOfWork.Flush();
}

Delete
Just as with Update, if I work with entities created/fetched by another context (unit-of-work) I need to attach them to the new context. Although, when deleting, I don’t need to know each individual change of the entity’s properties, hence AttachMany will do.

var customers = new List<Customer>
                    {
                        new Customer {Firstname = "C", Lastname = "1"},
                        new Customer {Firstname = "C", Lastname = "2"}
                    };
            
using(var unitOfWork = storageProvider.CreateUnitOfWork())
{
    unitOfWork.InsertMany(customers);
    unitOfWork.Flush();
}

using(var unitOfWork = storageProvider.CreateUnitOfWork())
{
    unitOfWork.AttachMany(customers);
    unitOfWork.DeleteMany(customers);
    unitOfWork.Flush();
}

Querying
For querying you just make use of the Query method, which returns an IQueryable. Hence you can write linq-expressions or use lambdas to query it.

var customers = unitOfWork.Query<Customer>().Where(c => c.Firstname == "Daniel").ToList()

3 – Wrap up

So this was kind of an simple introduction, but it gives you a starting point to elaborate from. If I will make a follow-up it will be about mappings. But until then, for EF4 you can read about my old writings about mappings (different model and different infrastructure code):

The L2Sql and EF4 implementations can be downloaded here. I have zipped the entire solution. The project you are interested in is “Pls.Core.Storage“.

//Daniel

Entity framework 4 – CTP3 – Code first – Bug

So I finally got some time to start looking at Code first with the Entity framework CTP3 add-on, again. While doing some testing in a test library, where I have two different StorageProviders: One for Linq to Sql and another for Entity framework 4; I found something I call a bug.

I have two classes that shares nothing except that they lie in the same assembly and have the same name. One of them is decorated with mapping attributes for L2Sql and the other has no mapping attributes, but is instead mapped via the EntityConfiguration class, used by EF4.
The Customer classes lies in different namespaces and doesn’t share any specific base-class or interface.

First I register an ObjectSet/EntitySet and then the mappings (EntityConfiguration) for the Ef.Customer with my Contextbuilder. When I then invoke CreateObjectSet() I get promted with the exception:

Schema specified is not valid. Errors:
The mapping of CLR type to EDM type is ambiguous because multiple CLR types match the EDM type ‘Customer’. Previously found CLR type ‘Pls.IntegrationTestsOf.Core.Storage.L2Sql.TestModel.Customer’, newly found CLR type ‘Pls.IntegrationTestsOf.Core.Storage.Ef.TestModel.Customer’.

My opinions: If I specify the Ef.Customer with generics the EF-infrastructure shouldn’t care about the L2Sql.Customer. They aren’t the same. They don’t share anything except lying in the same assembly an having the same name.

Bug or by design? What do you think? Or have I done it wrong?

//Daniel

Entity framework 4 – Code first

I haven’t got the time to update my previous article to the new CTP-addon for supporting Code-first (old name Code-only). My goal is to start covering this topic during this week, so stay tuned.

//Daniel

Updates to – Putting Entity framework 4 to use in a business architecture

This post only reflects some updates to my previous article: Putting Entity framework 4 to use in a business architecture.

I made a “minor” error in my EfDynamicProxyAssemblies that is consumed by my EfDataContractSerializer. The error which I know have corrected arised when I added an example to the client application, where I intended to read back the created wishlist(s) for a certain username. What happened was that the service used the overload of the EfEntityStore’s Query method that lets you specify string’s for memebers in the objectgraph to include in the select. The generated proxie than contained three entities: Wishlist, UserAccount and Wish, but my EfDynamicProxyAssemblies cleared the cached assemblies once the types were extracted. And the first time the dynamic proxie assembly was executed, the only contained type was UserAccount, hence WishList and Wish didn’t get extracted and registrered as a known type.

I have corrected the code and added some information about the custom EfDataContractSerializer attribute in the article.

Download the pdf

The complete code example can be found here.

I have also added some information about “eager loading”. So there is some new code and two new sub chapters.

Enjoy!

//Daniel

Putting Entity framework 4 to use in a business architecture

Updates have been made since version 1 of this article!

“>Read more about it here.

Finally I’m finished. I have been struggling with an intended sum-up of my latest post using Entity framework 4. It ended up with an document of about 40 pages. There is a lot of code in there so the number of pages could be a bit missleading (which of course depends on what you are seeking).

Download the pdf

The complete code example can be found here.

As always….. Have fun with it!

//Daniel

WCF DataContractSerializer problems with dynamic proxies in Entity framework 4

This article was produced for Beta 2 & CTP2

Download the code

I were having trouble with serialization of my entities over WCF using net.tcp binding when letting the ObjectContext create dynamic proxies. To solve the problem I hade to extend the DataContractSerializerOperationBehavior and provide it with the dynamic proxies as “Known types”.

I also had to create a custom attribute that I could mark my methods (service operations) so that my custom DataContractSerializerOperationBehavior is invoked “only when needed”.

It was as simple as that. I also created an class that listens for the AppDomain.AssemblyLoad event and keeps track of the dynamic proxy types.

So if you are having these problems, feel free to use the code.

You also have to decorate your entities with the Serializable- and DataContract attribute with IsReference=true. Also you have to mark the members that should be serialized in your entites with the DataMember attribute.

The Interface that defines my service
I only mark the methods that uses entities that has a dynamic proxy with my custom EfDataContractSerializer attribute.

[ServiceContract]
public interface ISecurityService : IService
{
    [OperationContract]
    [EfDataContractSerializer]
    ServiceResponse<UserAccount> SetupNewUserAccount(UserAccount userAccount);

    [OperationContract]
    [EfDataContractSerializer]
    ServiceResponse<UserAccount> GetUserAccountByCredentials(string username, string password);

    [OperationContract]
    [EfDataContractSerializer]
    ServiceResponse UpdateUserAccount(UserAccount userAccount);

    [OperationContract]
    ServiceResponse SetNewPassword(string username, string currentPassword, string newPassword);
}

EfDataContractSerializerAttribute – For decorationg methods in your service

[Serializable]
public class EfDataContractSerializerAttribute : Attribute, IOperationBehavior
{
    private readonly EfDataContractSerializerOperationBehavior _innerOperationBehavior;
    
    public EfDataContractSerializerAttribute()
    {
    }

    public EfDataContractSerializerAttribute(OperationDescription operation)
    {
        _innerOperationBehavior = new EfDataContractSerializerOperationBehavior(operation);
    }

    public EfDataContractSerializerAttribute(OperationDescription operation, DataContractFormatAttribute dataContractFormatAttribute)
    {
        _innerOperationBehavior = new EfDataContractSerializerOperationBehavior(operation, dataContractFormatAttribute);
    }

    public void Validate(OperationDescription operationDescription)
    {
        if (_innerOperationBehavior == null)
            return;

        (_innerOperationBehavior as IOperationBehavior).Validate(operationDescription);
    }

    public void ApplyDispatchBehavior(OperationDescription operationDescription, DispatchOperation dispatchOperation)
    {
        ReplaceDataContractSerializerOperationBehavior(operationDescription);
    }

    public void ApplyClientBehavior(OperationDescription operationDescription, ClientOperation clientOperation)
    {
        ReplaceDataContractSerializerOperationBehavior(operationDescription);
    }

    public void AddBindingParameters(OperationDescription operationDescription, BindingParameterCollection bindingParameters)
    {
        if (_innerOperationBehavior == null)
            return;

        (_innerOperationBehavior as IOperationBehavior).AddBindingParameters(operationDescription, bindingParameters);
    }

    private void ReplaceDataContractSerializerOperationBehavior(OperationDescription description)
    {
        var operationBehavior = description.Behaviors.Find<DataContractSerializerOperationBehavior>();

        if (operationBehavior == null)
            return;

        description.Behaviors.Remove(operationBehavior);
        description.Behaviors.Add(new EfDataContractSerializerOperationBehavior(description));
    }
}

EfDataContractSerializerOperationBehavior – For populating the DataContractSerializer with known types

public class EfDataContractSerializerOperationBehavior : DataContractSerializerOperationBehavior
{
    public EfDataContractSerializerOperationBehavior(OperationDescription operation)
        : base(operation)
    {
    }

    public EfDataContractSerializerOperationBehavior(OperationDescription operation, DataContractFormatAttribute dataContractFormatAttribute)
        : base(operation, dataContractFormatAttribute)
    {
    }

    public override XmlObjectSerializer CreateSerializer(Type type, string name, string ns, IList<Type> knownTypes)
    {
        return new DataContractSerializer(type, name, ns, MergeKnownTypesWithDynamicProxyTypes(knownTypes));
    }

    public override XmlObjectSerializer CreateSerializer(Type type, XmlDictionaryString name, XmlDictionaryString ns, IList<Type> knownTypes)
    {
        return new DataContractSerializer(type, name, ns, MergeKnownTypesWithDynamicProxyTypes(knownTypes));
    }

    private static IEnumerable<Type> MergeKnownTypesWithDynamicProxyTypes(IEnumerable<Type> knownTypes)
    {
        var tmp = new HashSet<Type>();

        if (knownTypes != null)
            tmp.UnionWith(knownTypes);

        tmp.UnionWith(EfDynamicProxyAssemblies.GetTypes());

        return tmp;
    }
}

EfDynamicProxyAssemblies – For keeping track of currently generated dynamic proxies

public static class EfDynamicProxyAssemblies
{
    private static readonly object _lock = new object();
    private static readonly Dictionary<string, Assembly> _loadedAssemblies = new Dictionary<string, Assembly>();
    private static readonly HashSet<Assembly> _assembliesToExtract = new HashSet<Assembly>();
    private static readonly List<Type> _dynamixProxies = new List<Type>();

    static EfDynamicProxyAssemblies()
    {
        AppDomain.CurrentDomain.AssemblyLoad += new AssemblyLoadEventHandler(CurrentDomain_AssemblyLoad);
    }

    private static void CurrentDomain_AssemblyLoad(object sender, AssemblyLoadEventArgs args)
    {
        if (args == null || args.LoadedAssembly == null || string.IsNullOrEmpty(args.LoadedAssembly.FullName))
            return;

        lock (_lock)
        {
            if ((args.LoadedAssembly.FullName).StartsWith("EntityFrameworkDynamicProxies") &&
                !_loadedAssemblies.ContainsKey(args.LoadedAssembly.FullName))
            {
                _loadedAssemblies.Add(args.LoadedAssembly.FullName, args.LoadedAssembly);
                _assembliesToExtract.Add(args.LoadedAssembly);
                //Types aren't generated yet, so we can't add them to _dynamixEntityProxies
            }
        }
    }

    public static IEnumerable<Type> GetTypes()
    {
        lock(_lock)
        {
            var types = _assembliesToExtract.SelectMany(a => a.GetTypes()).ToList();
            _dynamixProxies.AddRange(types);

            _assembliesToExtract.Clear();
        }

        return _dynamixProxies;
    }
}

Enjoy!

//Daniel

Entity framework 4 – How-to reuse mappings and add a Concurrency token

I’m working on a larger sample (I will write or do a screencast about this shortly) which uses a distributed solution with WCF and the Entity framework 4 Code-only approach. Doing this I wanted to add a Concurrency token so that EF would trap concurrency violations, e.g:

1. I fetch an object which becomes detached.
2. The same object (seen to business equality) gets fetched in some other part of the application.

We now have to different instances representing the same domain object.

3. The entity from step 2 is updated and persisted.
4. When the entity from step 1 is sent to the object context for update, EF uses the Concurrency token field and gets 0-rows affected.

How do I add an concurrency token?
1. Add a property in your entity, which is of datatype byte[].
2. Add mapping for the field, stating that the added field is the Concurrency token and of which type (timestamp or rowversion)

Since I want all my entities to have this I add the property to my Entity-baseclass. I also have defined a base mapping class that maps this and the Id property for my entities, so that I don’t have to specify this all the time.

The Entities

public abstract class Entity
{
    public virtual int Id { get; set; }

    public virtual byte[] ConcurrencyToken { get; set; }
}
[Serializable]
public class UserAccount
    : Entity
{
    public virtual string Username { get; set; }

    public virtual string Password { get; set; }

    public virtual string Email { get; set; }

    public virtual string Firstname { get; set; }

    public virtual string Lastname { get; set; }
}

The mappings

[Serializable]
public abstract class EntityMapping<TEntity> : EntityConfiguration<TEntity>
    where TEntity : Entity
{
    protected EntityMapping()
    {
        HasKey(u => u.Id);
        Property(u => u.Id).IsIdentity();

        Property(u => u.ConcurrencyToken)
            .IsRequired()
            .IsConcurrencyToken()
            .HasStoreType("timestamp")
            .StoreGeneratedPattern = StoreGeneratedPattern.Computed;
    }
}
[Serializable]
public class UserAccountMapping : EntityMapping<UserAccount>
{
    public UserAccountMapping()
    {
        Property(u => u.Email)
            .IsRequired()
            .IsNotUnicode()
            .MaxLength = 150;

        Property(u => u.Username)
            .IsRequired()
            .IsUnicode()
            .MaxLength = 20;

        Property(u => u.Password)
            .IsRequired()
            .IsUnicode()
            .MaxLength = 20;

        Property(u => u.Firstname)
            .IsRequired()
            .IsUnicode()
            .MaxLength = 100;

        Property(u => u.Lastname)
            .IsRequired()
            .IsUnicode()
            .MaxLength = 100;
    }
}

//Daniel

Entity framework 4 – Code only – How-to change target names in the database table

For a couple of days ago I got the question of “my mappings doesn’t work”. I looked at it and found that the database tables were generated upfront and not by the ObjectContext. The columnnames had other names. Than how-do you solve this missmatch if you really do want to have separate namings? Using MapSingleType.

Personally, I let the context create the database and the tables, since I want the model to drive the database design.

The entity

[Serializable]
public class Car
{
    public string LicencePlate { get; set; }
    public int CountryCode { get; set; }
}

The mapping

[Serializable]
public class CarMapping : EntityConfiguration<Car>
{
    public CarMapping()
    {
        HasKey(c => new { c.CountryCode, c.LicencePlate });
        MapSingleType(c => 
            new
                {
                    TheCountryCode = c.CountryCode, 
                    TheLicencePlate = c.LicencePlate
                }).ToTable("dbo.TheCars");
    }
}

The result
Table: TheCars (you don’t have to use “ToTable”).
Columns:

  • TheCountryCode
  • TheLicencePlate

//Daniel