AdventureWorks 无法创建 ado.net 实体数据模型

2022-01-23 00:00:00 sql-server ado.net visual-studio-2010

我正在尝试使用 microsoft 的示例数据库 AdventureWorks2008R2...当我尝试创建 ADO.NET 实体数据模型时,我收到此错误:

I'm trying to use microsoft's sample database AdventureWorks2008R2... when I try to create the ADO.NET entity data model I get this error:

Unable to generate the model because of the following exception: 'The table 'C:USERSXXXXDOCUMENTSVISUAL STUDIO 2010PROJECTSANOTHERWORKSANOTHERWORKSAPP_DATAADVENTUREWORKS2008R2_DATA.MDF.Production.Document' was referenced by a relationship, but was not found.'.
Loading metadata from the database took 00:00:06.2308687.
Generating the model took 00:00:04.5808698.
Added the connection string to the Web.Config file.
Successfully registered the assembly 'System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' in the Web.Config file.
Writing the .edmx file took 00:00:00.0015898.

有人遇到并解决了这个问题吗?或者知道我可以在某个地方下载该数据库的工作版本(除此之外:http://msftdbprodsamples.codeplex.com/releases/view/59211 这是我得到它的地方)

Anyone encountered and fixed this? Or knows somewhere I can download a working version of this database ( other then: http://msftdbprodsamples.codeplex.com/releases/view/59211 which is where I got it )

或者有人可以指向我可以下载并与实体框架一起使用的示例数据库.

Or can someone point to a sample database I can download and use around with entity framework.

推荐答案

编辑

新的 EF 设计器(Beta 1 可用这里) 不会尝试与不存在的表创建关系,因此您将获得一个模型,其中无效的实体类型/集和关系被注释掉,而不是错误和空模型.

The new EF designer (Beta 1 available here) will not try creating relationships to non-existing tables so instead of the error and empty model you will get a model where invalid entity types/sets and relationships are commented out.

**

我在 AdventureWorks for Sql Server 2012 中查看了它,但我认为它与您在 2008R2 中遇到的相同/这里的问题是 Production.Document 表有一个 HierarchyId 类型的键,而 EF 当前不支持 HierarchyId 类型.EF 在创建模型时会忽略它不理解的类型的列,但是如果它不理解键列,它将从模型中排除整个实体.对于排除的实体,当您使用 Xml/文本编辑器打开模型时,您应该能够在模型中找到它们被注释掉.在这种特殊情况下,将会看到以下内容:

I looked at it for AdventureWorks for Sql Server 2012 but I think it is the same thing you hit for 2008R2/ The issue here is that the Production.Document table has a key that is of HierarchyId type and EF currently does not support HierarchyId type. EF ignores columns of types it does not understand when creating the model however if it does not understand a key column it will exclude the entire entity from the model. For excluded entities you should be able to find them commented out in the model when you open it with an Xml/Text editor. In this particular case this is what will see:

<EntityContainer Name="AdventureWorksModelStoreContainer" />
    <!--Errors Found During Generation:
  warning 6005: The data type 'hierarchyid' is currently not supported for the target .NET Framework version; the column 'DocumentNode' in table 'AdventureWorks.Production.Document' was excluded.
  warning 6031: The column 'DocumentNode' on the table/view 'AdventureWorks.Production.Document' was excluded, and is a key column.  The table/view has been excluded.  Please fix the entity in the schema file, and uncomment.

  <EntityType Name="Document">
    <Property Name="DocumentLevel" Type="smallint" StoreGeneratedPattern="Computed" />
    <Property Name="Title" Type="nvarchar" Nullable="false" MaxLength="50" />
    <Property Name="Owner" Type="int" Nullable="false" />
    <Property Name="FolderFlag" Type="bit" Nullable="false" />
    <Property Name="FileName" Type="nvarchar" Nullable="false" MaxLength="400" />
    <Property Name="FileExtension" Type="nvarchar" Nullable="false" MaxLength="8" />
    <Property Name="Revision" Type="nchar" Nullable="false" MaxLength="5" />
    <Property Name="ChangeNumber" Type="int" Nullable="false" />
    <Property Name="Status" Type="tinyint" Nullable="false" />
    <Property Name="DocumentSummary" Type="nvarchar(max)" />
    <Property Name="Document" Type="varbinary(max)" />
    <Property Name="rowguid" Type="uniqueidentifier" Nullable="false" />
    <Property Name="ModifiedDate" Type="datetime" Nullable="false" />
  </EntityType>-->
  </Schema>

注意这个警告:
警告 6031:表/视图AdventureWorks.Production.Document"上的DocumentNode"列已被排除,并且是关键列.表/视图已被排除.请修复架构文件中的实体,然后取消注释.

Notice this warning:
warning 6031: The column 'DocumentNode' on the table/view 'AdventureWorks.Production.Document' was excluded, and is a key column. The table/view has been excluded. Please fix the entity in the schema file, and uncomment.

现在,在 AdventureWorks 数据库中,Production.Document 表由 Production.ProductDocument 表引用.由于没有创建 Production.Document 表的实体,EF 无法从 Production.ProductDocument 实体创建引用,因此Production.Document"被关系引用,但无法找到.错误.

Now in the AdventureWorks database the Production.Document table is referenced by Production.ProductDocument table. Since no entity for Production.Document table was created EF is unable to create the reference from Production.ProductDocument entity and hence the "Production.Document' is referenced by a relationship, but cannot be found.' error.

由于 EF 无法真正按原样"使用 Production.Document 表,因此最简单的解决方法是在从实体生成模型时排除该实体 - 在向导中检查除 Production.Document 之外的所有表,您应该能够去.EF 将忽略对该实体的所有引用,因为您已将其排除在外,因此应该没有错误.

Since the Production.Document table cannot be really used "as is" by EF the easiest workaround is to exclude this entity when generating the model from entity - check all tables but Production.Document in the wizard and you should be good to go. EF will ignore all references to this entity since you excluded it and therefore there should be no error.

链接到实体框架 codeplex 站点上的相关工作项

相关文章