美文网首页.NETdotNET
FreeSql 教程 (十八)导航属性

FreeSql 教程 (十八)导航属性

作者: 叶先生_34e6 | 来源:发表于2020-03-14 09:47 被阅读0次

    FreeSql 以 MIT 开源协议托管于 github:https://github.com/2881099/FreeSql

    导航属性是 FreeSql 的特色功能之一,可通过约定配置、或自定义配置对象间的关系。

    导航属性有 OneToMany, ManyToOne, ManyToMany, OneToOne, Parent 五种配置关系。

    有了导航属性,多表查询会非常方便,lambda 表达式中直接使用导航对象点点点,舒服!!

    除了查询还有更多其他的特性在后续文章中再介绍。

    自定义导航关系

    //导航属性,OneToMany
    [Navigate("song_id")]
    public virtual List<song_tag> Obj_song_tag { get; set; }
    
    //导航属性,ManyToOne/OneToOne
    [Navigate("song_id")]
    public virtual Song Obj_song { get; set; }
    
    //导航属性,ManyToMany
    [Navigate(ManyToMany = typeof(tag_song))]
    public virtual List<tag> tags { get; set; }
    
    • 可约定,可不约定;
    • 不约定的,需指定 Navigate 特性关联;
    • 无关联的,查询时可以指明 On 条件,LeftJoin(a => a.Parent.Id == a.ParentId);
    • 已关联的,直接使用导航对象就行,On 条件会自动附上;

    也可以使用 FluentApi 在外部设置导航关系:

    fsql.CodeFirst.ConfigEntity<实体类>(a => a
        .Navigate(b => b.roles, null, typeof(多对多中间实体类))
        .Navigate(b => b.users, "uid")
    );
    

    优先级,特性 > FluentApi

    约定配置

    OneToOne 一对一

    class User {
        public int Id { get; set; } //Id、UserId、User_id
    
        public UserExt UserExt { get; set; }
    }
    
    class UserExt {
        public int id { get; set; } //Id、UserId、User_id、UserExtId、UserExt_id
    
        public User User { get; set; }
    }
    

    《OneToOne 一对一,怎么添加数据?》

    ManyToOne 多对一

    class Group {
        public int Id { get; set; } //Id、GroupId、Group_id
    }
    
    class User {
        public int Id { get; set; } //Id、UserId、User_id
    
    
        public int AGroupId { get; set; }
        public Group AGroup { get; set; }
    
        public int BGroupId { get; set; }
        public Group BGroup { get; set; }
    }
    

    OneToMany 一对多

    class Group {
        public int Id { get; set; } //Id、GroupId、Group_id
    
        public ICollection<User> AUsers { get; set; }
        public ICollection<User> BUsers { get; set; }
    }
    
    class User {
        public int Id { get; set; } //Id、UserId、User_id
    
    
        public int AGroupId { get; set; }
        public Group AGroup { get; set; }
    
        public int BGroupId { get; set; }
        public Group BGroup { get; set; }
    }
    

    《OneToMany 一对多,怎么添加数据?》

    Parent 父子

    class Group {
        public int Id { get; set; } //Id、GroupId、Group_id
    
        public int ParentId { get; set; } //ParentId、Parent_id
        public Group Parent { get; set; }
    
        public ICollection<Group> Childs { get; set; }
    }
    

    父子关系,与一对多其实差不多,添加数据参数上面的连接;

    ManyToMany 多对多

    class Song {
        [Column(IsIdentity = true)]
        public int Id { get; set; }
        public string Title { get; set; }
    
        public virtual ICollection<Tag> Tags { get; set; }
    }
    class Song_tag {
        public int Song_id { get; set; }
        public virtual Song Song { get; set; }
    
        public int Tag_id { get; set; }
        public virtual Tag Tag { get; set; }
    }
    class Tag {
        [Column(IsIdentity = true)]
        public int Id { get; set; }
        public string Name { get; set; }
    
        public int? Parent_id { get; set; }
        public virtual Tag Parent { get; set; }
    
        public virtual ICollection<Song> Songs { get; set; }
        public virtual ICollection<Tag> Tags { get; set; }
    }
    

    Song、Tag、Song_tag,这三个实体使用了 OneToMany、ManyToOne、Parent、ManyToMany 4种关系。

    系列文章导航

    相关文章

      网友评论

        本文标题:FreeSql 教程 (十八)导航属性

        本文链接:https://www.haomeiwen.com/subject/nljudhtx.html