实体框架5 DbUpdateException:不可为空的成员Null值 [英] Entity Framework 5 DbUpdateException: Null value for non-nullable member

查看:219
本文介绍了实体框架5 DbUpdateException:不可为空的成员Null值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些数据模型:

[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();
    }
}

和一个复杂类型:

    [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; }
}

和数据库包含:

[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();
        }



错误:
ExceptionType
System.Data这。 Entity.Infrastructure.DbUpdateException
不可为空的会员会籍Null值:'统计'。
变量被完全填满,并在它的对象统计信息。
有什么不对?

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?


推荐答案

复杂类型不能为null - 所以你必须保存之前创建类的实例。这篇文章帮我解决了同样的问题。
http://weblogs.asp.net/manavi/archive/2011/03/28/associations-in-ef-4-1-code-first-part-2-complex-types.aspx

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

这篇关于实体框架5 DbUpdateException:不可为空的成员Null值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆