2023-09-29 15:49:49 +08:00
using System ;
using System.Collections.Generic ;
using System.Linq ;
using System.Text ;
using SqlSugar ;
namespace OrmTest
{
public class UnitNavDynamic
{
public static void Init ( )
{
var db = NewUnitTest . Db ;
var list = db . Queryable < UnitAddress011 > ( ) . Includes ( x = > x . Persons ) . ToList ( ) ;
2023-09-29 16:02:47 +08:00
var list2 = db . Queryable < UnitPerson011 > ( )
. Includes ( x = > x . adds )
. Includes ( x = > x . adds2 ) . ToList ( ) ;
if ( list . First ( ) . Persons . Count ( ) ! = 1 )
{
throw new Exception ( "unit error" ) ;
}
if ( list2 . First ( ) . adds = = null )
{
throw new Exception ( "unit error" ) ;
}
if ( list2 . Last ( ) . adds2 = = null )
{
throw new Exception ( "unit error" ) ;
}
2024-02-07 19:10:25 +08:00
var list3 = db . Queryable < UnitPerson011 > ( )
. Includes ( x = > x . adds )
. Where ( x = > x . adds . Id = = 1 ) . ToList ( ) ;
2024-02-08 16:48:24 +08:00
if ( db . Queryable < UnitPerson011 > ( )
. Where ( x = > x . adds . Id = = 1 ) . ToSqlString ( ) ! = "SELECT [Id],[Name],[AddressId],[AddressId2] FROM [UnitPerson0x1x1] [x] WHERE ( (SELECT TOP 1 Id FROM [UnitAddress0x1x1] WHERE [x].[AddressId]=[Id] AND [x].[AddressId]=[Id] ) = 1 )" )
{
throw new Exception ( "unit error" ) ;
}
2023-09-29 15:49:49 +08:00
}
[SqlSugar.SugarTable("UnitPerson0x1x1")]
public class UnitPerson011
{
[SqlSugar.SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
public int Id { get ; set ; }
public string Name { get ; set ; }
public int AddressId { get ; set ; }
public int AddressId2 { get ; set ; }
2023-10-08 01:04:00 +08:00
[SqlSugar.Navigate(SqlSugar.NavigateType.Dynamic, "[{m:\"AddressId\",c:\"Id\"},{m:\"AddressId\",c:\"Id\"}] ", " Id > 0 ")]
2023-09-29 16:02:47 +08:00
public UnitAddress011 adds { get ; set ; }
2023-10-08 01:04:00 +08:00
[SqlSugar.Navigate(SqlSugar.NavigateType.Dynamic, "[{m:\"AddressId2\",c:\"Id\"},{m:\"AddressId2\",c:\"Id\"}] ", " Id > 0 ")]
2023-09-29 16:02:47 +08:00
public UnitAddress011 adds2 { get ; set ; }
2023-09-29 15:49:49 +08:00
}
[SqlSugar.SugarTable("UnitAddress0x1x1")]
public class UnitAddress011
{
[SqlSugar.SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
public int Id { get ; set ; }
public string Street { get ; set ; }
[SqlSugar.Navigate(SqlSugar.NavigateType.Dynamic, "[{m:\"Id\",c:\"AddressId\"},{m:\"Id\",c:\"AddressId\"}] ")]
public List < UnitPerson011 > Persons { get ; set ; }
//[SqlSugar.Navigate(SqlSugar.NavigateType.OneToMany, nameof(UnitPerson011.AddressId2))]
//public List<UnitPerson011> Persons2 { get; set; }
}
}
}