【问题标题】:Entity Framework view associations not working实体框架视图关联不起作用
【发布时间】:2014-04-27 03:08:26
【问题描述】:

我有两个简单的观点

我需要使用 CustomerID 在 CustomerOrderView 和 CustomerView 之间创建关联。

我这样添加了协会:

不,这似乎是给我带来问题的地方。
我双击关联行并尝试添加新的引用约束。
如果我选择 CustomerOrderView 作为委托人,我会得到以下信息。

这不起作用,因为我试图让两个视图中的两个 CustomerID 都连接。
第 2 轮...所以这次我将 Principal 更改为 CustomerView,现在我可以看到 CustomerID 的连接。像这样。

但是如果我尝试保存实体文件,我会收到以下错误。
“错误 111:主体角色 CustomerOrderView 引用的属性必须与关系 TestModel.CustomerOrderViewCustomerView 的关系约束中主体角色引用的 EntityType TestModel.CustomerOrderView 的键完全相同。确保在主要角色。”

“错误 5 运行转换:多重性在关系‘CustomerOrderViewCustomerView’中的角色‘CustomerView’中无效。主要角色多重性的有效值为‘0..1’或‘1’。”

“错误 6 运行转换:关系‘CustomerOrderViewCustomerView’中的角色‘CustomerOrderView’中的多重性无效。因为从属角色属性不是关键属性,所以从属角色的多重性上限必须为 *。”

那么如何在实体框架中将视图连接在一起呢?

这里是 edmx 以获得更多帮助。

<?xml version="1.0" encoding="utf-8"?>
<edmx:Edmx Version="3.0" xmlns:edmx="http://schemas.microsoft.com/ado/2009/11/edmx">
  <!-- EF Runtime content -->
  <edmx:Runtime>
    <!-- SSDL content -->
    <edmx:StorageModels>
    <Schema Namespace="TestModel.Store" Provider="System.Data.SqlClient" ProviderManifestToken="2012" Alias="Self" xmlns:store="http://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator" xmlns:customannotation="http://schemas.microsoft.com/ado/2013/11/edm/customannotation" xmlns="http://schemas.microsoft.com/ado/2009/11/edm/ssdl">
        <!--Errors Found During Generation:
warning 6002: The table/view 'Test.dbo.CustomerOrderView' does not have a primary key defined. The key has been inferred and the definition was created as a read-only table/view.-->
        <EntityType Name="CustomerOrderView">
          <Key>
            <PropertyRef Name="CustomerOrderID" />
          </Key>
          <Property Name="CustomerOrderID" Type="numeric" Precision="9" Scale="0" Nullable="false" />
          <Property Name="OrderStatus" Type="char" MaxLength="1" />
          <Property Name="CustomerID" Type="numeric" Precision="6" Scale="0" />
        </EntityType>
        <!--Errors Found During Generation:
warning 6002: The table/view 'Test.dbo.CustomerView' does not have a primary key defined. The key has been inferred and the definition was created as a read-only table/view.-->
        <EntityType Name="CustomerView">
          <Key>
            <PropertyRef Name="CustomerID" />
          </Key>
          <Property Name="CustomerID" Type="numeric" Precision="6" Scale="0" Nullable="false" />
          <Property Name="CustomerName" Type="varchar" MaxLength="30" />
        </EntityType>
        <EntityContainer Name="TestModelStoreContainer">
          <EntitySet Name="CustomerOrderView" EntityType="Self.CustomerOrderView" store:Type="Views" store:Schema="dbo">
            <DefiningQuery>SELECT 
    [CustomerOrderView].[CustomerOrderID] AS [CustomerOrderID], 
    [CustomerOrderView].[OrderStatus] AS [OrderStatus], 
    [CustomerOrderView].[CustomerID] AS [CustomerID]
    FROM [dbo].[CustomerOrderView] AS [CustomerOrderView]</DefiningQuery>
          </EntitySet>
          <EntitySet Name="CustomerView" EntityType="Self.CustomerView" store:Type="Views" store:Schema="dbo">
            <DefiningQuery>SELECT 
    [CustomerView].[CustomerID] AS [CustomerID], 
    [CustomerView].[CustomerName] AS [CustomerName]
    FROM [dbo].[CustomerView] AS [CustomerView]</DefiningQuery>
          </EntitySet>
        </EntityContainer>
      </Schema></edmx:StorageModels>
    <!-- CSDL content -->
    <edmx:ConceptualModels>
      <Schema Namespace="TestModel" Alias="Self" annotation:UseStrongSpatialTypes="false" xmlns:annotation="http://schemas.microsoft.com/ado/2009/02/edm/annotation" xmlns:customannotation="http://schemas.microsoft.com/ado/2013/11/edm/customannotation" xmlns="http://schemas.microsoft.com/ado/2009/11/edm">
        <EntityContainer Name="TestEntities" annotation:LazyLoadingEnabled="true" >
          <EntitySet Name="CustomerOrderViews" EntityType="TestModel.CustomerOrderView" />
          <EntitySet Name="CustomerViews" EntityType="TestModel.CustomerView" />
          <AssociationSet Name="CustomerOrderViewCustomerView" Association="TestModel.CustomerOrderViewCustomerView">
            <End Role="CustomerOrderView" EntitySet="CustomerOrderViews" />
            <End Role="CustomerView" EntitySet="CustomerViews" />
          </AssociationSet>
        </EntityContainer>
        <EntityType Name="CustomerOrderView">
          <Key>
            <PropertyRef Name="CustomerOrderID" />
          </Key>
          <Property Name="CustomerOrderID" Type="Decimal" Nullable="false" Precision="9" Scale="0" />
          <Property Name="OrderStatus" Type="String" MaxLength="1" FixedLength="true" Unicode="false" />
          <Property Name="CustomerID" Type="Decimal" Precision="6" Scale="0" />
          <NavigationProperty Name="CustomerViews" Relationship="TestModel.CustomerOrderViewCustomerView" FromRole="CustomerOrderView" ToRole="CustomerView" />
        </EntityType>
        <EntityType Name="CustomerView">
          <Key>
            <PropertyRef Name="CustomerID" />
          </Key>
          <Property Name="CustomerID" Type="Decimal" Nullable="false" Precision="6" Scale="0" />
          <Property Name="CustomerName" Type="String" MaxLength="30" FixedLength="false" Unicode="false" />
          <NavigationProperty Name="CustomerOrderView" Relationship="TestModel.CustomerOrderViewCustomerView" FromRole="CustomerView" ToRole="CustomerOrderView" />
        </EntityType>
        <Association Name="CustomerOrderViewCustomerView">
          <End Type="TestModel.CustomerOrderView" Role="CustomerOrderView" Multiplicity="1" />
          <End Type="TestModel.CustomerView" Role="CustomerView" Multiplicity="*" />
          <ReferentialConstraint>
            <Principal Role="CustomerView">
              <PropertyRef Name="CustomerID" />
            </Principal>
            <Dependent Role="CustomerOrderView">
              <PropertyRef Name="CustomerID" />
            </Dependent>
          </ReferentialConstraint>
        </Association>
      </Schema>
    </edmx:ConceptualModels>
    <!-- C-S mapping content -->
    <edmx:Mappings>
      <Mapping Space="C-S" xmlns="http://schemas.microsoft.com/ado/2009/11/mapping/cs">
        <EntityContainerMapping StorageEntityContainer="TestModelStoreContainer" CdmEntityContainer="TestEntities" >
          <EntitySetMapping Name="CustomerOrderViews">
            <EntityTypeMapping TypeName="TestModel.CustomerOrderView">
              <MappingFragment StoreEntitySet="CustomerOrderView">
                <ScalarProperty Name="CustomerID" ColumnName="CustomerID" />
                <ScalarProperty Name="OrderStatus" ColumnName="OrderStatus" />
                <ScalarProperty Name="CustomerOrderID" ColumnName="CustomerOrderID" />
              </MappingFragment>
            </EntityTypeMapping>
          </EntitySetMapping>
          <EntitySetMapping Name="CustomerViews">
            <EntityTypeMapping TypeName="TestModel.CustomerView">
              <MappingFragment StoreEntitySet="CustomerView">
                <ScalarProperty Name="CustomerName" ColumnName="CustomerName" />
                <ScalarProperty Name="CustomerID" ColumnName="CustomerID" />
              </MappingFragment>
            </EntityTypeMapping>
          </EntitySetMapping>
        </EntityContainerMapping>
      </Mapping>
    </edmx:Mappings>
  </edmx:Runtime>
  <!-- EF Designer content (DO NOT EDIT MANUALLY BELOW HERE) -->
  <Designer xmlns="http://schemas.microsoft.com/ado/2009/11/edmx">
    <Connection>
      <DesignerInfoPropertySet>
        <DesignerProperty Name="MetadataArtifactProcessing" Value="EmbedInOutputAssembly" />
      </DesignerInfoPropertySet>
    </Connection>
    <Options>
      <DesignerInfoPropertySet>
        <DesignerProperty Name="ValidateOnBuild" Value="true" />
        <DesignerProperty Name="EnablePluralization" Value="true" />
        <DesignerProperty Name="IncludeForeignKeysInModel" Value="True" />
        <DesignerProperty Name="UseLegacyProvider" Value="false" />
        <DesignerProperty Name="CodeGenerationStrategy" Value="None" />
      </DesignerInfoPropertySet>
    </Options>
    <!-- Diagram content (shape and connector positions) -->
    <Diagrams></Diagrams>
  </Designer>
</edmx:Edmx>



编辑 -- 更多示例
我已将 1 交换为多个,以便“1”CustomerView 可以具有“Many”CustomerOrderView。

现在我收到以下错误:
“错误 4 错误 3031:从第 90 行开始映射片段时出现问题:表 CustomerOrderView 中的不可空列 CustomerOrderView.CustomerID 被映射到可空实体属性。” - 这不是真的。 CustomerOrderView.CustomerID 可以为空,但 CustomerView.CustomerID 不能为空。

“错误 5 运行转换:关系‘CustomerViewCustomerOrderView’中角色‘CustomerView’的多重性无效。因为从属角色中的所有属性都可以为空,所以主体角色的多重性必须为‘0..1’。”

然后,我将 CustomerOrderView.CustomerID 更改为不可为空,即使实际上可能存在尚未分配给 CustomerID 的订单。但在那次更改之后,我在运行时收到以下错误。
“'CustomerOrderView' 上的 'CustomerID' 属性无法设置为 'null' 值。您必须将此属性设置为 'System.Decimal' 类型的非空值。”

所以我进入 SQL 中的 CustomerOrderView 并添加一个 ISNULL(CustomerID,-1)。
现在它可以工作了,但这就是我想要的。 CustomerOrderView 应该可以为 Nullable。
我觉得我做错了什么或设置有问题。

编辑 -- 修复。
基本上我需要使用 0..1 对多的关系,其中 0..1 在 CustomerOrderView 端,而很多在 CustomerView 端。接下来我告诉它不要添加外键属性并手动添加它,以 CustomerView 作为 Principal 和 CustomerOrderView 作为 Dependent。

【问题讨论】:

  • 是否需要在两个具有一对多关系的表中创建关系?
  • 你可能是对的。我删除了关联并交换了一对多,但它仍然无法保存。关于“多重性”的相同错误
  • 能否添加错误截图。
  • 我在“EDIT”下添加了更多代码以显示更多问题。

标签: c# entity-framework ef-model-first


【解决方案1】:

我认为问题在于您的关系(一对多关系)。我认为一侧应该是客户视图表。然后就好了。

更新

发现问题我认为这两个属性不符合要求。这就是弹出错误的原因。检查CustomerView.CustomerID 可空属性是否为真。如果是,则更改为 false,如下所示

【讨论】:

  • 我确实看到了,并且确实将其从 True 更改为 False。我知道 CustomerView.CustomerID 应该有 CustomerID Nullable = False,但我不明白为什么 CustomerOrderView.CustomerID 还必须将 CustomerID Nullable 也设置为 False。这是否意味着 Entity Framework 不支持 Nullable 外键?
  • 感谢您的帮助。我建议您删除您的电子邮件地址或删除以前的评论。无论如何,您是正确的,因为 EF 确实支持可为空的。我将更新上面的代码以显示最终修复。基本上我必须使用 0..1 到多个,其中 0..1 在 CustomerOrderView 一侧,而许多在 CustomerView 一侧。
【解决方案2】:

我已经通过使表字段不为空来解决这个问题,因此,依赖于该字段的视图会将键作为实体框架中的主键。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-04-28
    • 2017-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多