日期:2014-05-20  浏览次数:21535 次

ef 生成错误:警告 6002: 表/视图 未定义主键
C# code

 <!--生成过程中发现错误:
      警告 6002: 表/视图“Northwind.dbo.Category Sales for 1997”未定义主键。已推断出该键,并将定义创建为只读的表/视图。
      -->
        <EntityType Name="Category Sales for 1997">
          <Key>
            <PropertyRef Name="CategoryName" />
          </Key>
          <Property Name="CategoryName" Type="nvarchar" Nullable="false" MaxLength="15" />
          <Property Name="CategorySales" Type="money" />
        </EntityType>
        <!--生成过程中发现错误:
      警告 6002: 表/视图“Northwind.dbo.Current Product List”未定义主键。已推断出该键,并将定义创建为只读的表/视图。
      -->
        <EntityType Name="Current Product List">
          <Key>
            <PropertyRef Name="ProductID" />
            <PropertyRef Name="ProductName" />
          </Key>
          <Property Name="ProductID" Type="int" Nullable="false" StoreGeneratedPattern="Identity" />
          <Property Name="ProductName" Type="nvarchar" Nullable="false" MaxLength="40" />
        </EntityType>
        <!--生成过程中发现错误:
      警告 6002: 表/视图“Northwind.dbo.Customer and Suppliers by City”未定义主键。已推断出该键,并将定义创建为只读的表/视图。
      -->
        <EntityType Name="Customer and Suppliers by City">
          <Key>
            <PropertyRef Name="CompanyName" />
            <PropertyRef Name="Relationship" />
          </Key>
          <Property Name="City" Type="nvarchar" MaxLength="15" />
          <Property Name="CompanyName" Type="nvarchar" Nullable="false" MaxLength="40" />
          <Property Name="ContactName" Type="nvarchar" MaxLength="30" />
          <Property Name="Relationship" Type="varchar" Nullable="false" MaxLength="9" />
        </EntityType>



这是什么问题呢???

------解决方案--------------------
在视图中包含主键,当然,这也不完全是错误,你收到的是警告,如果你的视图只是只读的,也可以不理会。
------解决方案--------------------
以你的第一个为例:
XML code

        <EntityType Name="Category Sales for 1997">
          <Key>
            <PropertyRef Name="CategoryName" />
          </Key>
          <Property Name="CategoryName" Type="nvarchar" Nullable="false" MaxLength="15" />
          <Property Name="CategorySales" Type="money" />
        </EntityType>