提问模版

模版说明

提供这个模版好处有以下几点

1、方便你们复制,减少用例代码

2、方便你们验证是否是自已的原因

3、方便别人帮你找问题

控制台 模版

 这样一眼能看到全部代码,回答者很好给你解答

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using SqlSugar;
namespace OrmTest
{
class Program
{
    static void Main(string[] args)
    {
        var db = new SqlSugarScope(new SqlSugar.ConnectionConfig()
        {
            ConnectionString = "server=.;uid=sa;pwd=sasa;database=SQLSUGAR4XTEST",
            DbType = DbType.SqlServer,
            IsAutoCloseConnection = true
        });

        //建表 
        db.CodeFirst.InitTables<Test001>();
        //清空表
        db.DbMaintenance.TruncateTable<Test001>();


        //插入测试数据
        var result = db.Insertable(new Test001() { id = 1 }).ExecuteCommand();//用例代码

        Console.WriteLine(result);
        Console.WriteLine("用例跑完");
        Console.ReadKey();
    }
    //建类
    public class Test001
    {
        public int id { get; set; }
    }


}
}

API程序 模版

 这样一眼能看到全部代码,回答者很好给你解答

[HttpGet]
public object Get()
{
    var db=new  SqlSugarScope(new SqlSugar.ConnectionConfig() {
        ConnectionString = "server=.;uid=sa;pwd=sasa;database=SQLSUGAR4XTEST",
        DbType = DbType.SqlServer,
        IsAutoCloseConnection = true
    });

    //建表 
    if (!db.DbMaintenance.IsAnyTable("Test001", false))
    {
        db.CodeFirst.InitTables<Test001>();
    }
 

    //用例代码 
    var result=db.Insertable(new Test001() { id = 1 }).ExecuteCommand();//用例代码
       
 
    return result;
}
  //用例实体
  public class Test001 
  {
     public int id { get; set; }
  }

用户示例

其他用户提问题示例

https://www.donet5.com/ask/9/16415 

五、导航模版

 5.1 一对一

   public class UnitOneToOne
    {
        public static void Init() 
        {
            var db = NewUnitTest.Db;
            db.CodeFirst.InitTables<UnitPerson011, UnitAddress011>();
            db.DbMaintenance.TruncateTable<UnitPerson011, UnitAddress011>();

            var address = new UnitAddress011
            {
                Street = "123 Main Street"
            };
            int addressId = db.Insertable(address).ExecuteReturnIdentity();

            // 创建 UnitPerson011 对象并插入记录
            var person = new UnitPerson011
            {
                Name = "John Doe",
                AddressId = addressId
            };
            int personId = db.Insertable(person).ExecuteReturnIdentity();

            var list=db.Queryable<UnitPerson011>().Includes(x => x.Address).ToList();

        }
    }
    public class UnitPerson011
    {
        [SqlSugar.SugarColumn(IsPrimaryKey =true, IsIdentity = true)]
        public int Id { get; set; }
        public string  Name { get; set; }
        public int AddressId { get; set; }
        [SqlSugar.Navigate(SqlSugar.NavigateType.OneToOne,nameof(AddressId))]
        public UnitAddress011 Address { get; set; }
    }

    public class UnitAddress011
    {
        [SqlSugar.SugarColumn(IsPrimaryKey = true,IsIdentity =true)]
        public int Id { get; set; }
        public string Street { get; set; }
    }

 5.2 一对多

 public class UnitOneToMany
    {
        public static void Init() 
        {
            var db = NewUnitTest.Db;
            db.CodeFirst.InitTables<UnitPerson011, UnitAddress011>();
            db.DbMaintenance.TruncateTable<UnitPerson011, UnitAddress011>();

            var address = new UnitAddress011
            {
                Street = "123 Main Street"
            };
            int addressId = db.Insertable(address).ExecuteReturnIdentity();

            // 创建 UnitPerson011 对象并插入记录
            var person = new UnitPerson011
            {
                Name = "John Doe",
                AddressId = addressId
            };
            int personId = db.Insertable(person).ExecuteReturnIdentity();

            var list=db.Queryable<UnitAddress011>().Includes(x => x.Persons).ToList(); 
        }
    }
    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 class UnitAddress011
    {
        [SqlSugar.SugarColumn(IsPrimaryKey = true,IsIdentity =true)]
        public int Id { get; set; }
        public string Street { get; set; }
        [SqlSugar.Navigate(SqlSugar.NavigateType.OneToMany, nameof(UnitPerson011.AddressId))]
        public List<UnitPerson011> Persons { get; set; }
    }

 5.3 多对多

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SqlSugar;
namespace OrmTest
{
    internal class UnitManyToMany
	{
		public static void Init()
		{
			var db = NewUnitTest.Db;
			db.CodeFirst.InitTables<OperatorInfo, Role, OptRole>();
			db.DbMaintenance.TruncateTable<OperatorInfo, Role, OptRole>();
			db.Insertable(new OperatorInfo()
			{
				 id="1",
				  createTime=DateTime.Now,	
				   isDel=1,
				    isDisabled=1,
					 openid="",
					  phone="",
					   pwd="",
					    realname="a01",
						 remark="a",
						   sno="a",
						    username="a01"
			}).ExecuteCommand();
			db.Insertable(new OperatorInfo()
			{
				id = "2",
				createTime = DateTime.Now,
				isDel = 1,
				isDisabled = 1,
				openid = "",
				phone = "",
				pwd = "",
				realname = "a01",
				remark = "a",
				sno = "a",
				username = "admin"
			}).ExecuteCommand();
			db.Insertable(new OperatorInfo()
			{
				id = "3",
				createTime = DateTime.Now,
				isDel = 1,
				isDisabled = 1,
				openid = "",
				phone = "",
				pwd = "",
				realname = "a01",
				remark = "a",
				sno = "a",
				username = "admin"
			}).ExecuteCommand();
			var id=db.Insertable(new Role()
			{
				 id=1,
				  createTime=DateTime.Now,
				   name="admin"

			}).ExecuteReturnIdentity();
			var id2 = db.Insertable(new Role()
			{
				id = 2,
				createTime = DateTime.Now,
				name = "admin"

			}).ExecuteReturnIdentity();
			db.Insertable(new OptRole() { operId="1", roleId=id }).ExecuteCommand();
			db.Insertable(new OptRole() { id=2, operId = "2", roleId = id2 }).ExecuteCommand();
			
			var list1=db.Queryable<OperatorInfo>()
				.Includes(x => x.Roles)
				.ToList();
			var list2= db.Queryable<OperatorInfo>()
				.Includes(x => x.Roles).Where(x=>x.Roles.Any()).ToList();
            
        }

        /// <summary>
        /// 描述:
        /// 作者:synjones
        /// 时间:2022-04-20 21:30:28
        /// </summary>
        [SugarTable("unit_operatorinfo")]
		public partial class OperatorInfo
		{           /// <summary>
					/// 多角色
					/// </summary>
			[Navigate(typeof(OptRole), nameof(OptRole.operId), nameof(OptRole.roleId))]//名字换
			public List<Role> Roles { get; set; }
			/// <summary>
			/// 主键
			/// </summary>
			[SugarColumn(IsPrimaryKey = true)]
			public string id { get; set; }

			/// <summary>
			/// 姓名
			/// </summary>
			public string realname { get; set; }

			/// <summary>
			/// 账号
			/// </summary>
			public string username { get; set; }

			/// <summary>
			/// 密码
			/// </summary>
			public string pwd { get; set; }

			/// <summary>
			/// 学号
			/// </summary>
			public string sno { get; set; }

			/// <summary>
			/// openid
			/// </summary>
			public string openid { get; set; }

			/// <summary>
			/// 手机号码
			/// </summary>
			public string phone { get; set; }

			/// <summary>
			/// 备注信息
			/// </summary>
			public string remark { get; set; }

			/// <summary>
			/// 创建日期
			/// </summary>
			public DateTime createTime { get; set; }

			/// <summary>
			/// 状态(1:启用,2:禁用)
			/// </summary>
			public int isDisabled { get; set; }

			/// <summary>
			/// 是否删除(1:正常;2:删除)
			/// </summary>
			public int isDel { get; set; }

		}

		/// <summary>
		/// 描述:
		/// 作者:synjones
		/// 时间:2022-04-20 21:30:28
		/// </summary>
		[SugarTable("unit_role1")]
		public partial class Role
		{
			/// <summary>
			/// 角色
			/// </summary>
			[SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
			public int id { get; set; }

			/// <summary>
			/// 角色名称
			/// </summary>
			public string name { get; set; }

			/// <summary>
			/// 创建时间
			/// </summary>
			public DateTime createTime { get; set; }


		}

		/// <summary>
		/// 描述:
		/// 作者:synjones
		/// 时间:2022-04-21 14:35:09
		/// </summary>
		[SugarTable("unit_operator_role")]
		public partial class OptRole
		{
			/// <summary>
			/// 
			/// </summary>
			[SugarColumn(IsPrimaryKey = true)]
			public int id { get; set; }

			/// <summary>
			/// 
			/// </summary>
			public string operId { get; set; }

			/// <summary>
			/// 
			/// </summary>
			public int roleId { get; set; }


		}
	}
}

 5.4 Dynamic

支持多字段

    public class UnitDynamic
    {
        public static void Init() 
        {
            var db = NewUnitTest.Db;
            db.CodeFirst.InitTables<UnitPerson011, UnitAddress011>();
            db.DbMaintenance.TruncateTable<UnitPerson011, UnitAddress011>();

            var address = new UnitAddress011
            {
                Street = "123 Main Street"
            };
            int addressId = db.Insertable(address).ExecuteReturnIdentity();

            // 创建 UnitPerson011 对象并插入记录
            var person = new UnitPerson011
            {
                Name = "John Doe",
                AddressId = addressId
            };
            int personId = db.Insertable(person).ExecuteReturnIdentity();

            var list=db.Queryable<UnitAddress011>()
                .Includes(x =>
                  x.Persons.MappingField(y=>y.AddressId,()=>x.Id).ToList()//可以多字段匹配 MappingField(..).MappingField(..).Tolist()
                ).ToList(); 
        }
    }
    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 class UnitAddress011
    {
        [SqlSugar.SugarColumn(IsPrimaryKey = true,IsIdentity =true)]
        public int Id { get; set; }
        public string Street { get; set; }
        [SqlSugar.Navigate(SqlSugar.NavigateType.Dynamic,null)]
        public List<UnitPerson011> Persons { get; set; }
    }

 5.5一对多(多层级)

using SqlSugar;

    Console.WriteLine("Hello, World!");


    SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
    {
        ConnectionString = "Server=.;Database=TestDb;User=sa;Password=123456;",
        DbType = DbType.SqlServer,
        IsAutoCloseConnection = true,

    });

db.DbMaintenance.CreateDatabase();

db.CodeFirst.InitTables<Tab1, Tab2, Tab3, Tab4>();//Create Table

Tab1 tab1 = new Tab1();
tab1.Name = "111";
tab1.Tab2s = new List<Tab2>();
tab1.Tab2s.Add(new Tab2() { Name = "222" });

tab1.Tab2s[0].Tab3s = new List<Tab3>();
tab1.Tab2s[0].Tab3s.Add(new Tab3 { Name = "333" });

tab1.Tab2s[0].Tab3s[0].Tab4s = new List<Tab4>();
tab1.Tab2s[0].Tab3s[0].Tab4s.Add(new Tab4() { Name = "444" });

db.InsertNav(tab1)
    .Include(t1 => t1.Tab2s)
    .ThenInclude(t2 => t2.Tab3s)
    .ThenInclude(t3 => t3.Tab4s).ExecuteCommand();




[SqlSugar.SugarTable("Tab1")]
    public class Tab1
{
    [SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
    public long Id { get; set; }
    public string Name { get; set; }

    [SugarColumn(IsIgnore = true)]
    [Navigate(NavigateType.OneToMany, nameof(Tab2.Tab1Id))]
    public List<Tab2> Tab2s { get; set; }
}


[SqlSugar.SugarTable("Tab2")]
public class Tab2
{
    [SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
    public long Id { get; set; }
    public string Name { get; set; }

    public long Tab1Id { get; set; }

    [SugarColumn(IsIgnore = true)]
    [Navigate(NavigateType.OneToMany, nameof(Tab3.Tab2Id))]
    public List<Tab3> Tab3s { get; set; }
}


[SqlSugar.SugarTable("Tab3")]
public class Tab3
{
    [SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
    public long Id { get; set; }
    public string Name { get; set; }

    public long Tab1Id { get; set; }//问题:Tab1Id没有自动填充,如果想要填充改怎么写
    public long Tab2Id { get; set; }


    [SugarColumn(IsIgnore = true)]
    [Navigate(NavigateType.OneToMany, nameof(Tab4.Tab3Id))]
    public List<Tab4> Tab4s { get; set; }
}

[SqlSugar.SugarTable("Tab4")]
public class Tab4
{
    [SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
    public long Id { get; set; }
    public string Name { get; set; }
    public long Tab1Id { get; set; }//问题:Tab1Id没有自动填充,如果想要填充改怎么写
    public long Tab2Id { get; set; }//问题:Tab2Id没有自动填充,如果想要填充改怎么写
    public long Tab3Id { get; set; }
}

 5.6树型查询模版

WeatherForecastController.rar

关闭
果糖网