简体   繁体   中英

EF5 with custom T4 templates in Visual Studio 2013

We have a code-generation solution that uses the Entity Framework 5.0 designer in Visual Studio 2012 for Model-First code-generation. Because we needed to customize the code-generation, we used a custom workflow activity (und Common7\\IDE\\Extensions\\Microsoft\\Entity Framework Tools\\DbGen):

<Activity x:Class="MyWorkflow"
    xmlns="http://schemas.microsoft.com/netfx/2009/xaml/activities"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:s="clr-namespace:System;assembly=mscorlib"
    xmlns:sde="clr-namespace:System.Data.Metadata.Edm;assembly=System.Data.Entity,Version=5.0.0.0"
    xmlns:ded="clr-namespace:Microsoft.Data.Entity.Design.DatabaseGeneration.Activities;assembly=Microsoft.Data.Entity.Design"
    xmlns:dbtk="clr-namespace:OurCustomActivities;assembly=OurAssembly">
    <x:Members>
        <x:Property Name="Csdl" Type="InArgument(sde:EdmItemCollection)" />
        <x:Property Name="ExistingSsdl" Type="InArgument(s:String)" />
        <x:Property Name="ExistingMsl" Type="InArgument(s:String)" />
        <x:Property Name="Ssdl" Type="OutArgument(s:String)" />
        <x:Property Name="Msl" Type="OutArgument(s:String)" />
        <x:Property Name="Ddl" Type="OutArgument(s:String)" />
    </x:Members>
    <Sequence>
        <dbtk:CsdlToSsdlTemplateActivity SsdlOutput="[Ssdl]" TemplatePath="C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\Extensions\Microsoft\Entity Framework Tools\DBGen\CSDLToSSDL.tt"/>
        <dbtk:CsdlToMslTemplateActivity MslOutput="[Msl]" TemplatePath="C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\Extensions\Microsoft\Entity Framework Tools\DBGen\CSDLToMSL.tt"/>
        ...

This works well in a EF5 project in Visual Studio 2012.

However, in Visual Studio 2013 the same process does not work anymore. We adapted the template paths in the XAML above (to C:\\Program Files (x86)\\Microsoft Visual Studio 12.0\\Common7... ) and switched the Code Generation Strategy attribute of the entity model to Legacy ObjectContext .

When using the Generate database from model command in VS2013, we get the following message:

在此处输入图片说明

This means that the Csdl input parameter of the activity expects a

System.Data.Metadata.Edm.EdmItemCollection

but receives something else from the EF designer. I checked the file TablePerTypeStrategy.xaml of Visual Studio 2013 and saw that it seems to receive a

System.Data.Entity.Core.Metadata.Edm.EdmItemCollection

instead. This namespace clearly belongs to EF6 and not EF5.

So it seems that Visual Studio 2013 is inherently working with Entity Framework 6.0, although our project references EF5 solely.

Any ideas?

EDIT 1: I tried the suggestion by Pawel, ie replacing the namespace xmlns:sde by its EF6 counterpart. When I do that, the Generate database from model command shows the following errors:

The type 'System.Data.Entity.Core.Metadata.Edm.EdmItemCollection' is defined
in an assembly that is not referenced. You must add a reference to assembly
'EntityFramework, Version=6.0.0.0 ...

When I Nuget the following command in my .edmx project:

install-package EntityFramework -version 6.0.0.0

Nuget uninstalls version 5 of the EntityFramework package.

So we're forced to use EF6.

Is it possible to use EF5 with custom code generation in Visual Studio 2013?

The designer in VS2013 is using EF6 internally. Your activity expects EF5 EdmItemCollection but what is passed is EF6 EdmItemCollection . Note that type names and methods are the same but namespaces are not. They also come from different assemblies and therefore are totally different types. (EF5 was a starting point of EF6 where they forked). You may find more details about changes introduced in the designer in VS2013 in my post on the very subject. To solve your problem you want to take a look at the default TypePerTypeStrategy.xaml activity on codeplex. You will notice that the namespace for EF is as follows:

xmlns:sde="clr-namespace:System.Data.Entity.Core.Metadata.Edm;assembly=EntityFramework"

and I believe this is what you need to use in your activity. (Also check other namespaces and definitions - I don't think they changed but it's been a while since I looked at this).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM