Metadata file not found - Data.Entity.Model

错误    3    正在编译转换: 未能找到元数据文件“F:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\Tools\..\IDE\Microsoft.Data.Entity.Design.DatabaseGeneration.dll”    E:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\Extensions\Microsoft\Entity Framework Tools\DBGen\SSDLToSQL10.tt    1    1    杂项文件

原错误信息:

纠结了一半天,总算在 stackoverflow 找到了答案,

http://stackoverflow.com/questions/19664833/metadata-file-not-found-data-entity-model#

(没读懂英文的可以看我下面的分析)

问题简单分析,看下创建 EF 生成模型时候产生的 SSDLToSql10.tt(这是用于 Code First 模式下生成的一个模板文件,主要是用来生成一个 DLL SQL SCRIPT [此处没深入了解,如有不妥请指教])

  1 <# 
  2 //---------------------------------------------------------------------
  3 // <copyright file="SsdlToSql10.tt" company="Microsoft">
  4 //      Copyright (c) Microsoft Corporation.  All rights reserved.
  5 // </copyright>
  6 //---------------------------------------------------------------------
  7 // This T4 template generates T-SQL from an instance of 
  8 // System.Data.Metadata.Edm.StoreItemCollection, an object representation
  9 // of the SSDL. This T-SQL is compatible with SQL 2012, 2008, 2005, CE, and Azure databases.
 10 //---------------------------------------------------------------------
 11 // Note: We will resolve all paths in assembly directives at runtime, taking 
 12 // macros and environment variables into account (e.g. $(ProjectDir), %VS120COMNTOOLS% etc.)
 13 #>
 14 <#@ assembly name="System.Core" #>
 15 <#@ assembly name="%VS120COMNTOOLS%..\IDE\Microsoft.Data.Entity.Design.DatabaseGeneration.dll"#>
 16 <#@ assembly name="%VS120COMNTOOLS%..\IDE\EntityFramework.dll"#>
 17 <#@ assembly name="%VS120COMNTOOLS%..\IDE\EntityFramework.SqlServer.dll" #>
 18 <#@ assembly name="%VS120COMNTOOLS%..\IDE\EntityFramework.SqlServerCompact.dll" #>
 19 <#@ import namespace="System.Linq" #>
 20 <#@ import namespace="System.Text" #>
 21 <#@ import namespace="System.Collections.Generic" #>
 22 <#@ import namespace="System.Data.Entity" #>
 23 <#@ import namespace="System.Data.Entity.Core.Metadata.Edm" #>
 24 <#@ import namespace="Microsoft.Data.Entity.Design.DatabaseGeneration" #>
 25 <#@ import namespace="System.Runtime.Remoting.Messaging" #>
 26 <#@ import namespace="System.Text.RegularExpressions" #>
 27 <#@ template language="C#" debug="true" hostspecific="true" #>
 28 <#@ include file="GenerateTSQL.Utility.ttinclude"#>
 29 <#@ output extension = ".sql" #>
 30 <#
 31 
 32 // +++++++++++++++++++++++++++++++++++++++++++++++++
 33 // Setup for the template (initializing variables, etc.)
 34 // +++++++++++++++++++++++++++++++++++++++++++++++++
 35 
 36     string databaseName = this.GetInput<string>(EdmParameterBag.ParameterName.DatabaseName.ToString());
 37     string edmxPath = this.GetInput<string>(EdmParameterBag.ParameterName.EdmxPath.ToString());
 38     Version targetVersion = this.GetInput<Version>(EdmParameterBag.ParameterName.TargetVersion.ToString());
 39 
 40     DbConfiguration.SetConfiguration(new TemplateDbConfiguration());
 41 
 42     if (false == InitializeAndValidateExistingStore()) 
 43     {
 44 #>
 45 -- Warning: There were errors validating the existing SSDL. Drop statements
 46 -- will not be generated.
 47 <#
 48     }
 49 #>
 50 -- --------------------------------------------------
 51 <#
 52     if (this.IsSQLCE) {
 53 #>
 54 -- Entity Designer DDL Script for SQL Server Compact Edition
 55 <#
 56     } else {
 57 #>
 58 -- Entity Designer DDL Script for SQL Server 2005, 2008, 2012 and Azure
 59 <#
 60     }
 61 #>
 62 -- --------------------------------------------------
 63 -- Date Created: <#=DateTime.Now#>
 64 <#
 65     if (!String.IsNullOrEmpty(edmxPath))
 66     {
 67 #>
 68 -- Generated from EDMX file: <#=Id(edmxPath)#>
 69 <#
 70     }
 71 #>
 72 -- --------------------------------------------------
 73 
 74 <#  if (!this.IsSQLCE) 
 75     { 
 76 #>
 77 SET QUOTED_IDENTIFIER OFF;
 78 GO
 79 <#  if (!String.IsNullOrEmpty(databaseName))
 80     {
 81 #>
 82 USE [<#=Id(databaseName)#>];
 83 GO
 84 <#
 85     }
 86     foreach (string unescapedSchemaName in (from es in Store.GetAllEntitySets() select es.GetSchemaName()).Distinct())
 87     {
 88 #>
 89 IF SCHEMA_ID(N'<#=Lit(unescapedSchemaName)#>') IS NULL EXECUTE(N'CREATE SCHEMA [<#=Id(unescapedSchemaName)#>]');
 90 <#
 91     }
 92 #>
 93 GO
 94 <#  } #>
 95 
 96 -- --------------------------------------------------
 97 -- Dropping existing FOREIGN KEY constraints
 98 <#  if (this.IsSQLCE)
 99     {
100 #>
101 -- NOTE: if the constraint does not exist, an ignorable error will be reported.
102 <#  } #>
103 -- --------------------------------------------------
104 
105 <#
106     foreach (AssociationSet associationSet in ExistingStore.GetAllAssociationSets())
107     {
108         ReferentialConstraint constraint = associationSet.ElementType.ReferentialConstraints.Single();
109         string constraintName = Id(WriteFKConstraintName(constraint));
110         AssociationSetEnd dependentSetEnd = associationSet.AssociationSetEnds.Where(ase => ase.CorrespondingAssociationEndMember == constraint.ToRole).Single();
111         string schemaName = Id(dependentSetEnd.EntitySet.GetSchemaName());
112         string dependentTableName = Id(dependentSetEnd.EntitySet.GetTableName());
113         
114         if (!this.IsSQLCE)
115         {
116 #>
117 IF OBJECT_ID(N'[<#=Lit(schemaName)#>].[<#=Lit(constraintName)#>]', 'F') IS NOT NULL
118 <#      } #>
119     ALTER TABLE <# if (!IsSQLCE) {#>[<#=schemaName#>].<#}#>[<#=dependentTableName#>] DROP CONSTRAINT [<#=constraintName#>];
120 GO
121 <#
122     }
123 #>
124 
125 -- --------------------------------------------------
126 -- Dropping existing tables
127 <#  if (this.IsSQLCE)
128     {
129 #>
130 -- NOTE: if the table does not exist, an ignorable error will be reported.
131 <#  } #>
132 -- --------------------------------------------------
133 
134 <#
135     foreach (EntitySet entitySet in ExistingStore.GetAllEntitySets())
136     { 
137         string schemaName = Id(entitySet.GetSchemaName());
138         string tableName = Id(entitySet.GetTableName());
139         
140         if (!this.IsSQLCE)
141         {
142 #>
143 IF OBJECT_ID(N'[<#=Lit(schemaName)#>].[<#=Lit(tableName)#>]', 'U') IS NOT NULL
144 <#      } #>
145     DROP TABLE <# if (!IsSQLCE) {#>[<#=schemaName#>].<#}#>[<#=tableName#>];
146 GO
147 <#
148     }
149 #>
150 
151 -- --------------------------------------------------
152 -- Creating all tables
153 -- --------------------------------------------------
154 
155 <#
156     foreach (EntitySet entitySet in Store.GetAllEntitySets())
157     {
158         string schemaName = Id(entitySet.GetSchemaName());
159         string tableName = Id(entitySet.GetTableName());
160 #>
161 -- Creating table '<#=tableName#>'
162 CREATE TABLE <# if (!IsSQLCE) {#>[<#=schemaName#>].<#}#>[<#=tableName#>] (
163 <#
164         for (int p = 0; p < entitySet.ElementType.Properties.Count; p++)
165         {
166             EdmProperty prop = entitySet.ElementType.Properties[p];
167 #>
168     [<#=Id(prop.Name)#>] <#=prop.ToStoreType()#> <#=WriteIdentity(prop, targetVersion)#> <#=WriteNullable(prop.Nullable)#><#=(p < entitySet.ElementType.Properties.Count - 1) ? "," : ""#>
169 <#
170         }
171 #>
172 );
173 GO
174 
175 <# 
176     } 
177 #>
178 -- --------------------------------------------------
179 -- Creating all PRIMARY KEY constraints
180 -- --------------------------------------------------
181 
182 <#
183     foreach (EntitySet entitySet in Store.GetAllEntitySets())
184     {
185         string schemaName = Id(entitySet.GetSchemaName());
186         string tableName = Id(entitySet.GetTableName());
187 #>
188 -- Creating primary key on <#=WriteColumns(entitySet.ElementType.GetKeyProperties(), ',')#> in table '<#=tableName#>'
189 ALTER TABLE <# if (!IsSQLCE) {#>[<#=schemaName#>].<#}#>[<#=tableName#>]
190 ADD CONSTRAINT [PK_<#=tableName#>]
191     PRIMARY KEY <# if (!IsSQLCE) {#>CLUSTERED <#}#>(<#=WriteColumns(entitySet.ElementType.GetKeyProperties(), ',')#> <# if (!IsSQLCE) {#>ASC<#}#>);
192 GO
193 
194 <#
195     }
196 #>
197 -- --------------------------------------------------
198 -- Creating all FOREIGN KEY constraints
199 -- --------------------------------------------------
200 
201 <#
202     foreach (AssociationSet associationSet in Store.GetAllAssociationSets())
203     {
204         ReferentialConstraint constraint = associationSet.ElementType.ReferentialConstraints.Single();
205         AssociationSetEnd dependentSetEnd = associationSet.AssociationSetEnds.Where(ase => ase.CorrespondingAssociationEndMember == constraint.ToRole).Single();
206         AssociationSetEnd principalSetEnd = associationSet.AssociationSetEnds.Where(ase => ase.CorrespondingAssociationEndMember == constraint.FromRole).Single();
207         string schemaName = Id(dependentSetEnd.EntitySet.GetSchemaName());
208         string dependentTableName = Id(dependentSetEnd.EntitySet.GetTableName());
209         string principalTableName = Id(principalSetEnd.EntitySet.GetTableName());
210 #>
211 -- Creating foreign key on <#=WriteColumns(constraint.ToProperties, ',')#> in table '<#=dependentTableName#>'
212 ALTER TABLE <#if (!IsSQLCE) {#>[<#=schemaName#>].<#}#>[<#=dependentTableName#>]
213 ADD CONSTRAINT [<#=WriteFKConstraintName(constraint)#>]
214     FOREIGN KEY (<#=WriteColumns(constraint.ToProperties, ',')#>)
215     REFERENCES <# if (!IsSQLCE) {#>[<#=schemaName#>].<#}#>[<#=principalTableName#>]
216         (<#=WriteColumns(constraint.FromProperties, ',')#>)
217     ON DELETE <#=GetDeleteAction(constraint)#> ON UPDATE NO ACTION;
218 <#      
219         // if the foreign keys are part of the primary key on the dependent end, then we should not add a constraint.
220         if (!dependentSetEnd.EntitySet.ElementType.GetKeyProperties().Take(constraint.ToProperties.Count()).OrderBy(r => r.Name).SequenceEqual(constraint.ToProperties.OrderBy(r => r.Name)))
221         {
222 #>
223 
224 -- Creating non-clustered index for FOREIGN KEY '<#=WriteFKConstraintName(constraint)#>'
225 CREATE INDEX [IX_<#=WriteFKConstraintName(constraint)#>]
226 ON <#if (!IsSQLCE) {#>[<#=schemaName#>].<#}#>[<#=dependentTableName#>]
227     (<#=WriteColumns(constraint.ToProperties, ',')#>);
228 <#      
229         }
230 #>
231 GO
232 
233 <#
234     }
235 #>
236 -- --------------------------------------------------
237 -- Script has ended
238 -- --------------------------------------------------
SSDLToSql10.tt

这里面就可以看到它引用了这几个程序集

<#@ assembly name="System.Core" #>
<#@ assembly name="%VS120COMNTOOLS%..\IDE\Microsoft.Data.Entity.Design.DatabaseGeneration.dll"#>
<#@ assembly name="%VS120COMNTOOLS%..\IDE\EntityFramework.dll"#>
<#@ assembly name="%VS120COMNTOOLS%..\IDE\EntityFramework.SqlServer.dll" #>
<#@ assembly name="%VS120COMNTOOLS%..\IDE\EntityFramework.SqlServerCompact.dll" #>

索性我发现这几个程序集确实没在编译报错的路径中,我直接 copy 粘贴到了指定的目录下甚至还在编译器中引用,编译依旧如此。有点棘手!

最后看了老外的分析,说是 visual studio 安装的环境变量值路径不对,后来才一愣发现真是这个问题

控制面板->系统->高级系统设置->高级->环境变量

最终才明白原来编译的路径依据是根据环境变量来的,真是图样图森破

 

posted @ 2015-10-05 23:50  许大虾  阅读(1694)  评论(0编辑  收藏  举报