Quantcast
Channel: Telerik Forums RSS
Viewing all articles
Browse latest Browse all 78072

Filtering while using AdoTypeConverters

$
0
0

Are there any admins that can chime in on this?

I'm running into another issue where an artificial property with a converter on it is causing issues in concurrency.

A nullable datetime column is mapped to an artificial nullable datetime property with a converter that is supposed to convert nulls and dates before 1899 in the database to nulls in the application.

I think this is causing an issue where when I try to update an artificial datetime property, the optimistic concurrency verification is attempting to find the row by using 'null', when in the database it's '1899-1-1'. So the row is not found, exception is thrown.

Some code to aid in troubleshooting.

mappingConfiguration.HasArtificialPrimitiveProperty(udf.FieldName, typeof(Nullable<DateTime>)).HasFieldName(fieldName).ToColumn(udf.FieldName).WithConverter<DateTimeConverter>();

publicclassDateTimeConverter : AdoTypeConverter
{
    publicoverrideType DefaultType
    {
        get{ returntypeof(DateTime?); }
    }
 
    publicoverrideobjectRead(refDataHolder holder)
    {
        //check if the value is dbnull
        boolisNull = holder.Reader.IsDBNull(holder.Position);
 
        //set wheather or not there is a value
        holder.NoValue = isNull;
 
        //if the value is null, then we need to return default values
        DateTime? value = null;
        if(!isNull)
        {
            //the value is not null, so here we want to pull the value out, and convert it
            value = holder.Reader.GetValue(holder.Position) asDateTime?;
 
            //if the value is nullabel, or needs to be boxed, we set the ObjectValue Property of the DataHolder
            if(value.RemoveTime() <= InteumDate.EMPTY_DATE)
                value = null;
        }
        holder.ObjectValue = value;
 
        //now we return our value
        returnvalue;
    }
 
    publicoverridevoidWrite(refDataHolder holder)
    {
        //set the db type
        holder.Parameter.DbType = System.Data.DbType.DateTime;
 
        //if there is no value we could specify what to set the db field to
        if(holder.NoValue)
        {
            holder.Parameter.Value = InteumDate.EMPTY_DATE;
        }
        else
        {
            holder.Parameter.Value = holder.ObjectValue == null? InteumDate.EMPTY_DATE : holder.ObjectValue asDateTime?;
        }
    }
 
    publicoverrideboolCreateLiteralSql(refDataHolder holder)
    {
        //If there is no value, then we just want to query against null.
        if(holder.NoValue)
        {
            holder.StringValue = "NULL";
 
            //returning false indicates that no quotes are required. We want NULL instead of 'NULL'
            returnfalse;
        }
        else
        {
            holder.DateTimeValue = (holder.ObjectValue asDateTime?).GetValueOrDefault(InteumDate.EMPTY_DATE);
            // return true indicates that quotes are needed around the value
            returntrue;
        }
    }
}

Exception message:

"Row not found: GenericOID@50ac1abb TECHNOL PRIMARYKEY=4210
UPDATE [TECHNOL] SET [RUPDATEDD] = ?, [UDFG6EDDT] = ? WHERE [PRIMARYKEY] = ? AND [RUPDATEDD] = ? AND [UDFG6EDDT] is null"

 The value for column UDFG6EDDT in the database is (SQL Server) '1899-1-1'. So natually, there is no row found with the given filter. How is this fixed?


Viewing all articles
Browse latest Browse all 78072

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>