[英]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 Model
和Logical 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) 您会发现两个图层(不确定图层名称是否到处使用)
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.