开发者

How to add entity to multiple tables?

开发者 https://www.devze.com 2023-04-08 20:51 出处:网络
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 operati

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 E开发者_JS百科F to save the Student object to 2 tables.

Anyone can help?

PS It should be done in code, not using SQL triggers or something.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号