我有一个标准的实体框架实现直接查询一个表,根本不涉及任何视图,代码如下:
var r = cxt.HistoricalQualityComponents.Where(f => f.ProducerID == activeProducer);
一切正常,我希望有 24 条记录,我得到 24 条记录。除了当我查看 r 的内容时,我看到了第一个返回的记录,重复了 24 次。我查看生成的 SQL 并直接针对数据库运行它,我得到 24 条唯一记录。我通过在 activeProducer 更改时观察内容,甚至应用排序作为 LINQ 查询的一部分来得出结论。我不知道如何解决这个问题。这发生在我的数据库中有两个表,但没有其他表。
我尝试删除我的 .edmx 实体文件,我重命名了表(和生成的实体),并创建了一个完整的 .aspx 文件来处理实体。当然,我这样修改了查询:
var r = from h in cxt.HistoricalQualityComponents where h.ProducerId == activeProducer select h;
没有什么不同。
这可能完全不相关,但在(非常懒惰地)在两个结果集上尝试以下操作后,我注意到了这种行为:
r.Sort(delegate(HistoricalQualityComponents c1, HistoricalQualityComponents c2) { return Convert.ToDateTime(c2.Pickup_Date).CompareTo(Convert.ToDateTime(c1.Pickup_Date)); });
此时,我只想回到一个干净的状态,这样我就可以实现一个模型类,它将数据存储到一个正确类型的对象中,我可以更适当地对其进行排序,但我不知所措下一步要去哪里。
更新:生成的 SQL 如下:
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 | [Extent1].[RcdID] AS [RcdID], [Extent1].[FieldRepID] AS [FieldRepID], [Extent1].[ProducerID] AS [ProducerID], [Extent1].[MonthYear_PK] AS [MonthYear_PK], [Extent1].[Weight] AS [Weight], [Extent1].[Fat] AS [Fat], [Extent1].[Protein] AS [Protein], [Extent1].[Lactose] AS [Lactose], [Extent1].[SNF] AS [SNF], [Extent1].[StateLeuc] AS [StateLeuc], [Extent1].[Leuc] AS [Leuc], [Extent1].[StateRaw] AS [StateRaw], [Extent1].[Raw] AS [Raw], [Extent1].[RawBracket] AS [RawBracket], [Extent1].[PI] AS [PI], [Extent1].[PIBracket] AS [PIBracket], [Extent1].[lpc_avg] AS [lpc_avg], [Extent1].[Water] AS [Water], [Extent1].[AB] AS [AB], [Extent1].[SED] AS [SED], [Extent1].[mun] AS [mun], [Extent1].[LStd] AS [LStd], [Extent1].[RStd] AS [RStd], [Extent1].[PStd] AS [PStd], [Extent1].[SStd] AS [SStd], [Extent1].[QualPremRate] AS [QualPremRate], [Extent1].[QualPremAmt] AS [QualPremAmt], [Extent1].[FYTDQualScore] AS [FYTDQualScore], [Extent1].[FYTDQualPrem] AS [FYTDQualPrem], [Extent1].[FYTDAvgQualPremRate] AS [FYTDAvgQualPremRate], [Extent1].[OtherSolids] AS [OtherSolids], [Extent1].[AshFactor] AS [AshFactor], [Extent1].[ash_wtd_avg] AS [ash_wtd_avg], [Extent1].[fat_wtd_avg] AS [fat_wtd_avg], [Extent1].[leuc_wtd_avg] AS [leuc_wtd_avg], [Extent1].[protein_wtd_avg] AS [protein_wtd_avg], [Extent1].[os_wtd_avg] AS [os_wtd_avg], [Extent1].[snf_wtd_avg] AS [snf_wtd_avg], [Extent1].[iodine_wtd_avg] AS [iodine_wtd_avg] FROM (SELECT [HistoricalQualityComponents].[RcdID] AS [RcdID], [HistoricalQualityComponents].[FieldRepID] AS [FieldRepID], [HistoricalQualityComponents].[ProducerID] AS [ProducerID], [HistoricalQualityComponents].[MonthYear_PK] AS [MonthYear_PK], [HistoricalQualityComponents].[Weight] AS [Weight], [HistoricalQualityComponents].[Fat] AS [Fat], [HistoricalQualityComponents].[Protein] AS [Protein], [HistoricalQualityComponents].[Lactose] AS [Lactose], [HistoricalQualityComponents].[SNF] AS [SNF], [HistoricalQualityComponents].[StateLeuc] AS [StateLeuc], [HistoricalQualityComponents].[Leuc] AS [Leuc], [HistoricalQualityComponents].[StateRaw] AS [StateRaw], [HistoricalQualityComponents].[Raw] AS [Raw], [HistoricalQualityComponents].[RawBracket] AS [RawBracket], [HistoricalQualityComponents].[PI] AS [PI], [HistoricalQualityComponents].[PIBracket] AS [PIBracket], [HistoricalQualityComponents].[lpc_avg] AS [lpc_avg], [HistoricalQualityComponents].[Water] AS [Water], [HistoricalQualityComponents].[AB] AS [AB], [HistoricalQualityComponents].[SED] AS [SED], [HistoricalQualityComponents].[mun] AS [mun], [HistoricalQualityComponents].[LStd] AS [LStd], [HistoricalQualityComponents].[RStd] AS [RStd], [HistoricalQualityComponents].[PStd] AS [PStd], [HistoricalQualityComponents].[SStd] AS [SStd], [HistoricalQualityComponents].[QualPremRate] AS [QualPremRate], [HistoricalQualityComponents].[QualPremAmt] AS [QualPremAmt], [HistoricalQualityComponents].[FYTDQualScore] AS [FYTDQualScore], [HistoricalQualityComponents].[FYTDQualPrem] AS [FYTDQualPrem], [HistoricalQualityComponents].[FYTDAvgQualPremRate] AS [FYTDAvgQualPremRate], [HistoricalQualityComponents].[OtherSolids] AS [OtherSolids], [HistoricalQualityComponents].[AshFactor] AS [AshFactor], [HistoricalQualityComponents].[ash_wtd_avg] AS [ash_wtd_avg], [HistoricalQualityComponents].[fat_wtd_avg] AS [fat_wtd_avg], [HistoricalQualityComponents].[leuc_wtd_avg] AS [leuc_wtd_avg], [HistoricalQualityComponents].[protein_wtd_avg] AS [protein_wtd_avg], [HistoricalQualityComponents].[os_wtd_avg] AS [os_wtd_avg], [HistoricalQualityComponents].[snf_wtd_avg] AS [snf_wtd_avg], [HistoricalQualityComponents].[iodine_wtd_avg] AS [iodine_wtd_avg] FROM [dbo].[HistoricalQualityComponents] AS [HistoricalQualityComponents]) AS [Extent1] WHERE [Extent1].[ProducerID] = @p__linq__0 |
如果您的实体模型中的键与数据库表中的键不匹配,则可能会发生这种情况。
例如,如果数据库中的键是由
原因是 EF 仅"看到"该对的第一部分
长话短说:检查实体模型中定义的键是否与数据库模式中的键匹配。
Sinatra新手;我正在运行一些rspec测试,但在日志中收到了一堆不需要的噪音。如何消除日志中过多的噪音?我仔细检查了环境是否设置为:test,这意味着记录器级别应设置为WARN而不是DEBUG。spec_helper:require"./app"require"sinatra"require"rspec"require"rack/test"require"database_cleaner"require"factory_girl"set:environment,:testFactoryGirl.definition_file_paths=%w{./factories./test/
我有两个Rails模型,即Invoice和Invoice_details。一个Invoice_details属于Invoice,一个Invoice有多个Invoice_details。我无法使用accepts_nested_attributes_forinInvoice通过Invoice模型保存Invoice_details。我收到以下错误:(0.2ms)BEGIN(0.2ms)ROLLBACKCompleted422UnprocessableEntityin25ms(ActiveRecord:4.0ms)ActiveRecord::RecordInvalid(Validationfa
如何在ruby中调用C#dll? 最佳答案 我能想到几种可能性:为您的DLL编写(或找人编写)一个COM包装器,如果它还没有,则使用Ruby的WIN32OLE库来调用它;看看RubyCLR,其中一位作者是JohnLam,他继续在Microsoft从事IronRuby方面的工作。(估计不会再维护了,可能不支持.Net2.0以上的版本);正如其他地方已经提到的,看看使用IronRuby,如果这是您的技术选择。有一个主题是here.请注意,最后一篇文章实际上来自JohnLam(看起来像是2009年3月),他似乎很自在地断言RubyCL
我正在尝试在Ruby中复制Convert.ToBase64String()行为。这是我的C#代码:varsha1=newSHA1CryptoServiceProvider();varpasswordBytes=Encoding.UTF8.GetBytes("password");varpasswordHash=sha1.ComputeHash(passwordBytes);returnConvert.ToBase64String(passwordHash);//returns"W6ph5Mm5Pz8GgiULbPgzG37mj9g="当我在Ruby中尝试同样的事情时,我得到了相同sha
C#实现简易绘图工具一.引言实验目的:通过制作窗体应用程序(C#画图软件),熟悉基本的窗体设计过程以及控件设计,事件处理等,熟悉使用C#的winform窗体进行绘图的基本步骤,对于面向对象编程有更加深刻的体会.Tutorial任务设计一个具有基本功能的画图软件**·包括简单的新建文件,保存,重新绘图等功能**·实现一些基本图形的绘制,包括铅笔和基本形状等,学习橡皮工具的创建**·设计一个合理舒适的UI界面**注明:你可能需要先了解一些关于winform窗体应用程序绘图的基本知识,以及关于GDI+类和结构的知识二.实验环境Windows系统下的visualstudio2017C#窗体应用程序三.
我正在尝试将以下SQL查询转换为ActiveRecord,它正在融化我的大脑。deletefromtablewhereid有什么想法吗?我想做的是限制表中的行数。所以,我想删除少于最近10个条目的所有内容。编辑:通过结合以下几个答案找到了解决方案。Temperature.where('id这给我留下了最新的10个条目。 最佳答案 从您的SQL来看,您似乎想要从表中删除前10条记录。我相信到目前为止的大多数答案都会如此。这里有两个额外的选择:基于MurifoX的版本:Table.where(:id=>Table.order(:id).
我目前正在用Ruby编写一个项目,它使用ActiveRecordgem进行数据库交互,我正在尝试使用ActiveRecord::Base.logger记录所有数据库事件具有以下代码的属性ActiveRecord::Base.logger=Logger.new(File.open('logs/database.log','a'))这适用于迁移等(出于某种原因似乎需要启用日志记录,因为它在禁用时会出现NilClass错误)但是当我尝试运行包含调用ActiveRecord对象的线程守护程序的项目时脚本失败并出现以下错误/System/Library/Frameworks/Ruby.frame
我有一个应用需要发送用户事件邀请。当用户邀请friend(用户)参加事件时,如果尚不存在将用户连接到该事件的新记录,则会创建该记录。我的模型由用户、事件和events_user组成。classEventdefinvite(user_id,*args)user_id.eachdo|u|e=EventsUser.find_or_create_by_event_id_and_user_id(self.id,u)e.save!endendend用法Event.first.invite([1,2,3])我不认为以上是完成我的任务的最有效方法。我设想了一种方法,例如Model.find_or_cr
我在我的rails应用程序中安装了来自github.com的acts_as_versioned插件,但有一段代码我不完全理解,我希望有人能帮我解决这个问题class_eval我知道block内的方法(或任何它是什么)被定义为类内的实例方法,但我在插件的任何地方都找不到定义为常量的CLASS_METHODS,而且我也不确定是什么here,并且有问题的代码从lib/acts_as_versioned.rb的第199行开始。如果有人愿意告诉我这里的内幕,我将不胜感激。谢谢-C 最佳答案 这是一个异端。http://en.wikipedia
在许多ruby类之间共享记录器实例的最佳(正确)方法是什么?现在我只是将记录器创建为全局$logger=Logger.new变量,但我觉得有更好的方法可以在不使用全局变量的情况下执行此操作。如果我有以下内容:moduleFooclassAclassBclassC...classZend在所有类之间共享记录器实例的最佳方式是什么?我是以某种方式在Foo模块中声明/创建记录器还是只是使用全局$logger没问题? 最佳答案 在模块中添加常量:moduleFooLogger=Logger.newclassAclassBclassC..