mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-08-23 22:11:36 +08:00
Add demo
This commit is contained in:
parent
d343e79704
commit
7ceb56eace
@ -82,6 +82,7 @@
|
|||||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||||
<Compile Include="Unit\UInsert3.cs" />
|
<Compile Include="Unit\UInsert3.cs" />
|
||||||
<Compile Include="Unit\UnitInsertNavN.cs" />
|
<Compile Include="Unit\UnitInsertNavN.cs" />
|
||||||
|
<Compile Include="Unit\UnitOneToOneN.cs" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<None Include="App.config">
|
<None Include="App.config">
|
||||||
|
117
Src/Asp.Net/AccessTest/Unit/UnitOneToOneN.cs
Normal file
117
Src/Asp.Net/AccessTest/Unit/UnitOneToOneN.cs
Normal file
@ -0,0 +1,117 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using SqlSugar;
|
||||||
|
namespace OrmTest
|
||||||
|
{
|
||||||
|
public class UnitOneToOneN
|
||||||
|
{
|
||||||
|
public static void Init()
|
||||||
|
{
|
||||||
|
//创建数据库对象
|
||||||
|
SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
|
||||||
|
{
|
||||||
|
ConnectionString = Config.ConnectionString,//Master Connection
|
||||||
|
DbType = DbType.Access,
|
||||||
|
InitKeyType = InitKeyType.Attribute,
|
||||||
|
IsAutoCloseConnection = true
|
||||||
|
});
|
||||||
|
db.Aop.OnLogExecuting = (sql, pars) =>
|
||||||
|
{
|
||||||
|
Console.WriteLine(UtilMethods.GetSqlString(db.CurrentConnectionConfig.DbType, sql, pars));//输出sql,查看执行sql 性能无影响
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
//建表
|
||||||
|
|
||||||
|
if (!db.DbMaintenance.IsAnyTable("StudentA", false))
|
||||||
|
|
||||||
|
{
|
||||||
|
|
||||||
|
db.CodeFirst.InitTables<StudentA>();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!db.DbMaintenance.IsAnyTable("SchoolA", false))
|
||||||
|
|
||||||
|
{
|
||||||
|
|
||||||
|
db.CodeFirst.InitTables<SchoolA>();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!db.DbMaintenance.IsAnyTable("RoomA", false))
|
||||||
|
|
||||||
|
{
|
||||||
|
|
||||||
|
db.CodeFirst.InitTables<RoomA>();
|
||||||
|
|
||||||
|
}
|
||||||
|
db.Deleteable<StudentA>().ExecuteCommand();
|
||||||
|
db.Deleteable<SchoolA>().ExecuteCommand();
|
||||||
|
db.Insertable(new StudentA() { SchoolNo = 100, StudentId = 0 }).ExecuteCommand();
|
||||||
|
db.Insertable(new SchoolA() { SchoolId = 100, RoomId = 1 }).ExecuteCommand();
|
||||||
|
|
||||||
|
//用例代码
|
||||||
|
|
||||||
|
var result = db.Queryable<StudentA>()
|
||||||
|
|
||||||
|
.Includes(x => x.School)
|
||||||
|
|
||||||
|
.ToList();
|
||||||
|
|
||||||
|
if (result.First().School==null)
|
||||||
|
{
|
||||||
|
throw new Exception("unit error");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public class StudentA
|
||||||
|
|
||||||
|
{
|
||||||
|
|
||||||
|
[SugarColumn(IsPrimaryKey = true)]
|
||||||
|
|
||||||
|
public int StudentId { get; set; }
|
||||||
|
|
||||||
|
public int SchoolNo { get; set; }
|
||||||
|
|
||||||
|
[Navigate(NavigateType.OneToOne, nameof(SchoolNo) )]
|
||||||
|
|
||||||
|
public SchoolA School { get; set; }
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public class SchoolA
|
||||||
|
|
||||||
|
{
|
||||||
|
|
||||||
|
[SugarColumn(IsPrimaryKey = true)]
|
||||||
|
|
||||||
|
public int SchoolId { get; set; }
|
||||||
|
|
||||||
|
public int SchoolNo { get; set; }
|
||||||
|
|
||||||
|
public int RoomId { get; set; }
|
||||||
|
|
||||||
|
[Navigate(NavigateType.OneToOne, nameof(RoomId))]
|
||||||
|
|
||||||
|
public RoomA Room { get; set; }
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public class RoomA
|
||||||
|
|
||||||
|
{
|
||||||
|
|
||||||
|
[SugarColumn(IsPrimaryKey = true)]
|
||||||
|
|
||||||
|
public int RoomId { get; set; }
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user