允许实体框架4.5与SQL Server CE4使用DATETIME2 [英] Allow Entity Framework 4.5 to use datetime2 with SQL Server CE4

查看:295
本文介绍了允许实体框架4.5与SQL Server CE4使用DATETIME2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用SQL Server 2008中我们最近改为使用 DATETIME2 字段类型有很多我们的日期,因为我们需要在$ P上的实体框架项目工作$ pcision。

I am working on an Entity Framework project using SQL Server 2008. We recently changed to use the datetime2 field type for a lot of our Dates as we need the precision.

这工作对我们的生活和发展数据库罚款,但作为我们的终端到终端的测试的一部分,我们一直在使用SQL Server CE 4.0,它不支持 DATETIME2 类型。当下实体框架尝试构建它返回一系列像这样的异常的数据库:

This works fine against our live and development databases, but as part of our end-to-end tests we have been using SQL Server CE 4.0, which doesn't support the datetime2 type. The moment Entity Framework tries to construct 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.

显然,在改变着我们的生产code用于测试目的没有价值,所以有没有办法告诉它的 DATETIME2 值转换为常规日期时间或将其转换为 VARCHAR

Obviously, there is no value in changing our production code for test purposes, so is there a way to tell it to convert the datetime2 values to a regular datetime or converting them to a varchar?

该测试的目的是为了确保所有数据层中至接口作为预期的,因此,如果有实施这种测试可能提供有用的替代一个更好的方法。

The purpose of the test is to ensure that everything from the data layer up to the interface is working as expected, so if there is a better way to implement this kind of test that might provide a useful alternative.

推荐答案

在结束我发现了一个解决这个问题的作品足以使终端到终端的测试配置我一起工作。我去的解决方案是使用一种特殊的DataContext处理SQL服务器CE的要求,这样的:

In the end I found a solution to this problem that works sufficiently for the end-to-end testing configuration I am working with. The solution I went for was to use a special DataContext to handle Sql Server CE requests, thus:

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>();

    }

}

通过在我的测试类有TestDataContext取代常规的DataContext我有没有的SQL Server CE崩溃了相同的行为。

By replacing the regular DataContext with a TestDataContext in my test classes I have the same behaviour without SQL Server CE crashing out.

这篇关于允许实体框架4.5与SQL Server CE4使用DATETIME2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆