I'm using Entity Framework code-first mode, but how to auto-generate class field comments to database?
我使用的是实体框架代码优先模式,但如何将类字段注释自动生成到数据库?
Example:
示例:
[Table("User")]
public class User
{
/// <summary>
/// Id
/// </summary>
public long Id{get;set;}
/// <summary>
/// this is name
/// </summary>
public string name{get;set;}
}
SQL should be like this:
SQL应该这样:
CREATE TABLE User
(
id BIGINT(20) NOT NULL AUTO_INCREMENT COMMENT 'Id',
name VARCHAR(128) NOT NULL COMMENT 'this is name'
)
Does anyone have an idea how to achieve this?
有谁知道如何做到这一点吗?
更多回答
Short answer: not.
简短的回答是:不会。
优秀答案推荐
now, can use that:
现在,可以使用这一点:
using Namotion.Reflection; //need Namotion.Reflection in nuget
//Please enable GenerateDocumentationFile in project generation
private static void SetComment(ModelBuilder modelBuilder)
{
var entityTypes = modelBuilder.Model.GetEntityTypes();
foreach (var item in entityTypes)
{
var type = item.ClrType;
var t = XmlDocsExtensions.GetXmlDocsPath(type.Assembly,new());
var summary = type.GetXmlDocsSummary();
if (!string.IsNullOrEmpty(summary))
{
item.SetComment(summary);
}
var typeProperties = type.GetProperties();
var properties = item.GetProperties();
foreach (var property in properties)
{
summary = typeProperties.FirstOrDefault(a => a.Name == property.Name)
?.GetXmlDocsSummary();
if (!string.IsNullOrEmpty(summary))
{
property.SetComment(summary);
}
}
}
}
remember,must enable GenerateDocumentationFile:
<GenerateDocumentationFile>True</GenerateDocumentationFile>
请记住,必须启用生成文档文件:
True
To give a more specific answer besides "not." in the comments.
给出一个更具体的答案,而不是“不”。在评论中。
It's basically not possible to achieve this as comments are not passed over to the compiled object that is taken as the base for generating the migration elements.
这基本上是不可能实现的,因为注释不会传递给作为生成迁移元素的基础的编译对象。
In addition consider that the free comment section might contain a comment that might be used as a book (i.e. there's no limitation on comments but there is on comments in the database).
此外,考虑到自由评论部分可能包含可用作书籍的评论(即对评论没有限制,但在数据库中有评论)。
You might consider using a new attribute that might suit your needs, like:
您可以考虑使用可能适合您需要的新属性,例如:
[DbComment('This field is containing bla bla')]
public int FooBar {get; set;}
This might then be incorporated into the database generation process by overwriting the Sql-Generation classes.
然后,可以通过覆盖SQL生成类将其合并到数据库生成过程中。
The concern by using this approach is still that comments need to be maintained twice.
使用这种方法的问题仍然是注释需要维护两次。
更多回答
我是一名优秀的程序员,十分优秀!