FreeSql (xviii) Navigation Properties

FreeSql navigation property is one of the features can be configured through the convention, or between self-defined configuration object relationships.

Navigation attributes OneToMany, ManyToOne, ManyToMany, OneToOne, Parent five configurations relationship.

With navigation attributes, multi-table queries will be very convenient, lambda expressions directly using the navigation objects little point, comfortable! !

In addition to the query There are many more features re-introduced in subsequent articles.

Custom navigation relationship

//导航属性,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; }
  • You may agree, may not be agreed;
  • No agreement, the associated characteristics are required for Navigate;
  • Unrelated, the query can specify On condition, LeftJoin (a => a.Parent.Id == a.ParentId);
  • Linked directly use the navigation objects on the line, On the conditions are appended automatically;

Conventions Configuration

OneToOne one

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 one, how to add data? "

Many-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 many

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 many, how to add data? "

Parent & Sons

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; }
}

Parent-child relationship, and many are actually similar, add the above parameter data connection;

ManyToMany-many

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, these three entities using OneToMany, ManyToOne, Parent, ManyToMany 4 species relationship.

Guess you like

Origin www.cnblogs.com/FreeSql/p/11531352.html