[CodeFirst] 实体增加字段后,数据库表并没有增加相应的字段 返回

SqlSugar 处理完成
17 384
该叫什么 Jimex 发布于2023/9/14
悬赏:0 飞吻

使用如下 的代码生成数据库, 但实体中新增的字段 ForceCloseTriggerMessage 并没有在数据库中创建, 请问会是什么原因造成的?


 SugarIocServices.AddSqlSugar(new IocConfig()
{
    ConnectionString = $"DataSource=d:\temp\test.db",
    DbType = IocDbType.Sqlite,
    IsAutoCloseConnection = true

}); //多个库就传List<IocConfig>
    //配置参数
SugarIocServices.ConfigurationSugar(db =>
{

    db.CurrentConnectionConfig.LanguageType = LanguageType.English;
    if (ClientAppConfig.IsTestEnvironment)
    {
        db.Aop.OnLogExecuting = (sql, p) =>
        {

            AppLogger.LogSqlInfo(sql);

        };
    }
});

Type[] types = Assembly
    .Load("FFM.Express.Core")//如果 .dll报错,可以换成 xxx.exe 有些生成的是exe 
    .GetTypes().Where(it => it.FullName.StartsWith("FFM.Express.Core.Dal.Model."))//命名空间过滤,当然你也可以写其他条件过滤
    .ToArray();//断点调试一下是不是需要的Type,不是需要的在进行过滤

//建库

DbScoped.Sugar.DbMaintenance.CreateDatabase();
DbScoped.Sugar.CodeFirst.InitTables(types);


现有表结构

CREATE TABLE tb_com_order_fulfillment_options (
t_ShipperId                                VARCHAR (255),
t_FulfillmentOrderType                     VARCHAR (255),
t_BackFlushService                         VARCHAR (255),
t_FulfillmentProcess                       VARCHAR (255),
t_DirectFulfillment                        BIT           NOT NULL,
t_AutoBackflush                            BIT           NOT NULL,
t_OrderNoPrefix                            VARCHAR (255),
t_ScanByItemNo                             BIT           NOT NULL,
t_ScanByUpcCode                            BIT           NOT NULL,
t_ScanByEanCode                            BIT           NOT NULL,
t_ScanByJanCode                            BIT           NOT NULL,
t_ScanLocation                             BIT           NOT NULL,
t_ScanByOriginalOrderNo                    BIT           NOT NULL,
t_EnterQuantity                            BIT           NOT NULL,
t_VerifyLotNo                              BIT           NOT NULL,
t_ScanLotNo                                BIT           NOT NULL,
t_VerifySerialNo                           BIT           NOT NULL,
t_ScanComponentLocation                    BIT           NOT NULL,
t_EnterComponentQuantity                   BIT           NOT NULL,
t_VerifyComponentSerialNo                  BIT           NOT NULL,
t_EnterWeight                              BIT           NOT NULL,
t_PickupNeeded                             BIT           NOT NULL,
t_EnterItemQuantityForPackageSummary       BIT           NOT NULL,
t_EnterPackageSummary                      BIT           NOT NULL,
t_EnterMasterTrackingNumber                BIT           NOT NULL,
t_MatchMasterTrackingNumber                BIT           NOT NULL,
t_AllowPartialPicking                      BIT           NOT NULL,
t_AllowPartialComplete                     BIT           NOT NULL,
t_EnterLineReference                       BIT           NOT NULL,
t_RepeatingItemNo                          BIT           NOT NULL,
t_RepeatingLotNo                           BIT           NOT NULL,
t_RepeatingLocation                        BIT           NOT NULL,
t_RepeatingQuantity                        BIT           NOT NULL,
t_ScanPalletID                             BIT           NOT NULL,
t_UpdateShipDate                           BIT           NOT NULL,
t_OrderCompletionService                   VARCHAR (255),
t_OrderLineCompletionService               VARCHAR (255),
t_OrderLineDetailCompletionService         VARCHAR (255),
t_FulfillmentInspectorService              VARCHAR (255),
t_ItemIsUpperCase                          BIT           NOT NULL,
t_LocationIsUpperCase                      BIT           NOT NULL,
t_SerialNumberIsUpperCase                  BIT           NOT NULL,
t_DisplayScannedSummaryByItem              BIT           NOT NULL,
t_IsGreedyMatch                            BIT           NOT NULL,
t_BulkyScanByLineBatchNo                   BIT           NOT NULL,
t_CloseGroupOrderAfterConfirmed            BIT           NOT NULL,
t_UpdateSubOrderLinesWhenConfirmGroupOrder BIT           NOT NULL,
t_UpdateSubOrderStatusWhenConirmGroupOrder BIT           NOT NULL,
t_SubOrderStatusWhenConfirmGroupOrder      INTEGER       NOT NULL,
t_MobileAppLabel                           VARCHAR (255),
t_MobileOrderLifetime                      INTEGER       NOT NULL,
t_MobileScanLabelConfig                    VARCHAR (255),
t_MobileIcon                               VARCHAR (255),
t_AlternateOrderLookupQuery                VARCHAR (255),
t_FilterCompletedLinesToMobile             BIT           NOT NULL,
PRIMARY KEY (
t_ShipperId,
t_FulfillmentOrderType
)
);


实体代码

[SugarTable("tb_com_order_fulfillment_options")]
	public partial class OrderFulfillmentOptions 
	{
		#region Columns
		[SugarColumn(ColumnName ="t_ShipperId",IsPrimaryKey = true,IsNullable = true)] 
		public string ShipperId { get; set; }

		[SugarColumn(ColumnName ="t_FulfillmentOrderType",IsPrimaryKey = true,IsNullable = true)] 
		public string FulfillmentOrderType { get; set; }

		[SugarColumn(ColumnName ="t_BackFlushService",IsNullable = true)] 
		public string BackFlushService { get; set; }

		[SugarColumn(ColumnName ="t_FulfillmentProcess",IsNullable = true)] 
		public string FulfillmentProcess { get; set; }

		[SugarColumn(ColumnName ="t_DirectFulfillment")] 
		public bool DirectFulfillment { get; set; }

		[SugarColumn(ColumnName ="t_AutoBackflush")] 
		public bool AutoBackflush { get; set; }

		[SugarColumn(ColumnName ="t_OrderNoPrefix",IsNullable = true)] 
		public string OrderNoPrefix { get; set; }

		[SugarColumn(ColumnName ="t_ScanByItemNo")] 
		public bool ScanByItemNo { get; set; }

		[SugarColumn(ColumnName ="t_ScanByUpcCode")] 
		public bool ScanByUpcCode { get; set; }

		[SugarColumn(ColumnName ="t_ScanByEanCode")] 
		public bool ScanByEanCode { get; set; }

		[SugarColumn(ColumnName ="t_ScanByJanCode")] 
		public bool ScanByJanCode { get; set; }

		[SugarColumn(ColumnName ="t_ScanLocation")] 
		public bool ScanLocation { get; set; }

		[SugarColumn(ColumnName ="t_ScanByOriginalOrderNo")] 
		public bool ScanByOriginalOrderNo { get; set; }

		[SugarColumn(ColumnName ="t_EnterQuantity")] 
		public bool EnterQuantity { get; set; }

		[SugarColumn(ColumnName ="t_VerifyLotNo")] 
		public bool VerifyLotNo { get; set; }

		[SugarColumn(ColumnName ="t_ScanLotNo")] 
		public bool ScanLotNo { get; set; }

		[SugarColumn(ColumnName ="t_VerifySerialNo")] 
		public bool VerifySerialNo { get; set; }

		[SugarColumn(ColumnName ="t_ScanComponentLocation")] 
		public bool ScanComponentLocation { get; set; }

		[SugarColumn(ColumnName ="t_EnterComponentQuantity")] 
		public bool EnterComponentQuantity { get; set; }

		[SugarColumn(ColumnName ="t_VerifyComponentSerialNo")] 
		public bool VerifyComponentSerialNo { get; set; }

		[SugarColumn(ColumnName ="t_EnterWeight")] 
		public bool EnterWeight { get; set; }

		[SugarColumn(ColumnName ="t_PickupNeeded")] 
		public bool PickupNeeded { get; set; }

		[SugarColumn(ColumnName ="t_EnterItemQuantityForPackageSummary")] 
		public bool EnterItemQuantityForPackageSummary { get; set; }

		[SugarColumn(ColumnName ="t_EnterPackageSummary")] 
		public bool EnterPackageSummary { get; set; }

		[SugarColumn(ColumnName ="t_EnterMasterTrackingNumber")] 
		public bool EnterMasterTrackingNumber { get; set; }

		[SugarColumn(ColumnName ="t_MatchMasterTrackingNumber")] 
		public bool MatchMasterTrackingNumber { get; set; }

		[SugarColumn(ColumnName ="t_AllowPartialPicking")] 
		public bool AllowPartialPicking { get; set; }

		[SugarColumn(ColumnName ="t_AllowPartialComplete")] 
		public bool AllowPartialComplete { get; set; }

		[SugarColumn(ColumnName ="t_EnterLineReference")] 
		public bool EnterLineReference { get; set; }

		[SugarColumn(ColumnName ="t_RepeatingItemNo")] 
		public bool RepeatingItemNo { get; set; }

		[SugarColumn(ColumnName ="t_RepeatingLotNo")] 
		public bool RepeatingLotNo { get; set; }

		[SugarColumn(ColumnName ="t_RepeatingLocation")] 
		public bool RepeatingLocation { get; set; }

		[SugarColumn(ColumnName ="t_RepeatingQuantity")] 
		public bool RepeatingQuantity { get; set; }

		[SugarColumn(ColumnName ="t_ScanPalletID")] 
		public bool ScanPalletID { get; set; }

		[SugarColumn(ColumnName ="t_UpdateShipDate")] 
		public bool UpdateShipDate { get; set; }

		[SugarColumn(ColumnName ="t_OrderCompletionService",IsNullable = true)] 
		public string OrderCompletionService { get; set; }

		[SugarColumn(ColumnName ="t_OrderLineCompletionService",IsNullable = true)] 
		public string OrderLineCompletionService { get; set; }

		[SugarColumn(ColumnName ="t_OrderLineDetailCompletionService",IsNullable = true)] 
		public string OrderLineDetailCompletionService { get; set; }

		[SugarColumn(ColumnName ="t_FulfillmentInspectorService",IsNullable = true)] 
		public string FulfillmentInspectorService { get; set; }

		[SugarColumn(ColumnName ="t_ItemIsUpperCase")] 
		public bool ItemIsUpperCase { get; set; }

		[SugarColumn(ColumnName ="t_LocationIsUpperCase")] 
		public bool LocationIsUpperCase { get; set; }

		[SugarColumn(ColumnName ="t_SerialNumberIsUpperCase")] 
		public bool SerialNumberIsUpperCase { get; set; }

		[SugarColumn(ColumnName ="t_DisplayScannedSummaryByItem")] 
		public bool DisplayScannedSummaryByItem { get; set; }

		[SugarColumn(ColumnName ="t_IsGreedyMatch")] 
		public bool IsGreedyMatch { get; set; }

		[SugarColumn(ColumnName ="t_BulkyScanByLineBatchNo")] 
		public bool BulkyScanByLineBatchNo { get; set; }

		[SugarColumn(ColumnName ="t_CloseGroupOrderAfterConfirmed")] 
		public bool CloseGroupOrderAfterConfirmed { get; set; }

		[SugarColumn(ColumnName ="t_UpdateSubOrderLinesWhenConfirmGroupOrder")] 
		public bool UpdateSubOrderLinesWhenConfirmGroupOrder { get; set; }

		[SugarColumn(ColumnName ="t_UpdateSubOrderStatusWhenConirmGroupOrder")] 
		public bool UpdateSubOrderStatusWhenConirmGroupOrder { get; set; }

		[SugarColumn(ColumnName ="t_SubOrderStatusWhenConfirmGroupOrder")] 
		public int SubOrderStatusWhenConfirmGroupOrder { get; set; }

		[SugarColumn(ColumnName ="t_MobileAppLabel",IsNullable = true)] 
		public string MobileAppLabel { get; set; }

		[SugarColumn(ColumnName ="t_MobileOrderLifetime")] 
		public int MobileOrderLifetime { get; set; }

		[SugarColumn(ColumnName ="t_MobileScanLabelConfig",IsNullable = true)] 
		public string MobileScanLabelConfig { get; set; }

		[SugarColumn(ColumnName ="t_MobileIcon",IsNullable = true)] 
		public string MobileIcon { get; set; }

		[SugarColumn(ColumnName ="t_AlternateOrderLookupQuery",IsNullable = true)] 
		public string AlternateOrderLookupQuery { get; set; }

		[SugarColumn(ColumnName ="t_FilterCompletedLinesToMobile")] 
		public bool FilterCompletedLinesToMobile { get; set; }

		[SugarColumn(ColumnName ="t_ForceCloseTriggerMessage",IsNullable = true)] 
		public string ForceCloseTriggerMessage { get; set; }

		#endregion
	}


热忱回答17

  • fate sta fate sta VIP0
    2023/9/14

    不会的,除非AOP禁用了 更新表

    0 回复
  • fate sta fate sta VIP0
    2023/9/14

    或者你的工具没有刷新

    0 回复
  • fate sta fate sta VIP0
    2023/9/14

     建议独立测试


    DbScoped.Sugar.CodeFirst.InitTables(typeof(OrderFulfillmentOptions));

    0 回复
  • Jimex Jimex VIP0
    2023/9/14

    @fate sta:我在后续的操作中遇到了这个错误: 

    System.Data.SQLite.SQLiteException: 'SQL logic error

    no such column: t_ForceCloseTriggerMessage'


    0 回复
  • Jimex Jimex VIP0
    2023/9/14

    @fate sta:好,我测试一下看

    0 回复
  • Jimex Jimex VIP0
    2023/9/14

    如果我把数据库文件删除掉,再运行,生成的表里面是有新的字段的,让我单独做一个测试

    0 回复
  • Jimex Jimex VIP0
    2023/9/14

    @fate sta:我重现了这个问题,请参考一下添附项目

    TestSqlSugar_CodeFirst.zip


    0 回复
  • fate sta fate sta VIP0
    2023/9/14

    image.png

    0 回复
  • fate sta fate sta VIP0
    2023/9/14

    启动后点测试是成功的

    0 回复
  • Jimex Jimex VIP0
    2023/9/14

    你点击一下Drop Column, 然后再运行试试

    0 回复
  • fate sta fate sta VIP0
    2023/9/14

    找到原因了,你这个是双主键,sqlite双主键不支持修改

    0 回复
  • fate sta fate sta VIP0
    2023/9/14

    目前是这样的

    0 回复
  • Jimex Jimex VIP0
    2023/9/14

    原来是这样子,我明白了,谢谢

    0 回复
  • Jimex Jimex VIP0
    2023/9/14

    现在如果我想把实体改回去单主键好像不允许了,会出现


    image.png

     [SugarColumn(ColumnName = "t_Id", IsPrimaryKey = true, IsIdentity = true)]
     public int Id { get; set; }
    
    
     [SugarColumn(ColumnName = "t_ShipperId")]
     public string ShipperId { get; set; }
    
     [SugarColumn(ColumnName = "t_FulfillmentOrderType")]


    0 回复
  • fate sta fate sta VIP0
    2023/9/14

    @Jimex:双主键不能改

    0 回复
  • fate sta fate sta VIP0
    2023/9/14

    sqlite没有修改列的操作

    0 回复
  • Jimex Jimex VIP0
    2023/9/15

    明白,谢谢

    0 回复