如何向多个表添加实体?
我的数据库中有 2 个表:Students
和 StudentsHistory
。这个想法是,Students
表中的每个更改都必须在 StudentsHistory
表中创建一条新记录(例如,当我编辑学生时,必须执行 2 个操作:对学生进行更新并插入 StudentsHistory)。
如何使用 Entity Framework 4.1 Code-First 来做到这一点,而不创建 2 个类并映射它们?我只想拥有 Student
类,并以某种方式告诉 EF 将 Student 对象保存到 2 个表中。
有人可以帮忙吗?
PS 它应该在代码中完成,而不是使用 SQL 触发器或其他东西。
I have 2 tables in my DB: Students
and StudentsHistory
. The idea is that every change in the Students
table must create a new record in the StudentsHistory
table (e.g. when I edit a student, 2 operations must be performed: UPDATE on Students and INSERT on StudentsHistory).
How can I do this with Entity Framework 4.1 Code-First without creating 2 classes and having them mapped? I want to have only Student
class and somehow tell EF to save the Student object to 2 tables.
Anyone can help?
PS It should be done in code, not using SQL triggers or something.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正如 Ladislav 所说,您无法将 2 个表映射到一个实体。您可能需要考虑创建 审核学生历史记录。
Like Ladislav said, you cannot map 2 tables to one entity. You may want to think about creating an audit for student history.
这是不可能的。您必须创建两个类,映射它们并在业务逻辑中处理创建。您正在寻找的自动魔法只能通过数据库触发器来执行。
It is not possible. You must create two classes, map them and handle creation in your business logic. The auto magic you are looking for can be performed only by database triggers.