空子子类别或数据库定义

时间:2018-05-09 22:44:19

标签: c# asp.net-core entity-framework-core

我正在尝试开发一个非常简单的ASP.net核心2.0 Web API。它包含两个模型类:类别和子类别。但是,当我将CategoriesController称为:

http://localhost:51242/api/categories

...所有子项子类别为空。

[  
   {  
      "id":1,
      "en":"Sports",
      "de":"Sports",
      "fr":"Sports",
      "it":"Sport",
      "es":"Deportes",
      "shown":0,
      "subcategories":null,
      "subscribers":0
   },
   {  
      "id":2,
      "en":"Computers",
      "de":"Computers",
      "fr":"Computers",
      "it":"Computers",
      "es":"Computadoras",
      "shown":0,
      "subcategories":null,
      "subscribers":0
   }
]

当我向DB(UseInMemoryDatabase)播种时,我看到DbSet类别包含用于播种的json文件中存在的所有子类别(参见图像)。所以我不确定它发生了什么?我错过了什么吗?

Screenshot after seeding the DB

我是ASP.net核心新手,这是我的第一个应用程序:-)

[Produces("application/json")]
[Route("api/Categories")]
public class CategoriesController : Controller
{
    private readonly NetAdContext _context;

    public CategoriesController(NetAdContext context)
    {
        _context = context;
    }

    // GET: api/Categories
    [HttpGet]
    public IEnumerable<Category> GetCategories()
    {
        return _context.Categories;
    }
}

public class Category
{
    public long Id { get; set; }
    public string En { get; set; }
    public string De { get; set; }
    public string Fr { get; set; }
    public string It { get; set; }
    public string Es { get; set; }
    public long Shown { get; set; }
    public List<Subcategory> Subcategories { get; set; }
}

public class Subcategory
{
    public long Id { get; set; }
    public string En { get; set; }
    public string De { get; set; }
    public string Fr { get; set; }
    public string It { get; set; }
    public string Es { get; set; }
    public long Shown { get; set; }
    public long Subscribers { get; set; }
    public long CategoryId { get; set; }
    public long LocationId { get; set; }
}

从Program.cs调用NetAdSeed

static public class NetAdSeed
{
    public static void Initialize(NetAdContext context)
    {
        context.Database.EnsureCreated();

        // Seed categories and subcategories
        if (context.Categories.Count() == 0)
        {
            // Load list of categories and subcategories from file to string
            string json = System.IO.File.ReadAllText(@".\json\categories.json");

            // Deserialize
            List<Category> cats = JsonConvert.DeserializeObject<List<Category>>(json);

            context.Categories.AddRange(cats);
            context.SaveChanges();
        }
    }
}

public class NetAdContext : DbContext
{
    public DbSet<Category> Categories { get; set; }

    public DbSet<Subcategory> Subcategory { get; set; }

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

1 个答案:

答案 0 :(得分:1)

这段代码有很多问题:

IActionResult
  • 数据库请求应该是异步的,特别是对于支持开箱即用的实体框架核心。
  • 您应该始终在ASP.NET Core中返回public class CategoryDto // or some other better name { public string Name { get; set; } // Choose the MINIMUM amount of properties that the client needs } // GET: api/Categories [HttpGet] public async Task<IActionResult> GetCategories() { var categories = await _context.Categories .Include(x => x.SubCategories) // this line you are missing .ToListAsync(); return Ok(categories); } ,这样您就可以返回不同的结果并使其与结果无关
  • 您不应直接返回实体,而应使用数据传输对象。这允许您根据客户端
  • 更改返回的内容

所以,这应该是这样的:

=Fields!MyField.Value.ToString().Replace(". .", "")