1

外部キーDynamicDataとEntityFramework4.0で問題が発生しています。実体関連に問題があるように感じますが、よくわかりません。挿入ページに外部キーを表す複数のフィールドがあります。データを挿入しようとすると、エラーが発生します。ReferentialConstraintの依存プロパティは、ストアで生成された列にマップされます。列:'CommentId'

私のデータは非常に基本的な1対多の関係であり、問​​題の外部キーはコメントテーブルのBookIdです。

  • BookId
  • BookHref

コメントコメント

  • CommentId
  • ユーザー
  • コメント
  • BookId

次のSQLスクリプトを使用してFOREIGNKEYを作成します。

ALTER TABLE [dbo].[Comments]  WITH CHECK ADD  CONSTRAINT [FK_Comments_Books] FOREIGN KEY([CommentId])
REFERENCES [dbo].[Books] ([BookId])

EntityFrameworkは次のXMLを生成します

<EntityType Name="Books">
  <Key>
    <PropertyRef Name="BookId" />
  </Key>
  <Property Name="BookId" Type="int" Nullable="false" StoreGeneratedPattern="Identity" />
  <Property Name="Title" Type="nvarchar" Nullable="false" MaxLength="255" />
  <Property Name="Description" Type="nvarchar" Nullable="false" MaxLength="2000" />
  <Property Name="Abstract" Type="nvarchar" />
  <Property Name="UserName" Type="nvarchar" Nullable="false" MaxLength="255" />
  <Property Name="Image" Type="varbinary(max)" />
  <Property Name="BookContent" Type="varbinary(max)" />
  <Property Name="rowguid" Type="uniqueidentifier" Nullable="false" />
  <Property Name="CreateDate" Type="datetime" />
  <Property Name="ModifiedDate" Type="datetime" />
</EntityType>
<EntityType Name="Comments">
  <Key>
    <PropertyRef Name="CommentId" />
  </Key>
  <Property Name="CommentId" Type="int" Nullable="false" StoreGeneratedPattern="Identity" />
  <Property Name="UserName" Type="nvarchar" Nullable="false" MaxLength="255" />
  <Property Name="UserComment" Type="nvarchar" Nullable="false" />
  <Property Name="BookId" Type="int" Nullable="false" />
</EntityType>
<Association Name="FK_Comments_Books">
  <End Role="Books" Type="BookStoreModel.Store.Books" Multiplicity="1" />
  <End Role="Comments" Type="BookStoreModel.Store.Comments" Multiplicity="0..1" />
  <ReferentialConstraint>
    <Principal Role="Books">
      <PropertyRef Name="BookId" />
    </Principal>
    <Dependent Role="Comments">
      <PropertyRef Name="CommentId" />
    </Dependent>
  </ReferentialConstraint>
</Association>

スキャフォールディングに処理を任せると、外部キーを表す複数のフィールドが表示されます コメントの動的データ出力

4

1 に答える 1

0

FK は次のようにすべきだと思います。

ALTER TABLE [dbo].[Comments]  WITH CHECK 
ADD  CONSTRAINT [FK_Comments_Books] FOREIGN KEY([BookId])
REFERENCES [dbo].[Books] ([BookId])

との間にCommentId1:1の関係を作成する定義を定義し、列はまったく使用されません。CommentBookBookIdComment

于 2011-03-21T07:05:11.687 に答える