"The * field is required" when the EF object has the field already

c# ef-code-first entity-framework entity-framework-6

Question

I have the following datamodel:

[Table("Jobs")]
public abstract class Job
{
    public int Id { get; set; }
    public DateTime? StartTime { get; set; }
}

[Table("PbnJobs")]
public abstract class PbnJob : Job
{
    [Required]
    public virtual PbnSite Site{ get; set; }
    [ForeignKey("Site")]
    public int SiteId { get; set; }
}

[Table("DomainJobs")]
public class DomainJob : PbnJob
{
}

public class PbnSite
{
    public int Id { get; set; }
}

Site is nothing more than a foreign key.

My code appears as follows:

var context = new LmDbContext();
var job = context.Jobs.First();
job.StartTime = DateTime.Now;
_context.SaveChanges();

The problem is that the code above raises an exception of type

System.Data.Entity.Validation.DbEntityValidationException

by stating:

"The Site field is required"

Although when I add:

var site = ((PbnJob)job).Site;

Developing the code:

var context = new LmDbContext();
var job = context.Jobs.First();
var site = ((PbnJob)job).Site;
job.StartTime = DateTime.Now;
_context.SaveChanges();

The context is then successfully stored.

What's going on here?

EDIT: I should mention that I employ lazy loading and proxies.

1
2
12/18/2014 7:52:03 PM

Popular Answer

Instead of navigation properties, you should designate with the needed attribute simple properties:

[Table("PbnJobs")]
public abstract class PbnJob : Job
{
    public virtual PbnSite Site{ get; set; }

    [ForeignKey("Site")]
    [Required]
    public int SiteId { get; set; }
}

Given that your foreign key property cannot be null, Required is not applicable in this situation.

1
12/18/2014 7:50:09 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow