简体   繁体   English

从不同的表创建 ASP Net Core Entity Framework 实体

[英]ASP Net Core Entity Framework entity creation from different tables

I am trying to create an OData Service using Entity Framework for ASP.NET Core.我正在尝试使用 ASP.NET Core 的实体框架创建 OData 服务。

First of all, I am kindly new to Entity Framework, but I have read a lot of articles related to this Topic.首先,我是 Entity Framework 的新手,但我已经阅读了很多与该主题相关的文章。 Unfortunately, some of them are not written well and furthermore most of them are not up to date or missing some key information.不幸的是,其中一些写得不好,而且大多数都不是最新的或缺少一些关键信息。 So I am going to ask you.所以我要问你。

Current Situation:现在的情况:

I am trying to create a Model which combines values from two datatables of a datasource.我正在尝试创建一个模型,它结合了来自数据源的两个数据表的值。

Let´s say we have the class Order and Article.假设我们有 Order 和 Article 类。 Each order contains a bunch of articles.每个订单包含一堆文章。 The objects can be mapped by the OrderID property which is stored in Order and Article.对象可以通过存储在 Order 和 Article 中的 OrderID 属性进行映射。

So we have two lists containing every order and every article line in contract to the order所以我们有两个列表,包含每个订单和订单合同中的每个文章行

  • Order1 (0)订单 1 (0)
  • --> art1 (0)(0) -->艺术 1 (0)(0)
  • --> art2 (0)(1) -->艺术 2 (0)(1)
  • --> art3 (0)(2) -->艺术 3 (0)(2)
  • Order2 (1)订单 2 (1)
  • --> art1 (1)(0) -->艺术1 (1)(0)

Of Course the orders and articles contains a lot of more properties, but in my test case I just would like to get the OrderID and the related article names.当然,订单和文章包含更多属性,但在我的测试案例中,我只想获取 OrderID 和相关文章名称。

public class OrderList
{
    public int OrderID { get; set; }
    public List<string> Article { get; set; }
}

I want to create a new Model called OrderList.我想创建一个名为 OrderList 的新模型。 In that model, I have the OrderID and a List of article Code (string)在那个模型中,我有 OrderID 和文章代码列表(字符串)

I am getting the data from a database and need them to join into my new object OrderList.我从数据库中获取数据并需要它们加入到我的新对象 OrderList 中。

Currently, I just got stuck by creating the Entity relationship.目前,我只是因为创建实体关系而陷入困境。 Do you guys have an idea to solve this properly?你们有什么想法可以正确解决这个问题吗?

  • ASP.NET Core 2.1 ASP.NET 核心 2.1
  • Microsoft.AspNetCore.OData (7.1.0) Microsoft.AspNetCore.OData (7.1.0)
  • Microsoft.EntityFrameworkCore (2.1.4) Microsoft.EntityFrameworkCore (2.1.4)

What you want is implemented via navigation properties.您想要的是通过导航属性实现的。 When following the naming conventions , it is as easy as following:遵循命名约定时,就像以下一样简单:

public class Order
{
    public int OrderId { get; set; } // primary key
    public List<Article> Articles { get; set; } // navigation property
}

public class Article
{
    public int ArticleId { get; set; } // primary key
    public string Name { get; set; }

    public int OrderId { get; set; } // foreign key
    public Order Order { get; set; } // navigation property
}


public MyDbContext : DbContext
{
    public DbSet<Order> Orders { get; set; }
    public DbSet<Article> Articles { get; set; }
}

var foo = ctx.Orders.Include(e => e.Articles).ToList();

You can read more about ithere .您可以在此处阅读更多相关信息。

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

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