1、Data Annotation
把配置以特性(Annotation)的形式标注在实体类中
[Table("T_Dogs")]
public class Dog
{
public int Id { get; set; }
[Required]
[MaxLength(22)]
public string Name { get; set; }
}
优点:简单
缺点:耦合
2、FluentAPI
builder.ToTable("T_Books");
把配置写到单独的配置类中
优点:解耦
缺点:复杂
public class Book
{
public long Id { get; set; } //主题
public string Title { get; set; } //标题
public DateTime PubTime { get; set; } //发布日期
}
class BookConfig : IEntityTypeConfiguration<Book>
{
public void Configure(EntityTypeBuilder<Book> builder)
{
builder.ToTable("T_Books");
builder.Property(b => b.Title).HasMaxLength(50).IsRequired();
}
}
3、大部分功能重叠。可以混用,但是不建议混用
本文对比了DataAnnotation通过特性标注的简单易用与FluentAPI的解耦复杂性,探讨两者在配置管理中的适用场景和权衡。
1275

被折叠的 条评论
为什么被折叠?



