2

I am trying to simply load an entity, modify a property and then save it back to the database.

var db = new NewsletterContext();
var newsletter  = db.Newsletters.Find(x => x.ID==newsletterID); 
newsletter.SomeProperty = 5;
db.SaveChanges();

This causes a validation error as there are some properties on the newsletter object which are required and apparently not loaded when I do a Find().

I can solve this using an Include() for each required property followed by a Where():

var db = new NewsletterContext();
var newsletter  = db.Newsletters.Include(x => x.RequiredProp1)
                    .Include(x => x.RequiredProp2).Include(x => x.RequiredProp3)
                    .Where(x => x.ID==newsletterID)
                    .FirstOrDefault(); 
db.SaveChanges();

This isn't a very elegant solution and will break if I add more required properties to the Newsletter object.

Is there a better solution?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Judo
  • 5,167
  • 3
  • 24
  • 34
  • Yes, sorry just the terminology. RequiredProp1 etc are actually related entities. The newsletter entity itself loads fine and I can read all the properties but saving back causes the error. – Judo Jan 09 '12 at 06:11
  • EF 4.1 code first with .NET 4.0 in an MVC 3 app. – Judo Jan 09 '12 at 06:31

2 Answers2

6

Entity framework will disable lazy loading when doing the validation. Hence if you put required validation on navigational properties the validation will fail. You can decorate the scalar property related to the navigational property instead.

public class Foo
{

    [Required]
    public int? RequiredScalarId { get; set; }

    public virtual Bar RequiredNavigationalProp { get; set; }
}
Eranga
  • 32,181
  • 5
  • 97
  • 96
  • Doesn't this just add another (perhaps un-necessary) field to the database? Is there a way to programmatically turn on lazyloading for the validation? – Judo Jan 09 '12 at 06:38
  • @Judo Lazy loading on validation is disabled because of the exact reason. Avoid unnecessary lazy loading. Having the scalar property is convenient like in this case. It does not add a field to the database. – Eranga Jan 09 '12 at 06:45
1

Do not want to pollute your model with foriegn keys? Good! Read on. Instead you can override the SaveChanges method in NewsletterContext. Get all modified entities and using reflection, load all their lazy loaded properties and collections using the following sample syntax:

db.Entry(newsletter).Reference(n => n.RequiredProp1).Load();
db.Entry(newsletter).Collection(n => n.RequiredCollec1).Load();

It won't be easy as it sounds, but once you are have written it, it would feel so good to see it work seamlessly :)

Ε Г И І И О
  • 11,199
  • 1
  • 48
  • 63