简体   繁体   中英

ServiceStack.OrmLite complex types from string field in SQL Server

We are looking at using ServiceStack.OrmLite.SqlServer 4.0.31 connected to MSSQL 2008R2 to replace our current ORM and are having difficulty in setting up the complex type string serialization. In our old ORM these were manually mapping using auto mapper to string fields using JSON.NET so we already have our JSON in the database in columns which are VARCHAR(MAX).

I have set the string serializer to be:

SqlServerDialect.Provider.StringSerializer = new JsonDataContractSerializer();

The data base type is:

public class Group
{
    [Alias("GroupID")]
    [AutoIncrement]
    [PrimaryKey]
    public int GroupId { get; set; }

    [Alias("Name")]
    public string Name { get; set; }

    [Alias("ShortName")]
    public string ShortName { get; set; }

    [Alias("GroupTypeID")]
    public int GroupTypeId { get; set; }

    [Alias("ContactDetails")]
    public ContactDetails ContactDetails { get; set; }

}

The contact details is:

[DataContract]
[Serializable]
public class ContactDetails
{

    [DataMember]
    public string FirstName { get; set; }

    [DataMember]
    public string LastName { get; set; }

    [DataMember]
    public string CompanyName { get; set; }

    [DataMember]
    public string Title { get; set; }

    [DataMember]
    public string Address { get; set; }
}

The object is created but none of the fields are serialised:

在此输入图像描述

The data in the database is as follows:

{
    "Address" : "2 Doe Street, Dublin 2",
    "Phone" : "01 4684455",
    "PhoneIsVerified" : false,
    "MobileIsVerified" : false,
    "Fax" : "01 4684455",
    "Email" : "john@doe.ie",
    "Website" : "http://www.doe.ie",
    "PropertySectionEmail" : {},
    "IsPrivate" : false
}

The data in the DB looks like:

在此输入图像描述

I saw in github that the default field for storing for these fields was VARBINARY(MAX) so I am not sure if thats why its not working but I checked all the attributes in data annotations and there was not one I could see to specify that it was a complex type. Any ideas why this is not working?

I found the issue in my code in the end. I was using:

SqlServerDialect.Provider.StringSerializer = new JsonDataContractSerializer();
OrmLiteConfig.DialectProvider = SqlServerDialect.Provider;
OrmLiteConnectionFactory(connectionString, new SqlServerOrmLiteDialectProvider());

Where I should have been using:

SqlServerDialect.Provider.StringSerializer = new JsonDataContractSerializer();
OrmLiteConfig.DialectProvider = SqlServerDialect.Provider;
OrmLiteConnectionFactory(connectionString, SqlServerDialect.Provider);

So I was creating a blank dialect provider which did not have the serializer attached.

I'm not sure what you're wrong, but I've just tried your example and it works as expected:

SqlServerDialect.Provider.StringSerializer = new JsonDataContractSerializer();

var db = OpenDbConnection();
db.DropAndCreateTable<Group>();

Will generate the schema below:

DROP TABLE "Group"
CREATE TABLE "Group" 
(
  "GroupID" INTEGER PRIMARY KEY IDENTITY(1,1), 
  "Name" VARCHAR(8000) NULL, 
  "ShortName" VARCHAR(8000) NULL, 
  "GroupTypeID" INTEGER NOT NULL, 
  "ContactDetails" VARCHAR(MAX) NULL 
); 

Which we're now able to insert and select from without issue:

var group = new Group
{
    Name = "Group Name",
    ShortName = "GN",
    GroupTypeId = 1,
    ContactDetails = new ContactDetails {
        Address = "Address",
        CompanyName = "Company",
        FirstName = "First",
        LastName = "Last",
        Title = "Title",
    }
};

db.Save(group);

var result = db.SingleById<Group>(group.GroupId);

result.PrintDump();

That prints out the populated row as expected:

{
    GroupId: 1,
    Name: Group Name,
    ShortName: GN,
    GroupTypeId: 1,
    ContactDetails: 
    {
        FirstName: First,
        LastName: Last,
        CompanyName: Company,
        Title: Title,
        Address: Address
    }
}

And inside SQL Server it's storing JSON as expected:

SQL Server中的JSON

This example is also available as a stand-alone test .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM