gpt4 book ai didi

c# - Entity Framework View 关联不起作用

转载 作者:行者123 更新时间:2023-11-30 22:06:52 25 4
gpt4 key购买 nike

我有两个简单的观点
Model

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

我是这样添加协会的:
Association

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

这行不通,因为我正在尝试让两个 View 中的两个 CustomerID 进行连接。
第 2 轮...所以这次我将 Principal 更改为 CustomerView,现在我可以看到 CustomerID 都已连接。像这样。
CustomerView Constraint

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

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

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

那么如何在 Entity Framework 中将 View 连接在一起?

这里是 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 中的不可为 null 的列 CustomerOrderView.CustomerID 映射到可为 null 的实体属性。” - 这不是真的。 CustomerOrderView.CustomerID 可以为空,但 CustomerView.CustomerID 不可为空。

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

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

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

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

最佳答案

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

已更新

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

enter image description here

关于c# - Entity Framework View 关联不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23318929/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com