You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

86 lines
3.7 KiB

2 years ago
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. namespace SqlSugar
  5. {
  6. public class SqliteCodeFirst : CodeFirstProvider
  7. {
  8. public override void ExistLogic(EntityInfo entityInfo)
  9. {
  10. var tableName = GetTableName(entityInfo);
  11. var backupName = tableName + DateTime.Now.ToString("yyyyMMddHHmmss");
  12. Check.Exception(entityInfo.Columns.Count(it => it.IsPrimarykey)> 1, "Use Code First ,The primary key must not exceed 1");
  13. var columns = new List<DbColumnInfo>();
  14. if (entityInfo.Columns.HasValue())
  15. {
  16. foreach (var item in entityInfo.Columns.Where(it => !it.IsIgnore))
  17. {
  18. var dbColumnInfo = this.EntityColumnToDbColumn(entityInfo, tableName, item);
  19. columns.Add(dbColumnInfo);
  20. }
  21. }
  22. this.Context.DbMaintenance.BackupTable(tableName, backupName, int.MaxValue);
  23. this.Context.DbMaintenance.DropTable(tableName);
  24. this.Context.DbMaintenance.CreateTable(tableName,columns);
  25. }
  26. public override void NoExistLogic(EntityInfo entityInfo)
  27. {
  28. var tableName = GetTableName(entityInfo);
  29. var backupName=tableName+DateTime.Now.ToString("yyyyMMddHHmmss");
  30. Check.Exception(entityInfo.Columns.Count(it => it.IsPrimarykey)> 1, "Use Code First ,The primary key must not exceed 1");
  31. var columns = new List<DbColumnInfo>();
  32. if (entityInfo.Columns.HasValue())
  33. {
  34. foreach (var item in entityInfo.Columns.Where(it=> !it.IsIgnore))
  35. {
  36. var dbColumnInfo = this.EntityColumnToDbColumn(entityInfo, tableName, item);
  37. columns.Add(dbColumnInfo);
  38. }
  39. }
  40. this.Context.DbMaintenance.CreateTable(tableName, columns);
  41. }
  42. protected override DbColumnInfo EntityColumnToDbColumn(EntityInfo entityInfo, string tableName, EntityColumnInfo item)
  43. {
  44. var propertyType = UtilMethods.GetUnderType(item.PropertyInfo);
  45. var result = new DbColumnInfo
  46. {
  47. TableId = entityInfo.Columns.IndexOf(item),
  48. DbColumnName = item.DbColumnName.HasValue() ? item.DbColumnName : item.PropertyName,
  49. IsPrimarykey = item.IsPrimarykey,
  50. IsIdentity = item.IsIdentity,
  51. TableName = tableName,
  52. IsNullable = item.IsNullable,
  53. DefaultValue = item.DefaultValue,
  54. ColumnDescription = item.ColumnDescription,
  55. Length = item.Length
  56. };
  57. GetDbType(item, propertyType, result);
  58. if (result.DataType.Equals("varchar", StringComparison.CurrentCultureIgnoreCase) && result.Length == 0)
  59. {
  60. result.Length = 1;
  61. }
  62. return result;
  63. }
  64. protected override void ConvertColumns(List<DbColumnInfo> dbColumns)
  65. {
  66. foreach (var item in dbColumns)
  67. {
  68. if (item.DataType == "DateTime")
  69. {
  70. item.Length = 0;
  71. }
  72. }
  73. }
  74. protected override void ChangeKey(EntityInfo entityInfo, string tableName, EntityColumnInfo item)
  75. {
  76. this.Context.DbMaintenance.UpdateColumn(tableName, EntityColumnToDbColumn(entityInfo, tableName, item));
  77. if (!item.IsPrimarykey)
  78. this.Context.DbMaintenance.DropConstraint(tableName,null);
  79. if (item.IsPrimarykey)
  80. this.Context.DbMaintenance.AddPrimaryKey(tableName, item.DbColumnName);
  81. }
  82. }
  83. }