gpt4 book ai didi

c# - DatabaseGeneratedOption 在 Dapper.FastCRUD 中不起作用

转载 作者:行者123 更新时间:2023-11-30 12:24:14 25 4
gpt4 key购买 nike

我这样设置我的映射:

[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]

最佳答案

原来 Dapper.FastCrud 的当前版本没有正确支持计算列。请注意解决 this ticket .

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

关于c# - DatabaseGeneratedOption 在 Dapper.FastCRUD 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34537729/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com