How do I get Entity Framework to handle optional table splitting?
Say I have these classes:
public class temp
{
[Key, Required]
public Guid tempid { get; set; }
[Required(AllowEmptyStrings = true), MaxLength(50)]
public string name { get; set; }
[MaxLength(100)]
public string filename { get; set; }
[Required]
public virtual tempContent tempContent { get; set; }
public temp()
{
tempid = Guid.NewGuid();
}
public override string ToString()
{
return name;
}
public override bool Equals(object obj)
{
if (obj == null || GetType() != obj.GetType()) return false;
temp other = (temp)obj;
return tempid == other.tempid;
}
public override int GetHashCode()
{
return tempid.GetHashCode();
}
}
public class tempContent
{
[Key, Required]
public Guid tempid { get; set; }
public byte[] filecontents { get; set; }
[Required]
public virtual temp temp { get; set; }
}
Then I have:
public DbSet<temp> temps { get; set; }
public DbSet<tempContent> tempContents { get; set; }
I define the split with this:
modelBuilder.Entity<tempContent>(o => o.ToTable("temps"));
modelBuilder.Entity<temp>(temp =>
{
temp.HasOne(o => o.tempContent).WithOne(o => o.temp).HasForeignKey<temp>(o => o.tempid);
temp.Navigation(o => o.tempContent).IsRequired();
});
When I try add an instance without the filename/content, I get :
Microsoft.EntityFrameworkCore.DbUpdateConcurrencyException: The database operation was expected to affect 1 row(s), but actually affected 0 row(s); data may have been modified or deleted since entities were loaded.