Skip to content
FreeSql 官方文档FreeSql 官方文档
指南
  • 指南

      • code-first
        • 实体特性✨
          • 流式接口
            • 优先级
            • 自定义特性
              • 类型映射
                • 导航属性✨
                • DB First
                  • 表达式函数
                    • 事务
                      • 过滤器
                        • ADO
                          • AOP✨
                            • 读写分离
                              • 分表分库
                                • 多租户
                                  • 性能
                                    • 你不知道的功能✨

                                    流式接口

                                    2021年2月5日大约 1 分钟约 386 字

                                    此页内容
                                    • 优先级

                                    # 流式接口

                                    FreeSql 提供使用 Fluent Api, 在外部配置实体的数据库特性,Fluent Api 的方法命名与特性名保持一致,如下:

                                    fsql.CodeFirst
                                        .ConfigEntity<TestFluenttb1>(a => 
                                        {
                                            a.Name("xxdkdkdk1");
                                            a.Property(b => b.Id).Name("Id22").IsIdentity(true);
                                            a.Property(b => b.name).DbType("varchar(100)").IsNullable(true);
                                        })
                                        .ConfigEntity<TestFluenttb2>(a =>
                                        {
                                            a.Name("xxdkdkdk2");
                                            a.Property(b => b.Id).Name("Id22").IsIdentity(true);
                                            a.Property(b => b.name).DbType("varchar(100)").IsNullable(true);
                                        });
                                    
                                    //以下为实体类
                                    class TestFluenttb1 {
                                        public int Id { get; set; }
                                        public string name { get; set; } = "defaultValue";
                                    }
                                    
                                    [Table(Name = "cccccdddwww")]
                                    class TestFluenttb2 {
                                        public int Id { get; set; }
                                        public string name { get; set; } = "defaultValue";
                                    }
                                    

                                    fsql 是一个 IFreeSql 对象

                                    这段配置尽量只执行一次,避免性能损耗

                                    参考:《实体特性说明》

                                    FreeSql.DbContext v1.4.0+ 实现了 EfCore FluentApi 99% 相似的语法

                                    fsql.CodeFirst.Entity<Song>(eb => {
                                        eb.ToTable("tb_song");
                                        eb.Ignore(a => a.Field1);
                                        eb.Property(a => a.Title).HasColumnType("varchar(50)").IsRequired();
                                        eb.Property(a => a.Url).HasMaxLength(100);
                                    
                                        eb.Property(a => a.RowVersion).IsRowVersion();
                                        eb.Property(a => a.CreateTime).HasDefaultValueSql("current_timestamp");
                                    
                                        eb.HasKey(a => a.Id);
                                        eb.HasIndex(a => new { a.Id, a.Title }).IsUnique().HasName("idx_xxx11");
                                    
                                        //一对多、多对一
                                        eb.HasOne(a => a.Type).HasForeignKey(a => a.TypeId).WithMany(a => a.Songs);
                                    
                                        //多对多
                                        eb.HasMany(a => a.Tags).WithMany(a => a.Songs, typeof(Song_tag));
                                    });
                                    
                                    fsql.CodeFirst.Entity<SongType>(eb => {
                                        eb.HasMany(a => a.Songs).WithOne(a => a.Type).HasForeignKey(a => a.TypeId);
                                        eb.HasData(new[]
                                        {
                                            new SongType
                                            {
                                                Id = 1,
                                                Name = "流行",
                                                Songs = new List<Song>(new[]
                                                {
                                                    new Song{ Title = "真的爱你" },
                                                    new Song{ Title = "爱你一万年" },
                                                })
                                            },
                                            new SongType
                                            {
                                                Id = 2,
                                                Name = "乡村",
                                                Songs = new List<Song>(new[]
                                                {
                                                    new Song{ Title = "乡里乡亲" },
                                                })
                                            },
                                        });
                                    });
                                    
                                    public class SongType {
                                        public int Id { get; set; }
                                        public string Name { get; set; }
                                    
                                        public List<Song> Songs { get; set; }
                                    }
                                    public class Song {
                                        [Column(IsIdentity = true)]
                                        public int Id { get; set; }
                                        public string Title { get; set; }
                                        public string Url { get; set; }
                                        public DateTime CreateTime { get; set; }
                                    
                                        public int TypeId { get; set; }
                                        public SongType Type { get; set; }
                                    
                                        public int Field1 { get; set; }
                                        public long RowVersion { get; set; }
                                    }
                                    

                                    # 优先级

                                    数据库特性 > 实体特性 > FluentApi(配置特性) > Aop(配置特性)

                                    在 GitHub 上编辑此页open in new window
                                    上次编辑于: 2021/2/5 上午12:03:18
                                    贡献者: luoyunchong
                                    上一页
                                    实体特性✨
                                    下一页
                                    自定义特性
                                    Copyright © 2018-present nicye