Entity Framework 5 DbUpdateException: Null value for non-nullable member - c#

I have some data models:
[DataContract(Name = "artist")]
public class artist : IEqualityComparer<artist>
{
[Key]
[XmlIgnore]
[DataMember]
public int ID { get; set; }
[DataMember]
[XmlElement(ElementName = "name")]
public string name { get; set; }
[DataMember]
[XmlElement(ElementName = "mbid", IsNullable = true)]
public string mbid { get; set; }
[DataMember]
[XmlElement(ElementName = "url")]
public string url { get; set; }
[XmlElement(ElementName = "image", IsNullable = true)]
public List<string> image { get; set; }
[DataMember(IsRequired=false)]
[XmlElement(ElementName = "stats", IsNullable = true)]
public stats stats { get; set; }
public double? match { get; set; }
public List<tag> tags { get; set; }
[XmlElement(ElementName = "similar")]
[DataMember(Name = "similar")]
public List<artist> similar { get; set; }
[DataMember]
[XmlElement(ElementName = "bio", IsNullable = true)]
public wiki bio { get; set; }
public bool Equals(artist x, artist y)
{
return x.name == y.name;
}
public int GetHashCode(artist obj)
{
return obj.name.GetHashCode();
}
}
and a complex type:
[DataContract]
[ComplexType]
[XmlRoot(ElementName = "streamable", IsNullable = true)]
public class stats
{
[DataMember(IsRequired = false)]
public int listeners { get; set; }
[DataMember(IsRequired = false)]
public int playcount { get; set; }
}
and database inclusion:
[Table("CachedArtistInfo")]
public class MusicArtists
{
[Key]
public string artistName { get; set; }
public artist artistInfo { get; set; }
private DateTime _added = default(DateTime);
[DataMember(IsRequired = true)]
[Timestamp]
public DateTime added
{
get
{
return (_added == default(DateTime)) ? DateTime.Now : _added;
}
set { _added = value; }
}
}
Final step:
foreach (artist a in id)
{
df.CachedArtists.Add(new MusicArtists() { artistName = a.name, artistInfo = a });
df.SaveChanges();
}
ERROR:
ExceptionType
"System.Data.Entity.Infrastructure.DbUpdateException"
"Null value for non-nullable member. Member: 'stats'."
a variable is fully filled and object stats in it.
what's wrong?

Complex types cannot be null - so you have to create an instance of the class before saving. This article helped me solve the same problem.
http://weblogs.asp.net/manavi/archive/2011/03/28/associations-in-ef-4-1-code-first-part-2-complex-types.aspx

Related

Can't deserialize xml properly

I have the following string to deserialize:
<result>
<error>
<errorcode>0</errorcode>
<errorge>წარმატებით</errorge>
<errorru>Удачно</errorru>
<erroren>successfully</erroren>
<line>89</line>
</error>
<amount>
<gel>1</gel>
</amount>
<user>01001</user>
<service>MyService</service> // here
<data>
<nickname>popcorn2</nickname>
<identification_name>identified</identification_name>
<wallet_code>5554654</wallet_code>
<NationalRate>1</NationalRate>
<RATE>1</RATE>
<GENERATED_AMOUNT>1</GENERATED_AMOUNT>
<CURRENCY>GEL</CURRENCY>
</data>
<accoutant>
<agentBenefit>0</agentBenefit>
<agentCommission>0.44</agentCommission>
<clientCommission>0</clientCommission>
</accoutant>
<service>
<min_amount>0.49</min_amount>
<max_amount>1500.00</max_amount>
<currency>GEL</currency>
</service>
<avance>-134206.1500</avance>
<operation_status>0</operation_status>
</result>
as you noticed it has two tags named "service" and they have different contents
here is the class that I've been using
[XmlRoot(ElementName = "error")]
public class ErrorInfo
{
[XmlElement(ElementName = "errorcode")]
public string Errorcode { get; set; }
[XmlElement(ElementName = "errorge")]
public string Errorge { get; set; }
[XmlElement(ElementName = "errorru")]
public string Errorru { get; set; }
[XmlElement(ElementName = "erroren")]
public string Erroren { get; set; }
[XmlElement(ElementName = "line")]
public string Line { get; set; }
}
[XmlRoot(ElementName = "amount")]
public class Amount
{
[XmlElement(ElementName = "gel")]
public string Gel { get; set; }
}
[XmlRoot(ElementName = "data")]
public class Data
{
[XmlElement(ElementName = "nickname")]
public string Nickname { get; set; }
[XmlElement(ElementName = "identification_name")]
public string Identification_name { get; set; }
[XmlElement(ElementName = "wallet_code")]
public string Wallet_code { get; set; }
[XmlElement(ElementName = "NationalRate")]
public string NationalRate { get; set; }
[XmlElement(ElementName = "RATE")]
public string RATE { get; set; }
[XmlElement(ElementName = "GENERATED_AMOUNT")]
public string GENERATED_AMOUNT { get; set; }
[XmlElement(ElementName = "CURRENCY")]
public string CURRENCY { get; set; }
}
[XmlRoot(ElementName = "accoutant")]
public class Accoutant
{
[XmlElement(ElementName = "agentBenefit")]
public string AgentBenefit { get; set; }
[XmlElement(ElementName = "agentCommission")]
public string AgentCommission { get; set; }
[XmlElement(ElementName = "clientCommission")]
public string ClientCommission { get; set; }
}
[XmlRoot(ElementName = "service")]
public class Service
{
[XmlElement(ElementName = "min_amount")]
public string Min_amount { get; set; }
[XmlElement(ElementName = "max_amount")]
public string Max_amount { get; set; }
[XmlElement(ElementName = "currency")]
public string Currency { get; set; }
}
[XmlRoot(ElementName = "result")]
public class Result
{
[XmlElement(ElementName = "error")]
public ErrorInfo Error { get; set; }
[XmlElement(ElementName = "amount")]
public Amount Amount { get; set; }
[XmlElement(ElementName = "user")]
public string User { get; set; }
[XmlElement(ElementName = "data")]
public Data Data { get; set; }
[XmlElement(ElementName = "accoutant")]
public Accoutant Accoutant { get; set; }
[XmlElement(ElementName = "service")]
public Service[] Service { get; set; }
[XmlElement(ElementName = "avance")]
public string Avance { get; set; }
[XmlElement(ElementName = "operation_status")]
public string Operation_status { get; set; }
}
I tried to handle service tags as array but it could not work. I was able to get values from the last tag , but not from the first one. both tags are neccessary so i am wondering if there is a way to get values from both tags ?
Visual Studio has this really great feature where you can put your XML in the clipboard and then do Edit -> Paste special -> Paste XML as classes.
If you do that with your XML, it generates the following class for the service tag:
/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class resultService
{
private decimal min_amountField;
private decimal max_amountField;
private string currencyField;
private string[] textField;
/// <remarks/>
public decimal min_amount
{
get
{
return this.min_amountField;
}
set
{
this.min_amountField = value;
}
}
/// <remarks/>
public decimal max_amount
{
get
{
return this.max_amountField;
}
set
{
this.max_amountField = value;
}
}
/// <remarks/>
public string currency
{
get
{
return this.currencyField;
}
set
{
this.currencyField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlTextAttribute()]
public string[] Text
{
get
{
return this.textField;
}
set
{
this.textField = value;
}
}
}
If you want to do it manually, the secret seems to be to add a field with the XmlTextAttribute attribute.
You can not have two different elements with the same name. It won't know which tag belongs where, you have to alter the name to "MyServiceName" or something similar.
Change your classes as follows:
[XmlRoot(ElementName = "result")]
public class Result
{
// no need XmlElement attribute
public ServiceComplex ServiceComplex { get; set; }
// no need XmlElement attribute
public string ServiceSimple { get; set; }
// other properties
}
// no need XmlRoot attribute
public class ServiceComplex
{
[XmlElement(ElementName = "min_amount")]
public string Min_amount { get; set; }
[XmlElement(ElementName = "max_amount")]
public string Max_amount { get; set; }
[XmlElement(ElementName = "currency")]
public string Currency { get; set; }
}
Instead of ServiceComplex and ServiceSimple, choose the names that suit you.
Subscribe XmlSerializer to UnknownElement event:
var xs = new XmlSerializer(typeof(Result));
xs.UnknownElement += Xs_UnknownElement;
private static void Xs_UnknownElement(object sender, XmlElementEventArgs e)
{
if (e.Element.Name == "service")
{
var result = (Result)e.ObjectBeingDeserialized;
if (e.Element.ChildNodes.Count == 1)
{
result.ServiceSimple = e.Element.InnerText;
}
else
{
result.ServiceComplex = new ServiceComplex
{
Min_amount = e.Element.SelectSingleNode("min_amount").InnerText,
Max_amount = e.Element.SelectSingleNode("max_amount").InnerText,
Currency = e.Element.SelectSingleNode("currency").InnerText
};
}
}
}
In the event handler, we manually populate the properties of our class from xml.

Deserialize with Json.Net fails on some fields

I try to deserialze Json into a c# object using Json.Net.
c# class:
public class Evaluation
{
[DataMember(Name = "id")]
public int Id { get; set; }
[DataMember(Name = "avg")]
public string Average { get; set; }
[DataMember(Name = "median")]
public string Median { get; set; }
[DataMember(Name = "teacherID")]
public int TeacherId { get; set; }
[DataMember(Name = "evalType")]
public int EvaluationType { get; set; }
}
Unit Test:
[Test]
[TestCase()]
public void TestParsing()
{
string json = "{" +
"\"id\": 439476," +
"\"avg\": \"69\"," +
"\"median\": \"75\"," +
"\"teacherID\": 1," +
"\"evalType\": 1" +
"}";
Evaluation evaluation = JsonConvert.DeserializeObject<Evaluation>(json);
Assert.IsTrue(evaluation.Average.Equals("69"));
Assert.IsTrue(evaluation.EvaluationType == 1);
}
This fails because evaluation.Average is null and evaluation.EvaluationType is 0. The other fields are parsed correctly.
Why is this failing?
Setting the DataContract attribute on your class declaration should fix the problem:
[DataContract]
public class Evaluation
{
[DataMember(Name = "id")]
public int Id { get; set; }
[DataMember(Name = "avg")]
public string Average { get; set; }
[DataMember(Name = "median")]
public string Median { get; set; }
[DataMember(Name = "teacherID")]
public int TeacherId { get; set; }
[DataMember(Name = "evalType")]
public int EvaluationType { get; set; }
}

InvalidOperationException Error Reflecting Class

After read multiples posts related on this error and not found an solution to my problem, I explain it here.
I use XmlSerializer to serialize simple classes.
Here's my code:
private void btnGenerateXml_Click(object sender, RoutedEventArgs e)
{
Orchard orchard = new Orchard
{
Recipe = new Recipe
{
Name = "Generated by JooWeb.Tools",
Author = "admin",
ExportUtc = DateTime.UtcNow
},
MyDatas = new MyDatas
{
//Test = "test"
TrendDatas = new TrendDatas
{
Id = null,
Status = "Published",
TrendDatasPart = new TrendDatasPart
{
IdSource = 0,
PostalCode = "1000",
Locality = "Test5",
Surface = (decimal)0.00,
Price = (decimal)0.00,
Type = "",
InsertDateIndicator = "",
UpdateDateIndicator = "",
GetFromDate = DateTime.Now,
UpdatedDate = new DateTime(1900, 1, 1)
},
CommonPart = new CommonPart
{
Owner = "/User.UserName=admin",
CreatedUtc = DateTime.UtcNow,
PublishedUtc = DateTime.UtcNow,
ModifiedUtc = DateTime.UtcNow
}
}
}
};
XmlSerializer orchardXmlSerializer = new XmlSerializer(typeof(Orchard));
var path = #"C:\Temp\orchardFileImport_" + string.Format("{0:yyyyMMdd}", DateTime.Today) + ".xml";
if (File.Exists(path))
File.Delete(path);
orchardXmlSerializer.Serialize(File.OpenWrite(path), orchard);
MessageBox.Show("Finished");
}
}
[XmlRoot]
public class Orchard
{
[XmlElement]
public Recipe Recipe { get; set; }
[XmlElement(ElementName = "Data")]
public MyDatas MyDatas { get; set; }
}
public class Recipe
{
[XmlElement]
public string Name { get; set; }
[XmlElement]
public string Author { get; set; }
[XmlElement]
public DateTime ExportUtc { get; set; }
}
public class MyDatas
{
public MyDatas()
{
}
//[XmlElement]
//public string Test { get; set; }
[XmlElement]
public TrendDatas TrendDatas { get; set; }
}
public class TrendDatas
{
[XmlAttribute]
public string Status { get; set; }
[XmlAttribute]
public int? Id { get; set; }
//[XmlIgnore]
[XmlElement]
public TrendDatasPart TrendDatasPart { get; set; }
//[XmlIgnore]
[XmlElement]
public CommonPart CommonPart { get; set; }
}
public class TrendDatasPart
{
[XmlAttribute]
public int IdSource { get; set; }
[XmlAttribute]
public string PostalCode { get; set; }
[XmlAttribute]
public string Locality { get; set; }
[XmlAttribute]
public decimal Surface { get; set; }
[XmlAttribute]
public decimal Price { get; set; }
[XmlAttribute]
public string Type { get; set; }
[XmlAttribute]
public string InsertDateIndicator { get; set; }
[XmlAttribute]
public string UpdateDateIndicator { get; set; }
[XmlAttribute]
public DateTime GetFromDate { get; set; }
[XmlAttribute]
public DateTime UpdatedDate { get; set; }
}
public class CommonPart
{
[XmlAttribute]
public string Owner { get; set; }
[XmlAttribute]
public DateTime CreatedUtc { get; set; }
[XmlAttribute]
public DateTime PublishedUtc { get; set; }
[XmlAttribute]
public DateTime ModifiedUtc { get; set; }
}
With this code when I click on Generate xml file, I got the error InvalidOperationException
There was an error reflecting type 'MergeExcelFiles.Orchard'.
{"There was an error reflecting property 'MyDatas'."}
Like you see in my comments, I try to just add a string xmlElement to node MyDatas, with this change I got no error but in the xml file I don't have any node with name Data.
I don't understand why with class Recipe all look right but with node MyDatas nothing showed in xml file or got this error "InvalidOperationException".
You need to dig into your error message more because the reason is in the innermost exception:
System.InvalidOperationException: Cannot serialize member 'Id' of type System.Nullable`1[System.Int32]. XmlAttribute/XmlText cannot be used to encode complex types.
The issue is that you have a nullable value type as a property (TrendDatas.Id) to be serialized as an attribute and XmlSerializer does not handle these well. There are a number of workarounds listed here and here. None of them is particularly elegant. The best option might be changing the definition of Id to an element:
public class TrendDatas
{
// ... snip ...
[XmlElement(IsNullable = true)]
public int? Id { get; set; }
public bool ShouldSerializeId() { return Id.HasValue; }
// ... snip ...
}
The ShouldSerializeId is a method that, by convention, the serializer uses to decide if the property should be serialized in the output. In the case of a null value, no element will be defined in the serialized output.

deserialize xml in complex object

I can't understand why object is null:
WebClient browse = new WebClient();
StreamReader res = new StreamReader(browse.OpenRead("http://ws.audioscrobbler.com/2.0/?method=track.getinfo&api_key=b25b959554ed76058ac220b7b2e0a026&artist=cher&track=believe"));
string result = res.ReadToEnd();
XmlDocument xmltrackinfo = new XmlDocument();
xmltrackinfo.InnerXml = result;
XmlRootAttribute xRoot = new XmlRootAttribute();
xRoot.ElementName = "lfm";
xRoot.IsNullable = true;
XmlSerializer xs = new XmlSerializer(typeof(fm), xRoot);
fm rez = (fm) xs.Deserialize(new MemoryStream(Encoding.UTF8.GetBytes(result)));
Object Model:
[Serializable()]
[XmlRoot(ElementName = "lfm", IsNullable = true)]
public class fm
{
[XmlElement("lfm")]
public Track lfm { get; set; }
}
[Serializable()]
[XmlRoot(ElementName = "artist", IsNullable = true)]
public class artist
{
public string name { get; set; }
public string mbid { get; set; }
public string url { get; set; }
}
[Serializable()]
[XmlRoot(ElementName = "album", IsNullable = true)]
public class album
{
public string artist { get; set; }
public string title { get; set; }
public string mbid { get; set; }
public string url { get; set; }
public List<string> image { get; set; }
}
[Serializable()]
[XmlRoot(ElementName = "tag", IsNullable = true)]
public class tag
{
public string name { get; set; }
public string url { get; set; }
}
[Serializable()]
[XmlRoot(ElementName = "wiki", IsNullable = true)]
public class wiki
{
public string summary { get; set; }
public string content { get; set; }
}
[Serializable()]
[XmlRoot(ElementName = "track", IsNullable = true)]
public class Track
{
public string id { get; set; }
public string name { get; set; }
public string mbid { get; set; }
public string url { get; set; }
public string duration { get; set; }
public string streamable { get; set; }
public string listeners { get; set; }
public string playcount { get; set; }
public artist artist { get; set; }
public album album { get; set; }
public List<tag> toptags { get; set; }
public wiki wiki { get; set; }
}
and XML:
http://ws.audioscrobbler.com/2.0/?method=track.getinfo&api_key=b25b959554ed76058ac220b7b2e0a026&artist=cher&track=believe
so what should I do?
Try renaming your fm class to lfm.
public class lfm
{
public Track track { get; set; }
}
and then you could also get rid of the xRoot variable:
XmlSerializer xs = new XmlSerializer(typeof(lfm));
lfm rez = (lfm) xs.Deserialize(new MemoryStream(Encoding.UTF8.GetBytes(result)));
Also you don't need the [Serializable] attribute. This is used for binary serialization and is completely ignored by the XmlSerializer class.
The lfm property of the fm class must have track as its XmlElement:
[Serializable()]
[XmlRoot(ElementName = "lfm", IsNullable = true)]
public class fm
{
[XmlElement("track")]
public Track lfm { get; set; }
}

XmlSerializer Deserialize returns empty array

I'm trying to deserialize the following XML (excerpt):
<NSArray>
<Song id="23507" type="Song">
<title>Waking The Demon</title>
<artist id="17" type="Artist">
<nameWithoutThePrefix>Bullet For My Valentine</nameWithoutThePrefix>
<useThePrefix>false</useThePrefix>
<name>Bullet For My Valentine</name>
</artist>
</Song>
<Song id="3663" type="Song">
<title>Hand Of Blood</title>
<artist id="17" type="Artist"/>
</Song>
<Song id="59226" type="Song">
<title>Your Betrayal</title>
<artist id="17" type="Artist"/>
</Song>
</NSArray>
with the following classes:
[GeneratedCode("xsd", "4.0.30319.1")]
[DebuggerStepThrough]
[XmlType(AnonymousType = true)]
[XmlRoot(ElementName = "NSArray", Namespace = "", IsNullable = false)]
public class SearchResult
{
[XmlElement("Song", Form = XmlSchemaForm.Unqualified)]
public Song[] Items { get; set; }
}
[GeneratedCode("xsd", "4.0.30319.1")]
[DebuggerStepThrough]
[XmlType(AnonymousType = true)]
[XmlRoot(Namespace = "", IsNullable = false)]
public class Song
{
[XmlElement(Form = XmlSchemaForm.Unqualified)]
public string Title { get; set; }
[XmlElement("artist", Form = XmlSchemaForm.Unqualified)]
public Artist Artist { get; set; }
[XmlAttribute]
public string Type { get; set; }
[XmlAttribute]
public string Id { get; set; }
}
[GeneratedCode("xsd", "4.0.30319.1")]
[DebuggerStepThrough]
[XmlType(AnonymousType = true)]
public class Artist
{
[XmlElement(Form = XmlSchemaForm.Unqualified)]
public string NameWithoutThePrefix { get; set; }
[XmlElement(Form = XmlSchemaForm.Unqualified)]
public string UseThePrefix { get; set; }
[XmlElement(Form = XmlSchemaForm.Unqualified)]
public string Name { get; set; }
[XmlAttribute]
public string Type { get; set; }
[XmlAttribute]
public string Id { get; set; }
}
and the following code:
var request = WebRequest.Create(string.Format("http://myurl.com");
request.BeginGetResponse(GetEventResponseCallback, request);
private void GetEventResponseCallback(IAsyncResult result)
{
var request = (HttpWebRequest)result.AsyncState;
var response = request.EndGetResponse(result);
if (response.GetResponseStream() == null) return;
using (var stream = response.GetResponseStream())
{
_xmlReader = XmlReader.Create(stream);
var songs = _xmlSerializer.Deserialize(_xmlReader) as SearchResult;
}
}
However, on var songs = _xmlSerializer.Deserialize(_xmlReader) as SearchResult;, the Deserialization executes successfully, but the songs variable does not contain any data. If I inspect with the debugger, it returns Could not evaluate expression for all the values in the array.
Any hints? Thanks.
Your SearchResult class needs some fixing. You're really close, the code is only missing a few element names and the serializable attributes.
Here's a class that works:
[GeneratedCode("xsd", "4.0.30319.1")]
[DebuggerStepThrough]
[XmlType(AnonymousType = true)]
[XmlRoot(ElementName = "NSArray", Namespace = "", IsNullable = false)]
[Serializable]
public class SearchResult
{
[XmlElement("Song", Form = XmlSchemaForm.Unqualified)]
public Song[] Items { get; set; }
}
[GeneratedCode("xsd", "4.0.30319.1")]
[DebuggerStepThrough]
[XmlType(AnonymousType = true)]
[XmlRoot(ElementName="Song", Namespace = "", IsNullable = false)]
[Serializable]
public class Song
{
[XmlElement("title", Form = XmlSchemaForm.Unqualified)]
public string Title { get; set; }
[XmlElement("artist", Form = XmlSchemaForm.Unqualified)]
public Artist Artist { get; set; }
[XmlAttribute("type")]
public string Type { get; set; }
[XmlAttribute("id")]
public string Id { get; set; }
}
[GeneratedCode("xsd", "4.0.30319.1")]
[DebuggerStepThrough]
[XmlType(AnonymousType = true)]
[Serializable]
public class Artist
{
[XmlElement("nameWithoutThePrefix", Form = XmlSchemaForm.Unqualified)]
public string NameWithoutThePrefix { get; set; }
[XmlElement("useThePrefix", Form = XmlSchemaForm.Unqualified)]
public string UseThePrefix { get; set; }
[XmlElement("name", Form = XmlSchemaForm.Unqualified)]
public string Name { get; set; }
[XmlAttribute("type")]
public string Type { get; set; }
[XmlAttribute("id")]
public string Id { get; set; }
}
I have a class that I am serializing and deserializing. Compared to the attributes you are using... you have a lot that I don't have (on the class), but one that you are missing is [Serializable]
Mine looks like this:
[Serializable]
public class Settings
{
[XmlElement]
public int Version { get; set; }
[XmlElement]
public string Name { get; set; }
// more settings
}
So start with something simple like this, that works, then add in each new tag one at a time, and you will quickly find which one is causing it to break.
Since you are having trouble deserializing, here is the code I am using. In another class, I have:
public string FilePath { get; set; }
public Settings LoadSettings()
{
XmlSerializer serializer = new XmlSerializer(typeof(Settings));
Settings settings = null;
using(TextReader reader = new StreamReader(this.FilePath))
{
settings = (Settings)serializer.Deserialize(reader);
}
return settings;
}

Categories