[英]Entity Framework: Alternate solution to using non primary unique keys in an association
我知道實體框架不允許您使用非主唯一鍵作為外鍵關聯從數據庫生成模型。 我可以手動修改 EDMX 嗎? 如果是這樣,有人可以為我提供示例或參考嗎? 如果沒有,還有其他可能嗎?
最簡單的例子:
這是表的 DDL。 你會注意到我有一個從 PersonType.TypeCode 到 Person.TypeCode 的外鍵
CREATE TABLE [dbo].[PersonType](
[PersonTypeId] [int] NOT NULL,
[TypeCode] [varchar](10) NOT NULL,
[TypeDesc] [varchar](max) NULL,
CONSTRAINT [PK_PersonType] PRIMARY KEY CLUSTERED
([PersonTypeId] ASC)
CONSTRAINT [UK_PersonType] UNIQUE NONCLUSTERED
([TypeCode] ASC)
)
CREATE TABLE [dbo].[Person](
[PersonId] [int] NOT NULL,
[TypeCode] [varchar](10) NOT NULL,
CONSTRAINT [PK_Person] PRIMARY KEY CLUSTERED
([PersonId] ASC)
)
ALTER TABLE [dbo].[Person] WITH CHECK ADD CONSTRAINT [FK_Person_PersonType] FOREIGN KEY([TypeCode])
REFERENCES [dbo].[PersonType] ([TypeCode])
ALTER TABLE [dbo].[Person] CHECK CONSTRAINT [FK_Person_PersonType]
這是生成的 EDMX
<?xml version="1.0" encoding="utf-8"?>
<edmx:Edmx Version="1.0" xmlns:edmx="http://schemas.microsoft.com/ado/2007/06/edmx">
<!-- EF Runtime content -->
<edmx:Runtime>
<!-- SSDL content -->
<edmx:StorageModels>
<Schema Namespace="testModel.Store" Alias="Self" Provider="System.Data.SqlClient" ProviderManifestToken="2008" xmlns:store="http://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator" xmlns="http://schemas.microsoft.com/ado/2006/04/edm/ssdl">
<EntityContainer Name="testModelStoreContainer">
<EntitySet Name="Person" EntityType="testModel.Store.Person" store:Type="Tables" Schema="dbo" />
<EntitySet Name="PersonType" EntityType="testModel.Store.PersonType" store:Type="Tables" Schema="dbo" />
</EntityContainer>
<EntityType Name="Person">
<Key>
<PropertyRef Name="PersonId" />
</Key>
<Property Name="PersonId" Type="int" Nullable="false" />
<Property Name="TypeCode" Type="varchar" Nullable="false" MaxLength="10" />
</EntityType>
<!--Errors Found During Generation:
warning 6035: The relationship 'FK_Person_PersonType' has columns that are not part of the key of the table on the primary side of the relationship. The relationship was excluded.
-->
<EntityType Name="PersonType">
<Key>
<PropertyRef Name="PersonTypeId" />
</Key>
<Property Name="PersonTypeId" Type="int" Nullable="false" />
<Property Name="TypeCode" Type="varchar" Nullable="false" MaxLength="10" />
<Property Name="TypeDesc" Type="varchar(max)" />
</EntityType>
</Schema>
</edmx:StorageModels>
<!-- CSDL content -->
<edmx:ConceptualModels>
<Schema Namespace="testModel" Alias="Self" xmlns="http://schemas.microsoft.com/ado/2006/04/edm">
<EntityContainer Name="testEntities">
<EntitySet Name="People" EntityType="testModel.Person" />
<EntitySet Name="PersonTypes" EntityType="testModel.PersonType" />
</EntityContainer>
<EntityType Name="Person">
<Key>
<PropertyRef Name="PersonId" />
</Key>
<Property Name="PersonId" Type="Int32" Nullable="false" />
<Property Name="TypeCode" Type="String" Nullable="false" MaxLength="10" Unicode="false" FixedLength="false" />
</EntityType>
<EntityType Name="PersonType">
<Key>
<PropertyRef Name="PersonTypeId" />
</Key>
<Property Name="PersonTypeId" Type="Int32" Nullable="false" />
<Property Name="TypeCode" Type="String" Nullable="false" MaxLength="10" Unicode="false" FixedLength="false" />
<Property Name="TypeDesc" Type="String" MaxLength="Max" Unicode="false" FixedLength="false" />
</EntityType>
</Schema>
</edmx:ConceptualModels>
<!-- C-S mapping content -->
<edmx:Mappings>
<Mapping Space="C-S" xmlns="urn:schemas-microsoft-com:windows:storage:mapping:CS">
<EntityContainerMapping StorageEntityContainer="testModelStoreContainer" CdmEntityContainer="testEntities">
<EntitySetMapping Name="People"><EntityTypeMapping TypeName="testModel.Person"><MappingFragment StoreEntitySet="Person">
<ScalarProperty Name="PersonId" ColumnName="PersonId" />
<ScalarProperty Name="TypeCode" ColumnName="TypeCode" />
</MappingFragment></EntityTypeMapping></EntitySetMapping>
<EntitySetMapping Name="PersonTypes"><EntityTypeMapping TypeName="testModel.PersonType"><MappingFragment StoreEntitySet="PersonType">
<ScalarProperty Name="PersonTypeId" ColumnName="PersonTypeId" />
<ScalarProperty Name="TypeCode" ColumnName="TypeCode" />
<ScalarProperty Name="TypeDesc" ColumnName="TypeDesc" />
</MappingFragment></EntityTypeMapping></EntitySetMapping>
</EntityContainerMapping>
</Mapping>
</edmx:Mappings>
</edmx:Runtime>
我試圖修改 EDMX 以在 personType 和 Person 之間創建導航屬性,但沒有成功。 我只是想我可以以某種方式手動創建關聯。 任何幫助將不勝感激。
不幸的是,目前還沒有辦法在候選鍵(即PersonType.TypeCode )上定義關聯。 因為在 EF(3.5 和 4.0)中 FK 必須指向主鍵。
根據Alex James在他的帖子中的說法,這是 EF 團隊正在為下一個版本考慮的事情。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.