エンティティフレームワーク4.1データベース最初にDBContext T4生成クラスに主キーを追加しません
質問
「データベースの最初の」モードを試して、Entity Framework 4.1を始めたばかりです。 EFが「ado.net dbcontextジェネレーター」でモデルクラスを生成する場合、[key]属性を持つクラスの主要なキーを識別する必要はありませんか?これがなければ、T4 MVCScoffoldingと互換性がないように見えます。
詳細は次のとおりです。
Entity Data Model Designer GUIを使用して、既存のデータベースからモデルに簡単な「国」テーブルを追加しました。 GUIは、「PK」という名前の単一の整数IDキーフィールドを私の主キーとして正しく識別します。 (Alas!私は新しいユーザーなのでスクリーンショットを追加できません。代わりにCSDLを以下に含めました。)ただし、EFが「ADO.NET DBContextジェネレーター」を使用してコードを生成するとき、PKは識別されません生成されたクラスのキーフィールドとしてのフィールド(以下のコード抜粋を参照)。
「国」テーブルのCSDL:
<edmx:ConceptualModels>
<Schema Namespace="EpiDataModel" Alias="Self" xmlns:annotation="http://schemas.microsoft.com/ado/2009/02/edm/annotation" xmlns="http://schemas.microsoft.com/ado/2008/09/edm">
<EntityContainer Name="EpiModelEntities" annotation:LazyLoadingEnabled="true">
<EntitySet Name="countries" EntityType="EpiDataModel.country" />
</EntityContainer>
<EntityType Name="country">
<Key>
<PropertyRef Name="PK" />
</Key>
<Property Name="PK" Type="Int32" Nullable="false" annotation:StoreGeneratedPattern="Identity" />
<Property Name="Abbreviation" Type="String" Nullable="false" MaxLength="200" Unicode="false" FixedLength="false" />
<Property Name="Name" Type="String" MaxLength="1024" Unicode="false" FixedLength="false" />
<Property Name="Description" Type="String" MaxLength="1024" Unicode="false" FixedLength="false" />
<Property Name="Sequence" Type="Int32" />
</EntityType>
</Schema>
</edmx:ConceptualModels>
これが自動生成コードです:
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated from a template.
//
// Manual changes to this file may cause unexpected behavior in your application.
// Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
namespace MvcApplication1.Areas.Epi.Models
{
public partial class country
{
public int PK { get; set; }
public string Abbreviation { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public Nullable<int> Sequence { get; set; }
}
}
これは、MVCScaffolding T4テンプレートを使用してコントローラーを足場に足場にしようとすると問題を引き起こします。 「プロパティはプライマリキーのように見えない」というエラーが発生します。 NUGETパッケージマネージャーコンソールからのコマンドと出力は以下のとおりです。
PM> scaffold controller MvcApplication1.Areas.Epi.Models.country -Area Epi -NoChildItems -DbContextType MvcApplication1.Areas.Epi.Models.EpiModelEntities -Force
Scaffolding countriesController...
Get-PrimaryKey : Cannot find primary key property for type 'MvcApplication1.Areas.Epi.Models.country'. No properties appear to be primary keys.
At C:\work\EPI\EPIC_MVC3\sandbox\MvcApplication1\packages\MvcScaffolding.1.0.6\tools\Controller\MvcScaffolding.Controller.ps1:74 char:29
+ $primaryKey = Get-PrimaryKey <<<< $foundModelType.FullName -Project $Project -ErrorIfNotFound
+ CategoryInfo : NotSpecified: (:) [Get-PrimaryKey], Exception
+ FullyQualifiedErrorId : T4Scaffolding.Cmdlets.GetPrimaryKeyCmdlet
ただし、生成されたクラスを手動で変更して[キー]属性をフィールドに追加すると、上記のまったく同じ足場コマンドが正常に機能します。
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations; // manually added
namespace MvcApplication1.Areas.Epi.Models
{
public partial class country
{
[Key] // manually added
public int PK { get; set; }
public string Abbreviation { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public Nullable<int> Sequence { get; set; }
}
}
では、なぜEFデータベースが最初に、T4 MVCScaffoldingが一緒に機能しないのはなぜですか?そして、足場の問題がなくても、EFクラスは重要なフィールドが何であるかを知る必要はありませんか?
正しい解決策はありません