c# - Attaching Issue in Entity Framework -


i create stored procedure returns table (physically stored table).

create procedure usptest begin  select * table1 end 

when capture output using entity framework, loads entity properly.

var output = entities.database.sqlquery<table1>("dbo.usptest").tolist<table1>(); 

the "output" variable contains data returned sp list of table1 object doesn't not load foreign key tables automatically. added below code mitigate problem

foreach (var member in output) {    entities.table1s.attach(member); } 

after attaching each entity child tables linked each table1 member. but, when come same method second time, gives me error failing attach.

attaching entity of type 'table1' failed because entity of same type has same primary key value. can happen when using 'attach' method or setting state of entity 'unchanged' or 'modified' if entities in graph have conflicting key values.  

i tried setting state of entity detached of no luck!! have clue should here?

i'm using database first approach.

the results of database.sqlquery<telement> never tracked context if type of object returned entity type. if want returned entities tracked context, should use dbset<tentity>.sqlquery method:

var output = entities.table1s.sqlquery("dbo.usptest").tolist(); 

this way don't need use code attach entities.

but, why want access table1 using sp when have dbset<table1> table1s?. guess have more complex stored procedure , did explain issue, if sp, @dleh commented above, should use linq entities.


Comments

Popular posts from this blog

css - SVG using textPath a symbol not rendering in Firefox -

Java 8 + Maven Javadoc plugin: Error fetching URL -

node.js - How to abort query on demand using Neo4j drivers -