DatabaseGeneratedOption ne fonctionne pas dans Dapper.FastCRUD

.net-4.5 c# dapper dapper-fastcrud

Question

Je configure mon mapping comme ceci:

[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
    ...
}

Mon test unitaire est configuré comme ceci:

        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);

La dernière ligne se bloquerait avec l'erreur: La colonne "OPPORTUNITY_VALUE" ne peut pas être modifiée car il s'agit soit d'une colonne calculée, soit du résultat d'un opérateur UNION.

Qu'est-ce que je fais mal? Je ne modifie pas la valeur de cette colonne du tout. Voici la pile d'erreur complète:

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.

Instruction CREATE pour la table Opportunité:

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]

Réponse acceptée

Dapper.FastCrud version actuelle de Dapper.FastCrud ne prend pas correctement en charge les colonnes calculées. S'il vous plaît regarder pour une résolution à ce billet .

MISE À JOUR: Le problème a été corrigé dans 2.3.0.




Sous licence: CC-BY-SA with attribution
Non affilié à Stack Overflow
Est-ce KB légal? Oui, apprenez pourquoi
Sous licence: CC-BY-SA with attribution
Non affilié à Stack Overflow
Est-ce KB légal? Oui, apprenez pourquoi