Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.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
C# EF6代码首先使用数据注释填充SQL表和列上的描述_C#_Entity Framework_Ef Code First_Data Annotations_Code First - Fatal编程技术网

C# EF6代码首先使用数据注释填充SQL表和列上的描述

C# EF6代码首先使用数据注释填充SQL表和列上的描述,c#,entity-framework,ef-code-first,data-annotations,code-first,C#,Entity Framework,Ef Code First,Data Annotations,Code First,我的工作有一个标准,要求每个表和列(以及其他数据库对象)都有一个描述。我会通过SQL Management Studio中的属性窗口或TSQL脚本来实现这一点 是否可以使用数据注释(例如说明)生成迁移说明 [Description("List of Valid System Parameter Groups")] public class SystemParameterGroup { public SystemParameterGroup()

我的工作有一个标准,要求每个表和列(以及其他数据库对象)都有一个描述。我会通过SQL Management Studio中的属性窗口或TSQL脚本来实现这一点

是否可以使用数据注释(例如说明)生成迁移说明

    [Description("List of Valid System Parameter Groups")]
    public class SystemParameterGroup
    {
        public SystemParameterGroup()
        {
            this.SystemParameters = new ObservableCollection<SystemParameter>();
        }

        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]   
        [Description("PK: Unique Record Identifer")]
        public int SystemParameterGroupId { get; set; }

        [Required(ErrorMessage="Group Name is Required")]
        [MinLength(5, ErrorMessage="Must be at least 5 Characters")]
        [MaxLength(50, ErrorMessage="Must be Less than 50 Characters")]
        [Description("Name of System Parameter Group")]
        public string SystemParameterGroupName { get; set; }

        #region Child Records

        public virtual ObservableCollection<SystemParameter> SystemParameters { set; get; }
        #endregion
    }
[说明(“有效系统参数组列表”)]
公共类SystemParameterGroup
{
公共系统参数组()
{
this.SystemParameters=新的ObservableCollection();
}
[关键]
[数据库生成(DatabaseGeneratedOption.Identity)]
[说明(“主键:唯一记录标识符”)]
public int SystemParameterGroupId{get;set;}
[必需(ErrorMessage=“需要组名”)]
[MinLength(5,ErrorMessage=“必须至少包含5个字符”)]
[MaxLength(50,ErrorMessage=“必须少于50个字符”)]
[说明(“系统参数组名称”)]
公共字符串SystemParameterGroupName{get;set;}
#地区儿童记录
公共虚拟ObservableCollection系统参数{set;get;}
#端区
}

如何首先使用代码获取要使用的描述?

我建议创建一个自定义迁移操作,请参阅本文:

通过这种方式,您可以封装表的添加和删除描述,但这仅足以手动添加这些操作,代码优先不会在您的模型中自动找到它


要实现自动添加,必须使用MigrationScaffolder类。具体来说,您必须创建一个新类,该类继承自MigrationScaffolder类并覆盖其Scaffold函数。目前,我正在写一篇关于如何做这件事的文章。如果我完成了,我将用一个链接来扩展这篇文章。

我听取了马克的一些建议,并创建了自己的评论代码

我创建了以下两个函数CreateTableDescription和CreateColumnDescription代码:

    /// <summary>
    /// Add Descriptions to New Tables and/or Columns
    /// </summary>
    /// <param name="tableName">Name of Table</param>
    /// <param name="tableDescription">Description of Table</param>
    /// <returns></returns>
    public static string CreateTableDescription(string tableName, string tableDescription )
    {

            return string.Format("IF NOT EXISTS (SELECT NULL FROM SYS.EXTENDED_PROPERTIES WHERE [major_id] = OBJECT_ID('{0}') AND [name] = N'MS_Description' AND [minor_id] = 0) EXECUTE sp_addextendedproperty N'MS_Description', '{1}', N'SCHEMA', N'dbo', N'TABLE', N'{0}', NULL, NULL ELSE EXECUTE sp_updateextendedproperty N'MS_Description', '{1}', N'SCHEMA', N'dbo', N'TABLE', N'{0}', NULL, NULL", tableName, tableDescription);

    }
    /// <summary>
    /// Updates Descriptions on Tables and Columns
    /// </summary>
    /// <param name="tableName">Name of Table for Column</param>
    /// <param name="columnName">Name of Column</param>
    /// <param name="columDescription">Description for Column</param>
    /// <returns></returns>
    public static string CreateColumnDescription(string tableName, string columnName , string columDescription )
    {
        return string.Format("IF NOT EXISTS (SELECT NULL FROM SYS.EXTENDED_PROPERTIES WHERE [major_id] = OBJECT_ID('{0}') AND [name] = N'MS_Description' AND [minor_id] = (SELECT [column_id] FROM SYS.COLUMNS WHERE [name] = '{1}' AND [object_id] = OBJECT_ID('{0}'))) EXECUTE sp_addextendedproperty N'MS_Description', '{2}', N'SCHEMA', N'dbo', N'TABLE', N'{0}', N'COLUMN', N'{1}' ELSE EXECUTE sp_updateextendedproperty N'MS_Description', '{2}', N'SCHEMA', N'dbo', N'TABLE', N'{0}', N'COLUMN', N'{1}'", tableName, columnName, columDescription);
    }
RunSqlTransaction的代码是

private void RunSqlTransaction(string sqlStatement)
        {
            //split the script on "GO" commands
            string[] splitter = new string[] { "\r\nGO\r\n" };
            string[] commandTexts = sqlStatement.Split(splitter,
              StringSplitOptions.RemoveEmptyEntries);
            foreach (string commandText in commandTexts)
            {
            Sql(commandText.Replace("GO", ""));
            }
        }
可能重复的
private void RunSqlTransaction(string sqlStatement)
        {
            //split the script on "GO" commands
            string[] splitter = new string[] { "\r\nGO\r\n" };
            string[] commandTexts = sqlStatement.Split(splitter,
              StringSplitOptions.RemoveEmptyEntries);
            foreach (string commandText in commandTexts)
            {
            Sql(commandText.Replace("GO", ""));
            }
        }