Navigation properties are a feature of FreeSql and can be configured by convention or custom to configure relationships between objects.

The navigation properties are OneToMany, ManyToOne, ManyToMany, OneToOne and Parent.

With navigation properties, multi-table queries will be very convenient, lambda expressions directly use navigation objects dot dot, comfortable!!

There are more features besides queries that will be covered in future articles.

Customize navigation relationships

// Navigation properties, OneToMany
[Navigate("song_id")]
public virtual List<song_tag> Obj_song_tag { get; set; }

// Navigation properties, ManyToOne/OneToOne
[Navigate("song_id")]
public virtual Song Obj_song { get; set; }

// Navigation properties, ManyToMany
[Navigate(ManyToMany = typeof(tag_song))]
public virtual List<tag> tags { get; set; }
Copy the code
  • Can be agreed, can not agree;
  • If no, specify the Navigate attribute.
  • LeftJoin(a => a.parent. Id == a.parentid);
  • Associated, directly use the navigation object On the line, On condition will be automatically attached;

You can also use the FluentApi to set up navigation relationships externally:

FSQL. CodeFirst. ConfigEntity < entity class > (a = > a. Navigate (b = > b.r oles,null.typeof(many-to-many intermediate entities).Navigate(b => b."uid"));Copy the code

Priority, features > FluentApi

Conventional configuration

OneToOne 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; }}Copy the code

OneToOne One, How to Add data?

ManyToOne for one more

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; }}Copy the code

OneToMany 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; }}Copy the code

OneToMany 1 to many, How to Add data?

The Parent father and son

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; }}Copy the code

Parent-child relationships, which are similar to one-to-many relationships, add connections to data parameters;

ManyToMany 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; }}Copy the code

Song, Tag, and Song_tag use OneToMany, ManyToOne, Parent, and ManyToMany.

Series article navigation

  • (1) Introduction

  • (2) Automatic migration of entities

  • (3) Entity characteristics

  • (4) Solid features of Fluent Api

  • (5) Insert data

  • (6) Batch insert data

  • (7) Ignore columns when inserting data

  • (8) Specify columns when inserting data

  • (9) Delete data

  • (x) Update data

  • (11) Update data Where

  • (12) Specify columns when updating data

  • (13) Ignore columns when updating data

  • (14) Batch update data

  • (15) Query data

  • (16) paging query

  • (17) joint table query

  • (18) Navigation attributes

  • (19) multi-table query

  • (20) query where ecascade

  • (21) Query returned data

  • (22) Dto mapping query

  • (23) Grouping and aggregation

  • (24) Introduction To Linq To Sql syntax

  • (25) delayed loading

  • Include, IncludeMany, Dto, ToList

  • (27) the SQL statement has been written, and entity class mapping for the second query

  • (28) Business

  • Lambda expression

  • (30) Reading and writing separation

  • (31) Zoning table

  • (32) Aop

  • CodeFirst type mapping

  • (34) CodeFirst migration instructions

  • CodeFirst custom features

The resources

Beginner’s Guide | “Select” | “Update” | “Insert” | “Delete”
Expression function | “CodeFirst” | “DbFirst” | “The BaseEntity”
“Repository” | “The UnitOfWork” | The Filter | Optimism Lock | “The DbContext”
Unread | Partition table | “The tenants” | The AOP | Black Tech | Update log