Entity Relationship

Navigation properties are one of FreeSql's characteristic functions, which can be configured by agreement or customized configuration of the relationship between objects.

Navigation properties have five configuration relationships: OneToMany, ManyToOne, ManyToMany, OneToOne, and Parent.

With navigation properties, multi-table query is very convenient. Directly using navigation objects in lambda expressions can get the IDE's BUFF blessing.

  • Naming convention,or not (need to specify Navigate attribute association);
  • If there is no association relationship, you can specify the On condition when querying, LeftJoin(a => a.Parent.Id == a.ParentId);
  • If there is an association relationship, just use the navigation object directly, and the On condition will be automatically attached;

《What problems can navigation properties solve?》open in new window

Custom Navigation Relationship

//Navigation properties, OneToMany
[Navigate(nameof(song_tag.song_id))]
public virtual List<song_tag> Obj_song_tag { get; set; }
//Find the song_id property in song_tag and associate it with this ENTITY.PrimaryKey

//Navigation properties, ManyToOne/OneToOne
[Navigate(nameof(song_id))]
public virtual Song Obj_song { get; set; }
//Find the song_id property in THIS ENTITY and associate it with the Song.PrimaryKey

//Navigation properties, ManyToMany
[Navigate(ManyToMany = typeof(tag_song))]
public virtual List<tag> tags { get; set; }
1
2
3
4
5
6
7
8
9
10
11
12
13

You can also use FluentApi to set the navigation relationship externally:

fsql.CodeFirst.ConfigEntity<YOUR_ENTITY>(a => a
    .Navigate(b => b.roles, null, typeof(MANY_TO_MANY_MID_ENTITY))
    .Navigate(b => b.users, "uid")
);
1
2
3
4

Priority: Attribute> FluentApi

Note:

  1. Set Column(IsIgnore = true) on Property, then the navigation property will be invalid

  2. The string set by Navigate is the property name of the type, NOT THE TABLE IR FIELD NAME.

Detect Navigation Properties

How to detect whether a navigation property is configured to take effect:

var tbref = fsql.CodeFirst
    .GetTableByEntity(typeof(T))
    .GetTableRef("Children", true);
1
2
3

Method signature:

GetTableRef(string propertyName, bool isThrow);
1

Naming convention (no need to specify Navigate)

One-to-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; }
}
1
2
3
4
5
6
7
8
9
10
11

《How to add data in one-to-one mode?》open in new window

Many-to-One

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; }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14

One-to-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; }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

《How to add data in one-to-many mode?》open in new window

Parent and Children

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; }
}
1
2
3
4
5
6
7
8

The parent-children relationship is similar to One-to-Many mode. You can also refer to this link:

《How to add data in one-to-many mode?》open in new window

Many-to-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; }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

Song, Tag, Song_tag, these three entities use the four relationships: OneToMany, ManyToOne, Parent, and ManyToMany.

Reference