Matt Barr
Matt Barr

Reputation: 494

EF: Non-nullable UpdateException for NotMapped property

I am syncing data periodically on my ASP.NET page. I have an object with two complex members, which I am ignoring in Entity Framework using the NotMapped attribute. Instead, I store serialized values.

Models:

public class Game
{
    //...
    [NotMapped]
    public Image Image { get; set; }

    [NotMapped]
    public List<Image> Images { get; set; }

    public string Image_Serialized
    {
        get
        {
            return JsonConvert.SerializeObject(Image);
        }
        set
        {
            Image = JsonConvert.DeserializeObject<Image>(value);
        }
    }

    public string Images_Serialized
    {
        get
        {
            return JsonConvert.SerializeObject(Images);
        }
        set
        {
            Images = JsonConvert.DeserializeObject<List<Image>>(value);
        }
    }
    //...
}

And...

public class Image
{
    public string IconUrl { get; set; }
    public string MediumUrl { get; set; }
    public string ScreenUrl { get; set; }
    public string SmallUrl { get; set; }
    public string SuperUrl { get; set; }
    public string ThumbUrl { get; set; }
    public string TinyUrl { get; set; }
}

When my sync is complete, I update my database:

foreach(var game in games)
{
    // address any null complex types.
    if (game.Image == null) game.Image = new Image();
    if (game.Images == null) game.Images = new List<Image>();

    // add game if new, update if already in db
    var dbGame = db.Games.Where(g => g.Id == game.Id).FirstOrDefault();

    if (dbGame == null)
    {
        db.Games.Add(game);
    }
    else
    {
        var queriedGame = db.Entry(dbGame);
        queriedGame.CurrentValues.SetValues(game);
        queriedGame.State = System.Data.Entity.EntityState.Modified;
    }

}

// returns 0 results... seems fine
var badGames = games.Where(g => g.Image == null || g.Images == null).ToList();

db.SaveChanges();

I receive the following exception at db.SaveChanges():

Server Error in '/' Application.

Null value for non-nullable member. Member: 'Image'.

Thank you for any help. Additionally, why do I need to worry about these values being null if I'm not expecting their values to be directly stored in the db (NotMapped attribute)?

Upvotes: 0

Views: 958

Answers (3)

Matt Barr
Matt Barr

Reputation: 494

+1 to Ali for sending me in the right direction.

After changing the Image to a struct, and making the Image reference nullable, I still had the same error. The issue was that the Game object was referencing a number of other object types (Developer, Publisher, Release, etc). Some of these objects had Image references in them which I had not yet set to NotMapped. My hypothesis is that when I called db.SaveChanges(), it was trying to add entries to these data tables, and causing issues.

Upvotes: 0

Ali Baig
Ali Baig

Reputation: 3867

I would not advise you to use such a model instead have image id in your game class with a ICollection of images (infact why even have both).

Having said that with the current model, you can try something like

public class Game
{
    //...
    [NotMapped]
    public Image? Image { get; set; }  //<== see ? to make it nullable

    [NotMapped]
    public List<Image> Images { get; set; }

    public string Image_Serialized
    {
        get
        {
            if(Image == null)
                return null;
            return JsonConvert.SerializeObject(Image);
        }
        set
        {
            if(value == null)
                Image = null;
            else
                Image = JsonConvert.DeserializeObject<Image>(value);
        }
    }
}

Upvotes: 1

Osman Toplica
Osman Toplica

Reputation: 21

You probably need to use Image? instead of Image as data type.

Upvotes: 0

Related Questions