Как добиться загрузки (включения) коллекции частных полей с общедоступным свойством только для чтения
System.NotSupportedException: коллекция доступна только для чтения. в System.ThrowHelper.ThrowNotSupportedException(ресурс ExceptionResource) в Microsoft.EntityFrameworkCore.Metadata.Internal.ClrICollectionAccessor`3.Add(Экземпляр объекта, значение объекта) в Microsoft.EntityFrameworkCore.ChangeTracking.Internal.NavryTirectoryInColisionActionEctionCityEctionityInCityEctionity INCityEctionity INColEctionEctionCityEctionityActionEntityEctionityActionEntityEctionityActionEctionCityEctionityActionEntityEctionityActionEctionCityEctionCityEctionityEctionityEctionCityEctionityEctionCityEctionCityEctionityActionEctionCityEction).Inception.EntityActionEntityEctionityActionEntityEctionityEctionCity).InterisionEctionityEctionityEctionityEctionityEctionityEctionityEctionityColder). IClrCollectionAccessor collectionAccessor, значение объекта)
public class Student
{
public int Id {get; private set;}
private List<StudentProgress> _progresses;
protected Student()
{
_progresses = new List<StudentProgress>();
}
public IEnumerable<StudentProgress> Progresses =>_progresses.AsReadOnly();
}
public class StudentProgress
{
public int Id {get; private set;}
public int ProgressStatusId { get; private set; }
public int Year { get; private set; }
public Grade Grade { get; private set; }
public int CourseId { get; private set; }
public StudentProgress(Grade grade, int year, int courseId, int progress)
{
ProgressStatusId = progress;
Year = year;
Grade = grade;
CourseId = courseId;
}
}
public class StudentEntityTypeConfiguration : IEntityTypeConfiguration<Student>
{
public void Configure(EntityTypeBuilder<Student> studentConfiguration)
{
studentConfiguration.HasKey(x => x.Id);
studentConfiguration.Ignore(x => x.DomainEvents);
studentConfiguration.Metadata.FindNavigation(nameof(Student.Progresses))
.SetPropertyAccessMode(PropertyAccessMode.Field);
}
}
public class Context : DbContext
{
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
builder.ApplyConfiguration(new StudentEntityTypeConfiguration());
}
}
вот где я получаю сообщение об ошибке (репозиторий):
public async Task<Student> FindAsync(int identity)
{
var student = await _context.Set<Student>()
.Include(x=>x.Progresses) // this line is generating the error
.Where(b => b.Id == identity)
.SingleOrDefaultAsync();
return student;
}
1 ответ
Я пытался изменить способ, которым вы получаете _progresses
в моем коде:
public class Student
{
public int Id {get; private set;}
private List<StudentProgress> _progresses;
protected Student()
{
_progresses = new List<StudentProgress>();
}
public IEnumerable<StudentProgress> Progresses =>_progresses; // changed from AsReadOnly()
}