Search code examples
jsonrestdreamfactory

Dream Factory .NET SDK Post records Unique Integrity Error


In teh .NET SDK your create record method passes a 0 for id on all records as this is unknown until teh response comes back with the id's populated etc

It Seems that the JSON DefaultValueHandling = DefaultValueHandling.Ignore is not working on the freshly minted int '0' id's

An therefore the body has the id:0 and trys inserts the records with id:0 on all and trips a Unique constraint on the inner exception in fiddler


Solution

  • I had a similar problem with DreamFactory

    I added a conditional Property Serialization attribute to the Poco/DTO for the staff record as an example

    internal class StaffRecord {

            public bool ShouldSerializeUid()
            {
                return Uid != 0;
            }
    
            public int Uid { get; set; }
    
            public string FirstName { get; set; }
            public string LastName { get; set; }
            public int Age { get; set; }
            public bool Active { get; set; }
    
            public override string ToString()
            {
                return string.Format("{0}: name = {1} {2}, age = {3}, active = {4}", Uid, FirstName, LastName, Age, Active);
            }
        }
    

    This now works as expected on both serialization/deserialization

    Here is the docs in the JSON.NET docs Conditional Property Serialization

    Cheers :D