实体框架视图关联不起作用

时间:2014-04-27 03:08:26

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

我有两个简单的观点 Model

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

我加入了这样的协会:
Association

这里似乎没有问题。
我双击关联线并尝试添加新的参考约束 如果我选择CustomerOrderView作为Principal我得到以下内容。
CustomerOrderView Constraint

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

但是如果我尝试保存实体文件,我将收到以下错误。
“错误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可以拥有“许多”CustomerOrderView Swapped one to many

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

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

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

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

编辑 - 修复。
基本上我需要使用0..1到多个关系,其中0..1位于CustomerOrderView端,许多位于CustomerView端。接下来我告诉它不要添加外键属性并手动将其添加为CustomerView作为Principal和CustomerOrderView作为Dependent。

2 个答案:

答案 0 :(得分:3)

我认为问题在于您的关系(1对多关系)。我认为一方应该是客户视图表。那就没事了。

<强>已更新

发现问题我认为这两个属性并不能满足彼此的要求。这就是错误弹出的原因。检查CustomerView.CustomerID可空属性是否为真。如果是,则将其改为false,如下所示

enter image description here

答案 1 :(得分:0)

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

相关问题