简体   繁体   English

实体框架:如何在数据库中的同一表的模型中具有多个实体?

[英]Entity Framework: How to have several entites in model for same table in database?

I'm new to entity framework and I've read entity framework documentation and It had talk about Conceptual Model and Logical Model . 我是实体框架的新手,并且已经阅读了实体框架文档,并且讨论了Conceptual ModelLogical Model

I was thinking if entities in my model should match with database tables? 我在想模型中的实体是否应该与数据库表匹配? I guess the answer is no. 我想答案是否定的。 But I want to know how can I Implement below scenario: 但是我想知道如何实现以下方案:

I want to have an entity for updating of a table and have another one for reading from that table. 我想要一个用于更新表的实体,并想要另一个用于从该表读取的实体。 Eg I want to have an entity called EditStudent and other one named ViewStudent with different fields brought from table Student in my database. 例如,我想要一个名为EditStudent的实体,另一个名为ViewStudent的实体,它具有从数据库中的Student表带来的不同字段。

And then when I update EditStudent field I can submit changes to those field on Student table. 然后,当我更新EditStudent字段时,可以将更改提交到Student表上的那些字段。

How is it possible? 这怎么可能? And besides, is my scenario is a usual practice in Entity Framework or it is common to have one entity per database table? 此外,我的场景是Entity Framework中的惯例吗,还是每个数据库表只有一个实体?

Usually when handling databases there are several levels of abstraction. 通常,在处理数据库时,有多个抽象级别。

When using Entity Framework you'll quite often the the Repository Pattern . 使用Entity Framework时,通常会使用Repository Pattern If you google it you'll find numerous explanations. 如果您使用google搜索,则会发现许多说明。

You'll find two layers (not sure if the names of the layers are the used everywhere) 您会发现两个图层(不确定图层名称是否到处使用)

  • The database layer. 数据库层。 This represents the tables in your database and the relationships between the tables. 这表示数据库中的表以及表之间的关系。 In Entity Framework this is your DbContext, with its DbSets. 在实体框架中,这是您的DbContext及其DbSet。 Every DbSet is a table, holding the columns and constraints on the columns. 每个DbSet都是一个表,其中包含列和列上的约束。 The model stored in the DbContext represents the relations between the tables: one-to-many, many-to-many, which keys are used as foreign keys etc. 存储在DbContext中的模型表示表之间的关系:一对多,多对多,哪些键用作外键等。
  • The repository layer represents the actions you'd want to do on the database layer. 存储库层表示您要在数据库层上执行的操作。 It hides what database method you are using. 它隐藏了您正在使用的数据库方法。 Users of your repository layer shouldn't know whether this layer uses entity framework or another method to access the database. 您的存储库层的用户不应该知道该层是使用实体框架还是其他方法来访问数据库。 In fact it doesn't have to be a database; 实际上,它不必是数据库; for a user of the repository layer the data could be an excel spreadsheet file. 对于存储库层的用户,数据可以是excel电子表格文件。

If you follow this division, your DbContext should represent your database. 如果按照这种划分,则DbContext应该代表您的数据库。 The way you plan to use this database is not in your DbContext. 您计划使用此数据库的方式不在您的DbContext中。

The notion of an editable student and a vieuwable student are typically things from your repository layer. 可编辑学生有才干学生的概念通常是您存储库层中的内容。 They will both access the Student table in your DbContext, but one type will probably only return interfaces with public get functions, while the other type will have functionality to change the data of the Student. 它们都将访问DbContext中的Student表,但是一种类型可能只会返回带有public get函数的接口,而另一种类型将具有更改Student数据的功能。 Both will use the same table in the same DbContext. 两者将在相同的DbContext中使用相同的表。

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

相关问题 实体框架将嵌套实体映射到视图模型 - Entity Framework Map Nested entites to View Model 是否有任何方法可以在Entity Framework中创建两个entite之间的关系,它在数据库中没有任何关系? - Is there is any way to create relation between two entites in Entity Framework, which doesn't have any relation in database? 实体框架 REST 端点以获取具有另一个实体的所有实体 - Entity Framework REST endpoint to get all entites that have another entity 如何在现有的远程数据库中添加表并将其链接到Entity Framework模型? - How to add table in existing remote database and link it to Entity Framework model? 实体框架:如何具有2个引用同一张其他表的属性 - Entity Framework: How to have 2 properties referring to same other table 实体框架核心 model 具有同一表的层次结构 - Entity Framework Core model with hierarchy of the same table 一次创建带有多个局部视图的多个依赖实体-Entity Framework - create several depending entites at once rendered with several partial views - Entity Framework 使用实体框架过滤相关实体 - Filtering Related Entites with Entity Framework 实体框架-嵌套实体为null - Entity Framework - nested entites are null 实体框架,渴望加载实体 - Entity Framework, eager loading entites
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM