简体   繁体   English

EF Core In Memory Database 不保存 ICollection 对象列

[英]EF Core In Memory Database not saving ICollection object column

I have read that InMemory Database with EF Core has limitations and was considering moving to sqlite for development, however, I wanted to know if this behavior is a limitation of InMemory Database or if it's me.我已经读到带有 EF Core 的 InMemory 数据库有局限性,并且正在考虑迁移到 sqlite 进行开发,但是,我想知道这种行为是 InMemory 数据库的局限性还是我的局限性。 I have tried reading the documentation but can't find material explicitly mentioning this.我试过阅读文档,但找不到明确提到这一点的材料。 I have the following:我有以下几点:

    // Startup
    services.AddDbContext<StudentResultsContext>(opt =>
                                               opt.UseInMemoryDatabase("StudentResultsList"));
    // context
    public class StudentResultsContext : DbContext
    {
        public StudentResultsContext(DbContextOptions<StudentResultsContext> options)
            : base(options)
        {
        }

        public DbSet<StudentResults> StudentResultsList { get; set; }
    }

    // classes
    public class StudentResults
    {
        public long ID { get; set; }
        public long ParentId { get; set; }
        public string Name { get; set; }
        public ICollection<ExamScores> Results { get; set; }
    }

    public class ExamScores
    {
        public long ID{ get; set; }
        public long StudentId { get; set; }
        public Exam ExamType { get; set; }
        
        public double Score { get; set; }
    }
    // controller
    [HttpPost]
    public async Task<ActionResult<StudentResults>> PostStudentResults(StudentResults studentResults)
    {
        _context.StudentResultsList.Add(studentResults);
        await _context.SaveChangesAsync();

        return CreatedAtAction(nameof(GetStudentResults), new { id = studentResults.ID }, studentResults);
    }

Results is saved as null in the database up even though the return from post claims they were created, like so Results在数据库中保存为空,即使从创建后的声明返回,就像这样

The post 在此处输入图片说明

What comes back from get 在此处输入图片说明

Is this something I did or a problem with InMemory Databse?这是我做的还是 InMemory Databse 的问题?

I guess you don't do anything to load related data and thus you get null for Results .我猜您没有做任何事情来加载相关数据,因此您的Resultsnull

Try to get saved entity with context.StudentResultsList.Include(s => s.Results) .尝试使用context.StudentResultsList.Include(s => s.Results)获取保存的实体。

CheckLoading Related Data for other strategies.检查加载相关数据以了解其他策略。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM