DatabaseGeneratedOption没有';我不在Dapper.FastCRUD工作

本文关键字:Dapper FastCRUD 工作 没有 DatabaseGeneratedOption | 更新日期: 2023-09-27 18:28:29

我这样设置映射:

[Table("Opportunity")]
public partial class Opportunity
{
    // Other columns
    ...
    [Key]
    [Column(Order = 1)]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public long OPPORTUNITY_ID { get; set; }
    [DatabaseGenerated(DatabaseGeneratedOption.Computed)]
    public long? OPPORTUNITY_VALUE { get; set; }
    // Other columns
    ...
}

我的单元测试设置如下:

        Opportunity opp = new Opportunity
            {
                DATE_CREATED_UTC = DateTime.UtcNow,
                OWNER_USER_ID = TestConstants.USER_ID,
                OPPORTUNITY_NAME = "unitTest-Opportunity",
                PROBABILITY = 50,
                BID_CURRENCY = "USD",
                BID_AMOUNT = 6000,
                BID_TYPE = "Fixed Bid",
                OPPORTUNITY_STATE = "OPEN",
                OPPORTUNITY_DETAILS = "UNIT TEST OPPORUNITY DETAIL",
                VISIBLE_TO = "EVERYONE"
            };
        OppService.Add(opp);
        Opportunity fromDB = OppService.Get(opp.OPPORTUNITY_ID);
        OppService.Update(fromDB);

最后一行将崩溃并返回错误:列"OPPORTUNITY_VALUE"无法修改,因为它是计算列或是UNION运算符的结果

我做错了什么?我根本不修改该列的值。以下是完整的错误堆栈:

Result StackTrace:  
at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
   at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
   at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
   at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
   at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString)
   at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite, SqlDataReader ds, Boolean describeParameterEncryptionRequest)
   at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean asyncWrite)
   at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource`1 completion, String methodName, Boolean sendToPipe, Int32 timeout, Boolean asyncWrite)
   at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
   at Dapper.SqlMapper.ExecuteCommand(IDbConnection cnn, CommandDefinition& command, Action`2 paramReader) in D:'Dev'dapper-dot-net'Dapper NET40'SqlMapper.cs:line 3397
   at Dapper.SqlMapper.ExecuteImpl(IDbConnection cnn, CommandDefinition& command) in D:'Dev'dapper-dot-net'Dapper NET40'SqlMapper.cs:line 1346
   at Dapper.FastCrud.SqlStatements.GenericSqlStatements`1.UpdateById(IDbConnection connection, TEntity keyEntity, ISqlStatementOptionsGetter statementOptions)
   at Dapper.FastCrud.DapperExtensions.Update[TEntity](IDbConnection connection, TEntity entityToUpdate, Action`1 statementOptions)
   at MyProject.Data.Repository`1.Update(T entity) in C:'MyFolder'Core'MyProject.Data'Repository.cs:line 80
   at MyProject.Service.Base.ModelService`1.Update(T entity) in C:'MyFolder'Core'MyProject.Service'Base'ModelService.cs:line 38
   at MyProject.Service.Base.InstanceEntityService`2.Update(T entity) in C:'MyFolder'Core'MyProject.Service'Base'InstanceEntityService.cs:line 72
   at MyProject.Service.OpportunityService.Update(Opportunity entity) in C:'MyFolder'Core'MyProject.Service'OpportunityService.cs:line 21
   at MyProject.Test.Service.OpportunityServiceTests.OpportunitiesCRUDTest() in C:'MyFolder'Test'Core'Service'OpportunityServiceTest.cs:line 22
Result Message: System.Data.SqlClient.SqlException : The column "OPPORTUNITY_VALUE" cannot be modified because it is either a computed column or is the result of a UNION operator.

表Opportunity的CREATE语句:

CREATE TABLE [dbo].[Opportunity](
    [OPPORTUNITY_ID] [bigint] IDENTITY(1,1) NOT NULL,
    // Other columns
    [BID_CURRENCY] [nvarchar](20) NULL,
    [BID_AMOUNT] [bigint] NULL,
    [BID_TYPE] [varchar](20) NULL,
    [BID_DURATION] [int] NULL,
    [OPPORTUNITY_VALUE]  AS (case when [BID_TYPE]='Fixed Bid' AND [BID_AMOUNT] IS NOT NULL then [BID_AMOUNT] when [BID_TYPE]='Fixed Bid' AND [BID_AMOUNT] IS NULL then NULL when [BID_AMOUNT] IS NOT NULL AND [BID_DURATION] IS NOT NULL then [BID_AMOUNT]*[BID_DURATION] when [BID_DURATION] IS NULL then [BID_AMOUNT]  end) PERSISTED,
    [SEQUENCE_ID] [int] NULL,
    [DELETED] [bit] NOT NULL,
    [DELETED_DATE_UTC] [datetime] NULL,
    [DELETED_USER_ID] [int] NULL,
 CONSTRAINT [PK_OPPORTUNITY] PRIMARY KEY CLUSTERED 
(
    [INSTANCE_ID] ASC,
    [OPPORTUNITY_ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY]
) ON [PRIMARY]

DatabaseGeneratedOption没有';我不在Dapper.FastCRUD工作

发现当前版本的Dapper.FastCrud不正确支持计算列。请留意这张票的解决方案。

更新:问题已在2.3.0中修复。