2010-03-09 70 views
3

我目前正在使用VS 2010 RC,並且正在嘗試創建一個包含遞歸自引用實體的模型。目前,當我從模型中導入實體時,我收到一個錯誤,指出父屬性不能成爲關聯的一部分,因爲它被設置爲'Computed'或'Identity',但我不確定它爲什麼會這樣做。我一直在手動編輯文件來解決這個錯誤,但是模型根本無法工作。什麼是在Entity Framework中創建遞歸實體的正確方法?

讓遞歸實體在實體框架中工作的正確方法是什麼?

默認情況下導入的模型不起作用。我嘗試了幾次調整,最終每次都得到不同類型的錯誤。在這一點上,我正在尋找一個解決方案,並解釋爲什麼解決方案是正確的。

相關的數據庫對象

CREATE TABLE [dbo].[Appointments](
    [AppointmentId] [int] IDENTITY(1,1) NOT NULL, 
    [Description] [nvarchar](1024) NULL, 
    [Start] [datetime] NOT NULL, 
    [End] [datetime] NOT NULL, 
    [Username] [varchar](50) NOT NULL, 
    [RecurrenceRule] [nvarchar](1024) NULL, 
    [RecurrenceState] [varchar](20) NULL, 
    [RecurrenceParentId] [int] NULL, 
    [Annotations] [nvarchar](50) NULL, 
    [Application] [nvarchar](100) NOT NULL, 
    CONSTRAINT [PK_Appointments] PRIMARY KEY CLUSTERED 
    (
     [AppointmentId] ASC 
    ) 
) 
GO 

ALTER TABLE [dbo].[Appointments] WITH CHECK ADD CONSTRAINT [FK_Appointments_ParentAppointments] FOREIGN KEY([RecurrenceParentId]) 
REFERENCES [dbo].[Appointments] ([AppointmentId]) 
GO 

ALTER TABLE [dbo].[Appointments] CHECK CONSTRAINT [FK_Appointments_ParentAppointments] 
GO 
EDMX 

-

<Schema Namespace="SafetyTrackerModel.Store" Alias="Self" Provider="System.Data.SqlClient" ProviderManifestToken="2005" xmlns:store="http://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator" xmlns="http://schemas.microsoft.com/ado/2006/04/edm/ssdl"> 
    <EntityContainer Name="SafetyTrackerModelStoreContainer"> 
     <EntitySet Name="Appointments" EntityType="SafetyTrackerModel.Store.Appointments" store:Type="Tables" Schema="dbo" /> 
     <AssociationSet Name="FK_Appointments_ParentAppointments" Association="SafetyTrackerModel.Store.FK_Appointments_ParentAppointments"> 
     <End Role="Appointments" EntitySet="Appointments" /> 
     <End Role="Appointments1" EntitySet="Appointments" /> 
     </AssociationSet> 
    </EntityContainer> 
    <EntityType Name="Appointments"> 
     <Key> 
     <PropertyRef Name="AppointmentId" /> 
     </Key> 
     <Property Name="AppointmentId" Type="int" Nullable="false" StoreGeneratedPattern="Identity" /> 
     <Property Name="Description" Type="nvarchar" MaxLength="1024" /> 
     <Property Name="Start" Type="datetime" Nullable="false" /> 
     <Property Name="End" Type="datetime" Nullable="false" /> 
     <Property Name="Username" Type="varchar" Nullable="false" MaxLength="50" /> 
     <Property Name="RecurrenceRule" Type="nvarchar" MaxLength="1024" /> 
     <Property Name="RecurrenceState" Type="varchar" MaxLength="20" /> 
     <Property Name="RecurrenceParentId" Type="int" StoreGeneratedPattern="Identity" /> 
     <Property Name="Annotations" Type="nvarchar" MaxLength="50" /> 
     <Property Name="Application" Type="nvarchar" Nullable="false" MaxLength="100" /> 
    </EntityType> 
    <Association Name="FK_Appointments_ParentAppointments"> 
     <End Role="Appointments" Type="SafetyTrackerModel.Store.Appointments" Multiplicity="0..1" /> 
     <End Role="Appointments1" Type="SafetyTrackerModel.Store.Appointments" Multiplicity="*" /> 
     <ReferentialConstraint> 
     <Principal Role="Appointments"> 
      <PropertyRef Name="AppointmentId" /> 
     </Principal> 
     <Dependent Role="Appointments1"> 
      <PropertyRef Name="RecurrenceParentId" /> 
     </Dependent> 
     </ReferentialConstraint> 
    </Association> 
    </Schema></edmx:StorageModels> 
<edmx:ConceptualModels> 
    <Schema Namespace="SafetyTrackerModel" Alias="Self" xmlns="http://schemas.microsoft.com/ado/2006/04/edm"> 
    <EntityContainer Name="SafetyTrackerEntities"> 
     <EntitySet Name="Appointments" EntityType="SafetyTrackerModel.Appointment" /> 
     <AssociationSet Name="FK_Appointments_ParentAppointments" Association="SafetyTrackerModel.FK_Appointments_ParentAppointments"> 
     <End EntitySet="Appointments" Role="Appointment" /> 
     <End EntitySet="Appointments" Role="Appointment1" /> 
     </AssociationSet> 
     </EntityContainer> 
    <EntityType Name="Appointment"> 
     <Key> 
     <PropertyRef Name="AppointmentId" /> 
     </Key> 
     <Property xmlns:a="http://schemas.microsoft.com/ado/2009/02/edm/annotation" Name="AppointmentId" Nullable="false" a:StoreGeneratedPattern="Identity" Type="Int32" /> 
     <Property Name="Description" MaxLength="1024" FixedLength="false" Unicode="true" Type="String" /> 
     <Property Name="Start" Nullable="false" Type="DateTime" /> 
     <Property Name="End" Nullable="false" Type="DateTime" /> 
     <Property Name="Username" Nullable="false" MaxLength="50" FixedLength="false" Unicode="false" Type="String" /> 
     <Property Name="RecurrenceRule" MaxLength="1024" FixedLength="false" Unicode="true" Type="String" /> 
     <Property Name="RecurrenceState" MaxLength="20" FixedLength="false" Unicode="false" Type="String" /> 
     <Property Name="Annotations" MaxLength="50" FixedLength="false" Unicode="true" Type="String" /> 
     <Property Name="Application" Nullable="false" MaxLength="100" FixedLength="false" Unicode="true" Type="String" /> 
     <NavigationProperty Name="ChildAppointments" Relationship="SafetyTrackerModel.FK_Appointments_ParentAppointments" FromRole="Appointment" ToRole="Appointment1" /> 
     <NavigationProperty Name="ParentAppointment" Relationship="SafetyTrackerModel.FK_Appointments_ParentAppointments" FromRole="Appointment1" ToRole="Appointment" /> 
    </EntityType> 
    </Schema> 
</edmx:ConceptualModels> 
<edmx:Mappings> 
    <Mapping Space="C-S" xmlns="urn:schemas-microsoft-com:windows:storage:mapping:CS"> 
    <EntityContainerMapping StorageEntityContainer="SafetyTrackerModelStoreContainer" CdmEntityContainer="SafetyTrackerEntities"> 
     <EntitySetMapping Name="Appointments"> 
     <EntityTypeMapping TypeName="SafetyTrackerModel.Appointment"> 
      <MappingFragment StoreEntitySet="Appointments"> <!-- [1] --> 
      <ScalarProperty Name="Application" ColumnName="Application" /> 
      <ScalarProperty Name="Annotations" ColumnName="Annotations" /> 
      <ScalarProperty Name="RecurrenceState" ColumnName="RecurrenceState" /> 
      <ScalarProperty Name="RecurrenceRule" ColumnName="RecurrenceRule" /> 
      <ScalarProperty Name="Username" ColumnName="Username" /> 
      <ScalarProperty Name="End" ColumnName="End" /> 
      <ScalarProperty Name="Start" ColumnName="Start" /> 
      <ScalarProperty Name="Description" ColumnName="Description" /> 
      <ScalarProperty Name="AppointmentId" ColumnName="AppointmentId" /> 
      </MappingFragment> 
     </EntityTypeMapping> 
     </EntitySetMapping> 
     <AssociationSetMapping Name="FK_Appointments_ParentAppointments" TypeName="SafetyTrackerModel.FK_Appointments_ParentAppointments" StoreEntitySet="Appointments"> 
     <EndProperty Name="Appointment1"> 
      <ScalarProperty Name="AppointmentId" ColumnName="AppointmentId" /> 
     </EndProperty> 
     <EndProperty Name="Appointment"> 
      <ScalarProperty Name="AppointmentId" ColumnName="RecurrenceParentId" /> 
     </EndProperty> 
     <Condition ColumnName="AppointmentId" IsNull="false" /> <!-- Problem Here --> 
     </AssociationSetMapping> 
     </EntityContainerMapping> 
    </Mapping> 
</edmx:Mappings> 

正如我得到的編譯器錯誤:

條件不能爲柱狀部件指定 'AppointmentId',因爲它被標記爲'Computed'或'Identity'StoreGeneratedPattern。

如果移除了錯誤消失,而是簡單地介紹了另外一個約:

問題在映射片段起始於線3350,3380:EntityTypes SafetyTrackerModel.Appointment被映射到表約會相同的行。映射條件可用於區分這些類型映射到的行。

恰好是上面標記爲[1]的行。

在這一點上,我失去了我應該做的事情。我嘗試了很多很多事情,最終我已經用盡了智慧的東西來嘗試進入貨物崇拜的土地。

我真的很想了解問題在這裏。

+0

咦?爲什麼父母計算或身份?我認爲它應該只是一個FK到同一張桌子。你能顯示你的數據庫元數據嗎?另外,請考慮嵌套集模型。 – 2010-03-09 16:29:29

+1

你能給我們SQL表的定義嗎?我有自我引用的實體,並沒有這樣的問題。 – LukLed 2010-03-09 18:29:11

+0

已添加SQL表定義。 – 2010-03-11 20:09:53

回答

1

EF可以支持遞歸對象。 你只需要導入模型,EF會選擇關係,並允許你做遞歸。

請參閱MSDN

關於您的FK,我不明白您爲什麼擁有CHECK CONSTRAINT。 添加FK應該是這樣的:

ALTER TABLE Appointments 
ADD FOREIGN KEY (RecurrenceParentId) REFERENCES Appointments(AppointmentId); 

也許出了點問題,您的FK創作和EF不能處理它?

希望這會有所幫助!

編輯:哈哈,剛纔看到它被問了9個月前...好吧,沒關係:)

相關問題