sql-server


Is there a way to get a Linq Query to select a timestamp field encoded as a Base64 string?


We have a concurrency check field in our database that is a SQL timestamp.
If I am using a razor view with
#Html.HiddenFor(m => m.DatabaseRowVersion)
it encodes to a Base64 string and binds back to my model in the save action fine.
If I get the field by a Json request with
Json(queryable.Select(f => new { DatabaseRowVersion = f.DatabaseRowVersion }))
then the resulting Json is an 8 field byte array and I cannot get it to bind back to my model in the Save action. Is there some way I can get Linq to Entities to select the field as a Base64 string before it gets serialised?
My solution is a dirty looking JavaScript function to convert 8 field byte arrays to Base64 strings, but this is not pretty and I would rather the original Json request return the field already encoded. Everything I have tried gives me an unsupported Linq to Entities function exception. I don't want to do it in memory, I would like to find some sort of Entities.Functions style of getting it to happen on the SQL server.
Another way I wouldn't mind doing it is if could get jQuery to post the save request in some way that would bind an 8 field byte array to a byte[] back in the model but everything I have tried doesn't work. I have tried all the traditional: true and contentType: "application/json; charset=utf-8" on the post with no success like with other actions that bind an array in an Ajax post successfully. It just doesn't seem to work like with other arrays as the model binder seems to be expecting a Base64 string rather than a byte array for byte[] fields on the model.
I'm not sure if you are still looking for an answer but I've managed to resolve this by converting the byte array into a string and back:
string stringValue = System.Convert.ToBase64String(byteSource);
Byte[] byteValue = System.Convert.FromBase64String(stringSource);
For your case you can create a ViewModel and use AutoMapper to map between your Model (which has the Byte Array TimeStamp) and ViewModel (which would have the String TimeStamp).
Finally you would use your ViewModel to create your view and when updating, you would map your ViewModel back to your Model and update from there. This way your hidden field would be a string and you wouldn't have the issues you are having right now.
Automapper does not have an implicit conversion method between Byte arrays to String so you would have to implement something like this for your converters:
public class ByteArrayTypeConverter : ITypeConverter<string, Byte[]>
{
public Byte[] Convert(ResolutionContext context)
{
string source = (string)context.SourceValue;
if (source == null)
{
source = "";
}
Byte[] returnValue = System.Convert.FromBase64String(source);
return returnValue;
}
}
public class StringTypeConverter : ITypeConverter<Byte[], string>
{
public string Convert(ResolutionContext context)
{
Byte[] source = (Byte[])context.SourceValue;
string returnValue = System.Convert.ToBase64String(source);
return returnValue;
}
}
And your Mapping initializer would look something like this:
Mapper.CreateMap<string, Byte[]>().ConvertUsing(new ByteArrayTypeConverter());
Mapper.CreateMap<Byte[], string>().ConvertUsing(new StringTypeConverter());
Mapper.CreateMap<YourViewModel, YourModel);
Mapper.CreateMap<YourModel, YourViewModel>();
Hope this helps.

Related Links

Bulk Load Data Conversion Error - Can't Find Answer
when i exec select on linux with odbc, hang on '__select_nocancel'
The system cannot find the file specified on Production server
An Error when trying to connect to SQL Server
Informatica powercenter
Temporary table not created from dynamic query execution
How to process the data in the table, which is frequently inserted
Combine two columns and input the result in a different columns using SQL server
SQL Query to accept time difference
How to change my T-SQL query to overwrite a csv file rather than append data to it?
How to join multiple columns with only one foreign key using linq
Deleting millions on records while using log shipping - SQL Server
Restricting decimal places [duplicate]
Powershell T-SQL Update Statement Error in Function
How to close if case in SQL
Set RESTRICTED_USER on Azure SQL database

Categories

HOME
visual-studio-2015
mql4
project-management
angular-cli
is-empty
branch.io
jrebel
java-home
triggers
height
cakephp-2.9
camera-calibration
gatsby
xlsx
cpanel
uisplitviewcontroller
titan
pyyaml
ejbca
prediction
jquery-form-validator
richfaces
explode
yosys
shopping-cart
jenkins-job-dsl
bus-error
hash-collision
spring-data-neo4j
fetch-api
nesc
crt
protobuf-net
svnkit
strophe
entity-system
spring-profiles
fabric-digits
rhino
google-guava-cache
dql
initializer
image-compression
onbackpressed
ical-dotnet
spring-bean
matlab-cvst
rapidweaver
floor
mouseclick-event
crash-reports
etherpad
pdfkit
verbose
mix
git-annex
visible
dojox.charting
cmocka
magento-1.4
android-cursoradapter
affix
registrykey
on-duplicate-key
jai
orientation-changes
sysctl
node-inspector
funq
alpha-transparency
funkload
alertifyjs
logentries
windows-mobile-6
jqmobi
threadx
vmware-server
ienumerator
backbone-relational
xml-libxml
domain-calculus
trailing-slash
attachevent
crocodoc
git-filter-branch
scrollto
subtract
gjs
signals2
law-of-demeter
flexicious
viewdidload
nsindexpath
handwriting
authenticode
testunit
pci-bus
system-codedom-compiler
economics
cots

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App