请问关于c的具体应用场景有哪些?
- 内容介绍
- 文章标签
- 相关推荐
本文共计429个文字,预计阅读时间需要2分钟。
我承认这是一段非常简单的代码,但结果却让我感到沮丧。我正在使用LINQ查询实体,然后通过迭代结果创建数组。我正在观察数据库的流量,看起来一切都不错。但当我不小心重复发送LINQ到SQL的查询时,问题就出现了。
var eventList = from e in entityContext.AuctionSet select e; ArrayList DayArray = new ArrayList(); int i = 0; foreach (Auction ae in eventList) { // If I put a watch on eventList all the records are the same! Auction temp = ae; // Even tried copying to a temp value per another solution // Even though the records are different in the database, // and the correct number of records are returned, EVERY "ae" instance // has the exact same values! DayArray.Add(new { id = i, title = temp.County.ToString() }); i++; }
谢谢!
编辑:忘了提到实体来自一个视图,这对于主键的评论是有意义的.
您是否错误配置了主键,以至于EF认为某些东西是主键,而实际上每行具有相同的值?重要的是,EF每次看到具有与之前相同类型的主键的对象时,都必须为您提供相同的实例.所以如果你的所有记录都是这样的:
id | title | ... -----+-------+------- 1 | Foo | ... 1 | Bar | ... 1 | Baz | ... ...
如果EF认为id是主键,它每次都会返回相同的对象 – 所以你会看到Foo,Foo,Foo ……
本文共计429个文字,预计阅读时间需要2分钟。
我承认这是一段非常简单的代码,但结果却让我感到沮丧。我正在使用LINQ查询实体,然后通过迭代结果创建数组。我正在观察数据库的流量,看起来一切都不错。但当我不小心重复发送LINQ到SQL的查询时,问题就出现了。
var eventList = from e in entityContext.AuctionSet select e; ArrayList DayArray = new ArrayList(); int i = 0; foreach (Auction ae in eventList) { // If I put a watch on eventList all the records are the same! Auction temp = ae; // Even tried copying to a temp value per another solution // Even though the records are different in the database, // and the correct number of records are returned, EVERY "ae" instance // has the exact same values! DayArray.Add(new { id = i, title = temp.County.ToString() }); i++; }
谢谢!
编辑:忘了提到实体来自一个视图,这对于主键的评论是有意义的.
您是否错误配置了主键,以至于EF认为某些东西是主键,而实际上每行具有相同的值?重要的是,EF每次看到具有与之前相同类型的主键的对象时,都必须为您提供相同的实例.所以如果你的所有记录都是这样的:
id | title | ... -----+-------+------- 1 | Foo | ... 1 | Bar | ... 1 | Baz | ... ...
如果EF认为id是主键,它每次都会返回相同的对象 – 所以你会看到Foo,Foo,Foo ……

