mapping - Entity Framework: Mapped multiple Tables into one Entity -Insert problems -
today have entity framework question. have 2 tables: projects , projects_rights
alt text http://www.zaczek.net/ef-stackoverflow.jpg
- projects contains projects...
- project_rights contains access rights each identity (=user) each project. table computed triggers/functions.
mapping table one entity no challenge:
class project { int id; double aufwandges; datetime createdon; ... int currentidentity__implementation__; int currentaccessrights__implementation__; } <entitysetmapping name="projekt"> <entitytypemapping typename="istypeof(model.projekt)"> <mappingfragment storeentityset="projekt"> <scalarproperty name="id" columnname="id" /> <scalarproperty name="aufwandges" columnname="aufwandges" /> <scalarproperty name="changedon" columnname="changedon" /> <scalarproperty name="createdon" columnname="createdon" /> <scalarproperty name="kundenname" columnname="kundenname" /> <scalarproperty name="name" columnname="name" /> </mappingfragment> <mappingfragment storeentityset="projekt_rights"> <scalarproperty name="id" columnname="id" /> <scalarproperty name="currentidentity__implementation__" columnname="identity" /> <scalarproperty name="currentaccessrights__implementation__" columnname="right" /> </mappingfragment> </entitytypemapping> </entitysetmapping>
selecting projects either:
var prjlist = ctx.getquery<project>() // condition added automatically custom linq provider .where(p => p.currentidentity__implementation__ == thread.currentprincipal.identity.id);
i've figured out how prevent entity framework updating currentidentity__implementation__(=identity) & currentaccessrights__implementation__(=right). done in ssdl setting storegeneratedpattern="computed".
<entitytype name="projekte_rights"> <key> <propertyref name="id" /> </key> <property name="id" type="int" nullable="false" /> <property name="identity" type="int" nullable="false" storegeneratedpattern="computed" /> <property name="right" type="int" nullable="false" storegeneratedpattern="computed" /> </entitytype>
i've declared cascade delete in sql server , ssdl. works great!
my problem is: how can prevent entity framework inserting record project_rights table? adding records there done trigger/function.
thanks pointing me right direction!
edit:
i found way. alex helping me leaving path.
i've created view
create view projekte_with_rights select tbl.*, r.[identity], r.[right] projekte tbl inner join projekte_rights r on tbl.id = r.id
this view updatable. able delete row implemented trigger:
create trigger projekte_with_rights_deletetrigger on projekte_with_rights instead of delete begin delete projekte id in (select id deleted) end
now can map view "table" in entity framework. [identity] , [rights] mapped computed columns.
another trigger responsible filling in correct identities , rights. , here have problem. if insert more 1 row projekte_rights table ef claims entity returned more 1 row. that's story , out of scope of question.
if want more granular control on inserts & updates need use storedprocedures insert/update/delete operations.
unfortunately scope of update/insert/delete entity, can't more granular , configure sproc (for 1 table) , standard t-sql (for other table).
this means if need override control projekt_rights table have projekt table too.
check out this sample more.
note: if need insert, unfortunately have update , delete too.
hope helps
alex
Comments
Post a Comment