如何向多个表添加实体?

发布于 2024-12-06 19:52:01 字数 409 浏览 4 评论 0原文

我的数据库中有 2 个表:StudentsStudentsHistory。这个想法是,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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

被翻牌 2024-12-13 19:52:01

正如 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.

撑一把青伞 2024-12-13 19:52:01

这是不可能的。您必须创建两个类,映射它们并在业务逻辑中处理创建。您正在寻找的自动魔法只能通过数据库触发器来执行。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文