sql server - History tables for many to many relationships -
i need track version history of several entities, able see state of properties @ given point in history, includes many many relationships. has support changesets. given following tables:
entitya ------- id name revision changeid entityb ------- id name revision changeid entityatoentitybmapping ----------------------- entityaid entitybid changeid changetracking -------------- changeid date user description
for entitya , entityb tables have history table exact same columns. data modified through object model, before change committed create new changetracking entry , add id entities being inserted or updated, plus incrementing revision. have insert/update trigger on entitya , entityb tables, , copy previous data history table. @ given point have history entity in history table.
the problem comes many many mapping tables. same mapping table , have history table, need able "at revision 3, entitya had these values columns, , these relationships entityb". entitya history table joined mapping history table changeid, happens when name of entitya changed? @ point relationship history lost, since it's new revision.
i can think of 3 scenarios need supported:
- only entitya changed, no changes in relationships.
- only relationships between entitya , entityb change.
- both entitya changed , relationships modified.
for these, consistency needs maintained , should able properties , relationships given revision of entitya.
do know how can handle history many many tables? after searching lot haven't been able find satisfactory solution problem. way, i'm using entity framework , sql server in case relevant.
thanks
this different might in head, , more simple, may not problem, if supports requirement scenarios. hope did not miss of them.
case #1 entitya changed, no changes in relationships.
- a copy of entitya inserted entityahistory (with actual date)
- a copy of entityatoentitybmapping inserted entityatoentitybmappingahistory (with actual date)
- entitya changed , gets new revision
case #2 relationships between entitya , entityb change.
- a copy of entityatoentitybmapping inserted entityatoentitybmappingahistory (with actual date)
- entityatoentitybmapping changed , gets new revision
case #3 both entitya changed , relationships modified.
- a copy of entitya inserted entityahistory (with actual date)
- a copy of entityatoentitybmapping inserted entityatoentitybmappingahistory (with actual date)
- entitya changed , gets new revision
- entityatoentitybmapping changed , gets new revision
(actually case #1 , case #2 can combined, redundant copy entityatoentitybmapping entityatoentitybmappingahistory twice)
when join of entityahistory, entitybhistory, entityatoentitybmappinghistory
you'll want include not id revision field in join. e.g. want see history of revision of entitya:
select * entityahistory a, entitybhistory b, entityatoentitybmappinghistory m a.id = entityatoentitybmappinghistory.entityaid , b.id = entityatoentitybmappinghistory.entitybid , a.revision = entityatoentitybmappinghistory.entityarevision , b.revision = entityatoentitybmappinghistory.entitybrevision , a.revision = :certainrevisionofentitya
the model:
entitya ------- id name revision entityb ------- id name revision entityatoentitybmapping ----------------------- entityaid entitybid entityahistory ------- id name revision date entitybhistory ------- id name revision date entityatoentitybmappinghistory ----------------------- entityaid entityarevision entitybid entitybrevision date
Comments
Post a Comment