New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Rebuilding relationships causes annotations on navigations to be lost #10612
Comments
@jose8789 I attempted to reproduce this with the code posted, but there is a lot of missing pieces. Can you look at the code below and see what is different about your code that would allow us to reproduce what you are seeing? Also, which version of EF Core and with database provider are you using? public class Student
{
public int Id { get; set; }
private List<StudentProgress> _progresses;
public Student()
{
_progresses = new List<StudentProgress>();
}
public IEnumerable<StudentProgress> Progresses => _progresses.AsReadOnly();
}
public class StudentProgress
{
public int Id { get; set; }
public int ProgressStatusId { get; private set; }
public int Year { get; private set; }
public int CourseId { get; private set; }
public StudentProgress()
{
}
public StudentProgress(int year, int courseId, int progressStatusId)
{
ProgressStatusId = progressStatusId;
Year = year;
CourseId = courseId;
}
}
public class TestContext : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder
.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=TestDatabase;ConnectRetryCount=0");
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Student>(b =>
{
b.Property(e => e.Id).ValueGeneratedNever();
b.HasKey(x => x.Id);
b.HasMany(e => e.Progresses).WithOne().HasForeignKey(e => e.ProgressStatusId);
b.Metadata.FindNavigation(nameof(Student.Progresses))
.SetPropertyAccessMode(PropertyAccessMode.Field);
});
}
}
public class Program
{
public static async Task Main()
{
using (var context = new TestContext())
{
context.Database.EnsureDeleted();
context.Database.EnsureCreated();
context.Add(new Student { Id = 7 });
context.Add(new StudentProgress(1, 2, 7));
await context.SaveChangesAsync();
}
using (var context = new TestContext())
{
var identity = 7;
var student = await context.Set<Student>()
.Include(x=>x.Progresses) // this line is generating the error
.Where(b => b.Id == identity)
.SingleOrDefaultAsync();
}
}
} |
I found out why I was having that exception, I just don't know if this is a bug or not. To Reproduce the exception you can use this model: public class Timetable
{
public int Id {get; private set;}
private readonly List<TimetableItem> _timetableItems;
public IEnumerable<TimetableItem> TimetableItems => _timetableItems.AsReadOnly();
private Timetable()
{
_timetableItems = new List<TimetableItem>();
}
}
public class TimetableItem
{
public int Slot { get; private set; }
public DayOfWeek DayOfWeek { get; private set; }
public Guid SubjectId { get; private set; }
private TimetableItem()
{
}
}
public class TimetableEntityTypeConfiguration : IEntityTypeConfiguration<Timetable>
{
public void Configure(EntityTypeBuilder<Timetable> timetatableConfiguration)
{
timetatableConfiguration.ToTable(nameof(Timetable));
timetatableConfiguration.Metadata.FindNavigation(nameof(Timetable.TimetableItems))
.SetPropertyAccessMode(PropertyAccessMode.Field);
}
}
public class TimetableItemEntityTypeConfiguration : IEntityTypeConfiguration<TimetableItem>
{
public void Configure(EntityTypeBuilder<TimetableItem> timetableItemConfiguration)
{
timetableItemConfiguration.ToTable(nameof(TimetableItem));
timetableItemConfiguration.Property<int>("TimetableId"); // if I create this property explicitly from model this error doesn't appear
timetableItemConfiguration.HasKey("TimetableId", nameof(TimetableItem.Slot),
nameof(TimetableItem.DayOfWeek));
timetableItemConfiguration.HasOne<Timetable>()
.WithMany(x => x.TimetableItems)
.HasForeignKey("TimetableId");
}
}
public Timetable FindForBooking(int timetableId)
{
return _context.Set<Timetable>()
.Include(x => x.TimetableItems)
.FirstOrDefault(x => x.Id == timetableId);
} But if I create use this model: public class TimetableItem
{
public int Slot { get; private set; }
public DayOfWeek DayOfWeek { get; private set; }
public Guid SubjectId { get; private set; }
public int TimetableId{get; private set;} // addition of this property avoids the exception
private TimetableItem()
{
}
} the error doesn't appear |
Note for triage: I was able to repro this with 2.0.1--looks like something very similar to: #7674 @jose8789 The problem seems to be that when the relationship is configured with the shadow key, then the original annotation on the navigation property is lost. A workaround is to make sure this: timetatableConfiguration.Metadata.FindNavigation(nameof(Timetable.TimetableItems))
.SetPropertyAccessMode(PropertyAccessMode.Field); Happens after this: timetableItemConfiguration.HasOne<Timetable>()
.WithMany(x => x.TimetableItems)
.HasForeignKey("TimetableId"); |
Cannot include a private collection in an entity
Steps to reproduce
The text was updated successfully, but these errors were encountered: