We have a stored procedure that has a select statement in it:
select convert(int, c.ID) as ID,
convert(nvarchar(255), c.name) as name,
convert(varchar(32), a.state) as state
from customer c join address a on
c.addressid = a.ID
where c.name like @custNameSpec
This produces two records when executed in a T-SQL window:
ID Name State
1 Robert PA
2 Rob VA
When executed in Entity Framework 4 as a function import, it returns two records, but the first record is duplicated:
ID Name State
1 Robert PA
1 Robert PA
We deleted the function import and the imported function, recreated it, etc. We also added those SQL convert() statements above to guarantee Entity Framework understands the data types coming back from the server.
What could we do to fix it? What causes duplicates like that?
Our tests include:
var myresult3 = myUOW.DC.GetAdDir(todaysdate: null, store_nbr: 14,
adtype: null).ToList();
var myresult4 = DB.GetAdDir(todaysdate: null, store_nbr: 14,
adtype: null).ToList();
Both return the same incorrect result. The SQL profiler shows this call:
exec [dbo].[GetCust] @todaysda开发者_Go百科te=NULL,@custNameSpec='Rob',@adtype=NULL
EDIT:
Apparently, the business rules changed. The POCO generated from Entity Framework had a primary key improperly set, so it returned the correct quantity of fields, but "removed" duplicates by making all duplicates the same (based on the POCO primary key fields.)
There was MergeOption
referenced in other remotely related questions that may explain why this happens.
When you create a unique restriction on an Entity, like a primary key, and you have duplicates coming from the data, Entity Framework will repeat the first duplicate record for all fields for each duplicate found.
In other words, Entity Framework returns a mangled mess if your underlying data doesn't reflect the primary key correctly.
精彩评论