简体   繁体   English

带有ASP.NET的实体框架为一对多集合返回null

[英]Entity Framework with ASP.NET returning null for one-to-many collection

I'm trying to create a one to many relationship with the following objects. 我正在尝试与以下对象建立一对多关系。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Data.Common;
using System.ComponentModel.DataAnnotations.Schema;
using Microsoft.EntityFrameworkCore;

namespace Program.Models
{
    public class Log
    {
        public int ID { get; set; }
        public string Source { get; set; }
        public string Title { get; set; }
        public DateTime Timestamp { get; set; }
        public ICollection<Category> Categories { get; set; }

        public Log()
        {
            this.Categories = new List<Category>();
        }
    }

    public class Category
    {
        public int ID { get; set; }
        public string Key { get; set; }
        public string Value { get; set; }
    }

    public class LogDBContext : DbContext
    {
        public DbSet<Log> Logs { get; set; }
        public DbSet<Category> Categories {get; set;}

        public LogDBContext(DbContextOptions<LogDBContext> options) : base(options)
        {
        }
        public LogDBContext() : base()
        {

        }
    }
}

On startup, I put the following entities into the database 在启动时,我将以下实体放入数据库中

        var cat1 = new Category { Key = "seed", Value = "seed" };
        var cat2 = new Category { Key = "seed2", Value = "seed2" };

        var log = new Log {
            Source = "seed",
                Timestamp = DateTime.Now,
                Title = "seed" };
        log.Categories.Add(cat1);
        log.Categories.Add(cat2);

        context.Logs.Add(log);

        context.SaveChanges();

I obtain all the entities by calling the function: 我通过调用函数来获取所有实体:

        [HttpGet]
        [Route("getAll")]
        public IEnumerable<Log> GetAll()
        {

            return _context.Logs.Include(c => c.Categories).AsEnumerable();


        }

I have tried putting the virtual keyword on the collection of categories (in the Log class) but no matter what I try I get null back for categories from the server. 我曾尝试将virtual关键字放在类别的集合中(在Log类中),但是无论我尝试什么,我都会从服务器获取categories null

[{"id":0,"source":"seed","title":"seed","timestamp":"2018-03-19T13:21:27.0034628","categories":null}]

Any help will be greatly appreciated. 任何帮助将不胜感激。

Edit: 编辑:

Based on the feedback I created a constructor for Log that initializes the ICollection. 根据反馈,我为Log创建了一个构造函数,用于初始化ICollection。

public Log()
{
    this.Categories = new List<Category>();
}

Now, instead of null I'm getting an empty list [{"id":0,"source":"seed","title":"seed","timestamp":"2018-03-19T13:21:27.0034628","categories":[]}] 现在,不是null ,而是一个空列表[{"id":0,"source":"seed","title":"seed","timestamp":"2018-03-19T13:21:27.0034628","categories":[]}]

A step in the right direction but not there yet. 朝正确方向迈出的一步,但还没有完成。

edit: For clarification, I'm using EF-Core 2.0.2 edit: Also, I am using .NET Core 2.05 编辑:为澄清起见,我使用的是EF-Core 2.0.2编辑:另外,我使用的是.NET Core 2.05

"solution": I switched my project to use Asp.net with the full .Net Framework and it appears to work. “解决方案”:我将我的项目切换为在完整的.Net Framework中使用Asp.net,它似乎可以正常工作。 I don't know why it refused to work with the asp.net with .net core. 我不知道为什么它拒绝使用带有.net核心的asp.net。

Problem is that your One-to-Many relationship is not configured correctly. 问题是您的一对多关系配置不正确。 Write your model classes as follows: 编写模型类,如下所示:

public class Log
{
    public Log()
    {
        this.Categories = new List<Category>();
    }

    public int ID { get; set; }
    public string Source { get; set; }
    public string Title { get; set; }
    public DateTime Timestamp { get; set; }
    public ICollection<Category> Categories { get; set; }


}

public class Category
{
    public int ID { get; set; }

    [ForeignKey("Log")]
    public int LogID { get; set;}
    public string Key { get; set; }
    public string Value { get; set; }

    public Log Log {get; set;}
}

Hope, this will solve your problem.. 希望能解决您的问题。

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

相关问题 ASP.NET MVC3和实体框架 - 一个视图中的一对多关系 - ASP.NET MVC3 and Entity Framework- one-to-many relationship in one view ASP.Net实体框架一对多关系:如何建立依赖关系和延迟加载不起作用 - ASP.Net Entity Framework one-to-many relationship: How to establish dependency and lazy loading not working ASP.NET MVC 5实体框架6自引用一对多关系 - ASP.NET MVC 5 Entity Framework 6 Self-Referencing One-to-Many Relation 通过ASP.NET WebMethod在JSON中添加实体框架实体(一对多) - Adding Entity Framework entities (one-to-many) in JSON via ASP.NET WebMethod ASP.NET Core 3.1 和 Entity Framework Core:一对多的关系 - ASP.NET Core 3.1 and Entity Framework Core : one-to-many relationship 将具有一对多关系的 SQL 查询转换为具有父/子 object 结构的 ASP.NET 核心/实体框架核心调用 - Convert SQL query with a one-to-many relationship to an ASP.NET Core / Entity Framework Core call with a parent/children object structure 实体框架核心,一对多关系,集合始终为空 - Entity Framework Core, one-to-many relationship, collection always empty 实体框架一对多 - Entity Framework One-To-Many 实体框架:一对多? - Entity Framework: One-To-Many? 一对多关系在Entity Framework中返回null - One-to-many relationship returns null in Entity Framework
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM