我刚刚开始使用Entity Framework 4.1,尝试"数据库优先"模式.当EF使用"ADO.Net DbContext Generator"生成Model类时,它不应该使用[Key]属性识别类的主键吗?没有它,它似乎与T4 MVCScaffolding不兼容.
以下是详细信息:
使用实体数据模型设计器GUI,我从现有数据库向模型添加了一个简单的"国家/地区"表.GUI正确地将名为"PK"的单个整数身份密钥字段标识为我的主键.(唉!我是新用户所以我无法添加屏幕截图.我已经在下面添加了CSDL.)但是,当EF使用"ADO.Net DbContext Generator"生成代码时,它无法识别PK field作为生成的类中的关键字段(请参阅下面的代码摘录).
"国家"表的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>
Run Code Online (Sandbox Code Playgroud)
这是自动生成的代码:
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated from a template. …Run Code Online (Sandbox Code Playgroud) primary-key entity-framework-4.1 ef-database-first dbcontext asp.net-mvc-scaffolding