c# - EF6 Code First 使用数据注释填充 SQL 表和列的描述

标签 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()
        {
            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
    }

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

最佳答案

我建议创建自定义迁移操作,请参阅这篇文章:http://dolinkamark.wordpress.com/2014/05/03/creating-a-custom-migration-operation-in-entity-framework/

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

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

关于c# - EF6 Code First 使用数据注释填充 SQL 表和列的描述,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23905247/

相关文章:

c# - Environment.UserName 在 Windows 7、Windows 8.x 和 Windows 10 中会返回相同的结果吗?

c# - LINQ 查询根据匹配的另一个表从一个表返回

c# - 如何使用 Entity Framework 查询外键对象?

asp.net - 为什么 Entity Framework 在使用 ADO.NET 实体数据模型映射数据库表时会自动使用 ObjectContext 而不是 DbContext

c# - 更新单个属性的最佳方法?

c# - 以编程方式确定 VSS 数据库中的最近 checkin

c# - 尝试保存更新时由于主键相同而附加实体时出错

c# - Entity Framework 代码优先外键问题

asp.net-mvc - 使用EF连接现有数据库

c# - List<List<Vector3>> 完全重复