使用VS 2015中的数据库中的EF Designer创建EF表,并且不会在生成的EF表中生成主键。 [英] Creating EF tables using the EF Designer from Database in VS 2015 and the primary keys are not generating in the resulting EF tables.

查看:301
本文介绍了使用VS 2015中的数据库中的EF Designer创建EF表,并且不会在生成的EF表中生成主键。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有主键的SQL数据库。当我尝试使用VS 2015中的实体数据模型向导添加实体数据模型并从数据库中选择EF Designer以获取模型应包含的内容时,生成的模式将显示包含主
键的表。(此处)是edmx文件中的代码片段:

I have an SQL database with Primary keys. When I try to add an Entity Data Model using the Entity Data Model Wizard in VS 2015 and selecting EF Designer from database for what should the model contain, the resulting schema displays the tables with the primary keys. (here is a code snippet from the edmx file):

  ; < EntityType Name =" BBS_Group_Tbl">

  <EntityType Name="BBS_Group_Tbl">

          < Key>

          <Key>

            < PropertyRef Name =" GroupID" />

            <PropertyRef Name="GroupID" />

          < / Key>

          </Key>

          < Property Name =" GroupID"类型=" INT" StoreGeneratedPattern = QUOT相同性QUOT;可空= QUOT假QUOT; />

          <Property Name="GroupID" Type="int" StoreGeneratedPattern="Identity" Nullable="false" />

          < Property Name =" GroupName"类型=" VARCHAR"的MaxLength = QUOT; 255" />

          <Property Name="GroupName" Type="varchar" MaxLength="255" />

          < Property Name =" GroupDescription"类型=" VARCHAR"的MaxLength = QUOT; 255" />

          <Property Name="GroupDescription" Type="varchar" MaxLength="255" />

          < Property Name =" CreatedBy"类型="炭"的MaxLength = QUOT; 20" />

          <Property Name="CreatedBy" Type="char" MaxLength="20" />

          < Property Name =" CreateDate"类型="日期时间" />

          <Property Name="CreateDate" Type="datetime" />

        < / EntityType>

        </EntityType>

        < EntityType Name =" BBS_Msg_Tbl">

        <EntityType Name="BBS_Msg_Tbl">

          < Key>

          <Key>

            < PropertyRef Name =" MsgID" />

            <PropertyRef Name="MsgID" />

          < / Key>

          </Key>

          < Property Name =" MsgID"类型=" INT" StoreGeneratedPattern = QUOT相同性QUOT;可空= QUOT假QUOT; />

          <Property Name="MsgID" Type="int" StoreGeneratedPattern="Identity" Nullable="false" />

          < Property Name =" TopicID"类型=" INT" />

          <Property Name="TopicID" Type="int" />

          < Property Name =" ReplyToID"类型=" VARCHAR"的MaxLength = QUOT; 75" />

          <Property Name="ReplyToID" Type="varchar" MaxLength="75" />

          < Property Name =" MsgTitle"类型=" VARCHAR"的MaxLength = QUOT; 255" />

          <Property Name="MsgTitle" Type="varchar" MaxLength="255" />

          < Property Name =" MsgText"类型=" VARCHAR"的MaxLength = QUOT; 4000" />

          <Property Name="MsgText" Type="varchar" MaxLength="4000" />

          < Property Name =" CreatedBy"类型="炭"的MaxLength = QUOT; 20" />

          <Property Name="CreatedBy" Type="char" MaxLength="20" />

          < Property Name =" CreateDate"类型="日期时间" />

          <Property Name="CreateDate" Type="datetime" />

        < / EntityType>

        </EntityType>

自动生成的结果ef表(类)不包括Key表示法:

The resulting ef tables (classes) that are autogenerated do not include the Key notation:

// -------------------------- -------------------------------------------------- -

//------------------------------------------------------------------------------

//<自动生成>

// <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>

// </auto-generated>

// --- -------------------------------------------------- -------------------------

//------------------------------------------------------------------------------

命名空间

PMDP6

{

   

   

使用 系统;

usingSystem;

   

   

使用 System.Collections.Generic;

usingSystem.Collections.Generic;

   

   

   

   

public partial class BBS_Group_Tbl

publicpartialclassBBS_Group_Tbl

    {

    {

       

       

public int GroupID
{
get ;
set ;}

publicintGroupID { get; set; }

     ;   

       

public string GroupName
{
get ;
set ;}

publicstringGroupName { get; set; }

     ;   

       

public string GroupDescription
{
get ;
set ;}

publicstringGroupDescription { get; set; }

     ;   

       

public string CreatedBy
{
get ;
set ;}

publicstringCreatedBy { get; set; }

     ;   

       

public Nullable < System。 DateTime >
CreateDate {
get ;
set ;}

publicNullable<System.DateTime> CreateDate { get; set; }

   }

    }

}

为什么?  当我尝试使用脚手架时,我收到错误,因为我的桌子没有主键。 如果我手动将[Key]符号放在表格中(我有超过50个),然后每当从数据库更新表格时,这些手动更改将被覆盖
。  我如何获得自动生成的表包含SQL数据库和EDMX架构中存在的主键吗?

Why?   When I try to then scaffold I get errors because my tables do not have primary keys.  If I manually put the [Key] notation in the tables (and I have over 50) then whenever a table is updated from the database, those manual changes will be overwritten.   How do I get the auto generated tables to include the Primary Keys that are present in the SQL database and the EDMX schema?

推荐答案

A2D4,

实体框架依赖于具有用于跟踪实体的密钥值的每个实体。其中一个约定该代码首先取决于它是如何暗示哪个属性是每个代码第一类中的键惯例是查找名为"ID"的
a属性或组合类名和"ID"的属性,例如" 的GroupID "。该属性将映射到数据库中的主键列。

Entity Framework relies on every entity having a key value that it uses for tracking entities. One of the conventions that code first depends on is how it implies which property is the key in each of the code first classes. That convention is to look for a property named "ID" or one that combines the class name and "ID", such as "GroupID". The property will map to a primary key column in the database.

>>为什么?  当我尝试使用脚手架时,我会收到错误,因为我的表没有主键。

根据您的描述,您的数据库表似乎有主键,什么操作会导致你的桌子丢失主键。

According to your description, it seems that your database tables have primary keys, what operation cause your tables lost primary keys.

祝你好运,

Cole Wu


这篇关于使用VS 2015中的数据库中的EF Designer创建EF表,并且不会在生成的EF表中生成主键。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆