Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 实体框架代码第一个SQL Server视图_Sql Server_Entity Framework_View_Ef Code First - Fatal编程技术网

Sql server 实体框架代码第一个SQL Server视图

Sql server 实体框架代码第一个SQL Server视图,sql-server,entity-framework,view,ef-code-first,Sql Server,Entity Framework,View,Ef Code First,我在数据库中定义了一个视图,然后在代码中编写了实体定义: [Persistence] [Table("ADMV_APPLICATION_OPTION")] public partial class ADMV_APPLICATION_OPTION { public string ID_APPLICATION_OPTION { get; set; } public string DS_APPLICATION_OPTION { get;

我在数据库中定义了一个视图,然后在代码中编写了实体定义:

    [Persistence]
    [Table("ADMV_APPLICATION_OPTION")]
    public partial class ADMV_APPLICATION_OPTION
    {
        public string ID_APPLICATION_OPTION { get; set; }
        public string DS_APPLICATION_OPTION { get; set; }
        public byte FL_TYPE { get; set; }
        public Nullable<double> OPTION_NUM_VALUE { get; set; }
        public string OPTION_STR_VALUE { get; set; }
        public string OPTION_XML_VALUE { get; set; }
        public System.Guid GUID_DIVISION_SAP { get; set; }
        public string ID_DIVISION_SAP { get; set; }
        public string ID_PLANT { get; set; }
    }
[持久性]
[表(“ADMV_应用程序_选项”)]
公共部分类ADMV_应用程序_选项
{
公共字符串ID_应用程序_选项{get;set;}
公共字符串DS_应用程序_选项{get;set;}
公共字节FL_类型{get;set;}
公共可为空的选项\u NUM\u值{get;set;}
公共字符串选项\u STR\u值{get;set;}
公共字符串选项\u XML\u值{get;set;}
public System.Guid Guid\u DIVISION\u SAP{get;set;}
公共字符串ID_DIVISION_SAP{get;set;}
公共字符串ID_PLANT{get;set;}
}
当我执行我的应用程序时,我得到了错误

在模型生成过程中检测到一个或多个验证错误:

MES.Core.ADMV_应用程序_选项::EntityType “ADMV_应用程序_选项”未定义密钥。为此定义密钥 EntityType

我是否也需要视图的键


谢谢

您的错误清楚地表明您需要添加PrimaryKey。EntityFramework中的视图也需要PK。您可以告诉EF,在SQL中创建视图时,使用
ISNULL
可以将某些列用作主键:

Create view SomeView
As
  Select 
      IsNull(YourUniqueId, -1) as YourUniqueId,
      ...
  From TableName
或者使用数据注释使用
[Key]
属性,将此属性设置为
ID\u应用程序\u选项
确保它是唯一的

    [Persistence]
    [Table("ADMV_APPLICATION_OPTION")]
    public partial class ADMV_APPLICATION_OPTION
    {
        [Key]
        public string ID_APPLICATION_OPTION { get; set; }
        public string DS_APPLICATION_OPTION { get; set; }
        public byte FL_TYPE { get; set; }
        public Nullable<double> OPTION_NUM_VALUE { get; set; }
        public string OPTION_STR_VALUE { get; set; }
        public string OPTION_XML_VALUE { get; set; }
        public System.Guid GUID_DIVISION_SAP { get; set; }
        public string ID_DIVISION_SAP { get; set; }
        public string ID_PLANT { get; set; }
    }
[持久性]
[表(“ADMV_应用程序_选项”)]
公共部分类ADMV_应用程序_选项
{
[关键]
公共字符串ID_应用程序_选项{get;set;}
公共字符串DS_应用程序_选项{get;set;}
公共字节FL_类型{get;set;}
公共可为空的选项\u NUM\u值{get;set;}
公共字符串选项\u STR\u值{get;set;}
公共字符串选项\u XML\u值{get;set;}
public System.Guid Guid\u DIVISION\u SAP{get;set;}
公共字符串ID_DIVISION_SAP{get;set;}
公共字符串ID_PLANT{get;set;}
}

您需要添加PrimaryKey,实体框架需要知道该键。同样对于视图?是视图也需要PK