Automatic serialization with Linq to Sql

Is there a way to store data in a table format:

Inside the SettingsModel column, which is defined in Linq-to-Sql as follows:

And also with the DataContext option:

With the SettingsModel class defined like this:

 namespace ElQueue.DataClasses { [DataContract] public sealed class SettingsModel { [DataMember(IsRequired = true)] public int[] VideoMediaData { get; set; } } } 

Here?...

 using (SomeDataContext dataContext = new SomeDataContext()) { SettingsModel m = new SettingsModel(); m.VideoMediaData = new int[] { 1, 2, 3 }; dataContext.MainTableSettings.InsertOnSubmit(new MainTableSetting() { SettingsModel = m }); dataContext.SubmitChanges(); } using (SomeDataContext dataContext = new SomeDataContext()) { var r = dataContext.MainTableSettings.Single(); } 

You see, the above code does not work correctly, it throws an exception that says that it cannot convert the string to MainTableSetting , which means that either it cannot save all the serialized data, or this plus cannot deserialize it back.

I need some tips on how to tell this Linq-to-Sql to actually execute serialization (and vice versa) when I access the database.

+7
source share
1 answer

Although you can map XElement and XDocument to SQL Server, as shown in the type matching time behavior matrix, the System.Data.Linq.DataContext.CreateDatabase method has a default default SQL Server type mapping for these types.

If the class implements Parse () and ToString () , you can map the object to any type of SQL text (CHAR, NCHAR, VARCHAR, NVARCHAR, TEXT, NTEXT, XML). The object is stored in the database by sending the value returned by ToString () to the column of the associated database. The object is restored by calling Parse () on the string returned by the database.

http://msdn.microsoft.com/en-us/library/bb386947.aspx#TextMapping

 public sealed class SettingsModel { // ... public static SettingsModel Parse(string source) { SettingsModel res; // Deserialise the object return res; } } 
+4
source

All Articles