Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

如何使用 Code First 的 Fluent API 方式定义表字段信息? #1800

Closed
zfchai opened this issue May 7, 2024 · 3 comments
Closed

Comments

@zfchai
Copy link

zfchai commented May 7, 2024

  • FreeSql 使用 code firstFluent API 方式
 fsql.CodeFirst.ApplyConfiguration(new SongConfiguration());
  • Song 实体模型定义和 SongConfiguration
// 实体模型类举例
public class Song
{
    /// <summary>
    /// 主键,必填
    /// </summary>
    public Guid Id { get; set; }
    /// <summary>
    /// Title,必填
    /// </summary>
    public string Title { get; set; } = string.Empty;
    /// <summary>
    /// Url,必填
    /// </summary>
    public string Url { get; set; } = string.Empty;
    /// <summary>
    /// CreateTime,必填
    /// </summary>
    public DateTime CreateTime { get; set; }
    /// <summary>
    /// TypeId,必填
    /// </summary>
    public string TypeId { get; set; } = string.Empty;
    /// <summary>
    /// Field1, 可选
    /// </summary>
    public string? Field1 { get; set; }
    /// <summary>
    /// RowVersion,必填
    /// </summary>
    public long RowVersion { get; set; }
}

public class SongConfiguration : IEntityTypeConfiguration<Song>
{
    public void Configure(EfCoreTableFluent<Song> eb)
    {
         eb.ToTable("tb_song");
         /* 
          * 其他字段按照上面的注释具体该怎么描写,并且可以在数据库表中生成对应字段的备注描述。
          * 其次,字段命名在数据库可自定义,比如:RowVersion 字段,在数据库表中列名 row_version  
          */
    }
}

SongConfiguration 继承 IEntityTypeConfiguration<Song> 对象,在 Configure 方法中关于实体对象的表结构定义特性该怎么写,有参考文档说明麽?谢谢!

@luoyunchong
Copy link
Collaborator

luoyunchong commented May 15, 2024

注释就用///<summary>写是最合适的,不仅能生成数据库备注,也方便别人使用这个类

如果只是RowVersion 字段 转换成row_version 不需要主动写任何特性,可以在定义FreeSqlBuilder时指定UseNameConvert参数

 .UseNameConvert(NameConvertType.PascalCaseToUnderscoreWithLower)

可以看FluentApi文档,配置具体的内容:
https://freesql.net/guide/fluent-api.html#entity

比如,使用.Help()方法后可使用和特性标签同样的配置项

   builder.Property(rt => rt.LockoutEnd).Help().Name("");

@zfchai
Copy link
Author

zfchai commented May 17, 2024

感谢你的解答,我还想知道定义表结构,使用 Fluent API 风格模式,在 Configure 方法中 eb 具体使用参考有文档麽?

@pjy612
Copy link

pjy612 commented May 17, 2024

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants