2017-12-27 8 views
0

System.NotSupportedException:コレクションは読み取り専用です。 System.ThrowHelper.ThrowNotSupportedExceptionで (ExceptionResourceリソース)Microsoft.EntityFrameworkCore.Metadata.Internal.ClrICollectionAccessor`3.Addで (例えば、オブジェクトの値をオブジェクト) Microsoft.EntityFrameworkCore.ChangeTracking.Internal.NavigationFixer.AddToCollection(InternalEntityEntryエントリにおいて、私は、エラー(リポジトリ)を取得していますどこINavigationナビゲーション、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; 
} 
+0

モデルを表示できますか? –

+0

私のモデルは質問 – jose8789

+0

に掲載されています。これは修正されたバグかサンプルが不完全であるとは言えませんが、EF Core 2.0.1ではサンプルは期待通りに動作します(例外はありません)。 –

答えて

0

私はあなたがしている方法を変更しようとしました_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() 
} 
関連する問題