Allow Entity Framework 4.5 to use datetime2 with SQL Server CE4

I am working on an Entity Framework project using SQL Server 2008. Recently, we have changed the use of the field type datetime2for many of our dates as we need precision.

This works great with our real-time databases and development, but as part of our end-to-end tests, we use SQL Server CE 4.0, which does not support type datetime2. The moment the Entity Framework tries to build the database, it returns a series of exceptions like this:

error 0040: The Type datetime2 is not qualified with a namespace or alias. Only primitive types can be used without qualification.

Obviously, there is no value in changing our production code for testing purposes, so is there a way to tell him to convert the values datetime2to normal datetimeor convert them to varchar?

The purpose of the test is to ensure that everything from the data layer to the interface works as expected, so if there is a better way to implement such a test, it can be a useful alternative.

+2
source share
2 answers

In the end, I found a solution to this problem that works enough for the end-to-end testing configuration I'm working with. The solution I used was to use a special DataContext to process SQL Server Sql queries, this way:

public class TestDataContext : DataContext 
{

    protected override void  OnModelCreating(System.Data.Entity.DbModelBuilder modelBuilder)
    {
        // list of available Conventions: http://msdn.microsoft.com/en-us/library/system.data.entity.modelconfiguration.conventions(v=vs.103).aspx 

        // Remove the column attributes that cause the datetime2 errors, so that 
        // for SQL Server CE we just have regular DateTime objects rather than using
        // the attribute value from the entity.
        modelBuilder.Conventions.Remove<ColumnAttributeConvention>();

        // Attempt to add back the String Length restrictions on the entities. I havent 
        // tested that this works.
        modelBuilder.Configurations.Add( new ComplexTypeConfiguration<StringLengthAttributeConvention>());

        // SQL Server CE is very sensitive to potential circular cascade deletion problems
        modelBuilder.Conventions.Remove<ManyToManyCascadeDeleteConvention>();
        modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();

    }

}

Replacing the regular DataContext with TestDataContext in my test classes, I have the same behavior as when SQL Server CE crashed.

+1
source

SQL Server 2012 LocalDB, CE. , SQL Server 2012 ( ), LocalDB - SQL Server. datetime2

+3

All Articles