0

I am using MVC3, ASP.NET 4.5, C#, EF6.1.1 and SQL Azure and SQL Server 2008 locally.

I am looking for a simple way to store a .NET type to a generic column, so binary is fine, and then retrieve it. When retrieving it I will know how to retrieve it ie as "Object", "String" or "Int32".

To save, I believe I need:

String -> Binary
Int32 -> Binary
Object -> Binary

To retrieve, I need:

Binary -> String via String Retrieve Method.
Binary -> Int32 via Int32 Retrieve Method.
Binary -> Object via Object Retrieve Method.

I have read much on serializing and deserializing, to and from the database, using something like JSON.NET. However I think I can keep things even more simple as this data is temporary and does not need to be human readable. The application as such needs to be able to retrieve it and then convert it to String, Int32 or Object.

Some guidance on this would be appreciated, and a code example would be even better.

Thanks.

EDIT

This is my best starting point, see "Answer" in ADO.NET Object Save Approach. However I do not understand how one would retrieve this data.

Community
  • 1
  • 1
SamJolly
  • 6,347
  • 13
  • 59
  • 125
  • If you are using EF why you're looking into serialization and deserialization? – deeiip Jul 08 '15 at 16:38
  • Thanks for this. So yes I can use an EF approach. What simple EF approach would you recommend? – SamJolly Jul 08 '15 at 16:40
  • SamJolly, I'd love to have a quick chat if you're willing. I seem a great stream of questions about Azure and SQL DB. I'm on the program management team for SQL DB and would love to learn a little more about your Journey if your willing. Guyhay @ microsoft.com – guyhay_MSFT Jul 08 '15 at 21:42

1 Answers1

1

Say you have a non human-readable object. Now you can write a code first class like:

class foo
{
    public string otherObj{get;set;}
    public byte[] nonReadableObj{get;set;}
}

Now put your non human-readable thing in nonReadableObj and EF will do the rest for you.

deeiip
  • 3,319
  • 2
  • 22
  • 33
  • Hmm, simple as that :) Will give it a try. Thanks – SamJolly Jul 08 '15 at 16:51
  • What would the DB column type be : Binary, Varbinary or Varbinary(Max) to match to the EF "Byte[]" type? – SamJolly Jul 08 '15 at 17:17
  • it doesn't matter. Anyway you can see the type by yourself from modeling diagram. https://msdn.microsoft.com/en-us/library/cc716729(v=vs.110).aspx this link list down all – deeiip Jul 08 '15 at 17:25
  • 1
    Ok Thanks, ended up with varbinary. – SamJolly Jul 09 '15 at 08:45
  • Sorry Just been been trying to get this to work. I cannot assign an object to a byte[]. How do you do this? I think I need the binaryformatter? See: http://stackoverflow.com/questions/4865104/convert-any-object-to-a-byte. – SamJolly Jul 09 '15 at 18:55