关于 Sqlite 的 CodeFirst 返回
关于 Sqlite 的 CodeFirst
Model
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Text;
namespace TimeManagement.Model
{
public class MainTask
{
/// <summary>
/// 主键
/// </summary>
[SugarColumn(IsNullable = false, IsPrimaryKey = true, IsIdentity = true, Length = 36)]
public string IDINFO { get; set; }
/// <summary>
/// 任务ID
/// </summary>
[SugarColumn(IsNullable = false, Length = 36)]
public string TaskID { get; set; }
/// <summary>
/// 任务名称
/// </summary>
[SugarColumn(IsNullable = false, Length = 50)]
public string TaskName { get; set; }
/// <summary>
/// 標籤
/// </summary>
[SugarColumn(IsNullable = false, Length = 50)]
public string TagInfo { get; set; }
/// <summary>
/// Cron表達式
/// </summary>
[SugarColumn(IsNullable = false, Length = 50)]
public string Cron { get; set; }
/// <summary>
/// 執行方式 0:Get ;1:Post 默認為0
/// </summary>
[SugarColumn(IsNullable = false, Length = 2, DefaultValue = "0")]
public int ModeOfExecution { get; set; }
/// <summary>
/// 狀態 0:關閉 ; 1:開啟 默認0
/// </summary>
[SugarColumn(IsNullable = false, Length = 1, DefaultValue = "0")]
public int StatusInfo { get; set; }
/// <summary>
/// 超時時間 0:永不超時
/// </summary>
[SugarColumn(IsNullable = false, Length = 11)]
public int TimeOutPeriod { get; set; }
/// <summary>
/// 失敗重試次數
/// </summary>
[SugarColumn(IsNullable = false, Length = 11, DefaultValue = "0")]
public int NumberOfFailedRetries { get; set; }
/// <summary>
/// 失敗重試間隔時間
/// </summary>
[SugarColumn(IsNullable = false, Length = 11, DefaultValue = "0")]
public int FailedRetryInterval { get; set; }
}
}
对应的DbContext
public static SqlSugarClient DbAsync
{
get
{
string connectionString = "Data Source=Time_Management_DbInfo.db";
return new SqlSugarClient(new ConnectionConfig()
{
ConnectionString = connectionString,
DbType = DbType.Sqlite,
IsAutoCloseConnection = true,
IsShardSameThread = false,
InitKeyType = InitKeyType.Attribute
});
}
}
对应的测试
[TestClass]
public class BaseTest
{
[TestMethod]
public void GenerateDatabase()
{
DbContext.DbAsync.CodeFirst.BackupTable().InitTables<MainTask>();
}
}
报错
Test method TimeManagement.Test.BaseTest.GenerateDatabase threw exception:
SqlSugar.SqlSugarException: SQLite Error 1: 'AUTOINCREMENT is only allowed on an INTEGER PRIMARY KEY'.
疑问
这里说是int主键 , 但是我检查了好几次都是string , 并且string指定了长度 36 , 主键我也很确定只有一个 , 为什么生成还是报错呢? 后来我把主见修改为 int 且 不指定长度了 . 但是又报错 :
Test method TimeManagement.Test.BaseTest.GenerateDatabase threw exception:
SqlSugar.SqlSugarException: Specified method is not supported.
请问是什么原因? 特别是第二个报错 , 莫名奇妙 ????
热忱回答(1)
-
Aaron 傲 VIP0
2020/7/25顺带问一下, 如何才能让插入的代码比较好看 ??? 真丑 !!!
0 回复