Note On <Programming Entity Framework 2nd Edition> -02: How to Use SqlExpress Profiler 2.0
来源:互联网 发布:淘宝商城权威检测 编辑:程序博客网 时间:2024/05/23 12:28
Chapter 3: Querying Entity Data Models
Example 3-3. Querying with Entity SQL
The example code of Entity SQL in this chapter in page 57 doesn't work in Entity Framework 6.0. And in EF5.0, you need to change it in this way:
var queryString = "SELECT VALUE c " +"FROM PROGRAMMINGEFDB1Entities.Contact AS c " +"WHERE c.FirstName='Robert'";ObjectQuery<Contact> contacts = ((IObjectContextAdapter)context).ObjectContext.CreateQuery<Contact>(queryString);foreach (var contact in contacts){Console.WriteLine("{0} {1}",contact.FirstName.Trim(),contact.LastName);}
If you compose the query with multiple items(Page 59), like this:
var queryString = "SELECT c.LastName, c.Title " +"FROM PROGRAMMINGEFDB1Entities.Contact AS c " +"WHERE c.FirstName='Robert'";
The author didn't elaborate how to invoke the methods to make it work immediately. You couldn't find the answer until Chapter 5:
ObjectQuery<Contact> contacts = this.objEntity.CreateQuery<Contact>(query);
Example 3-6. Entity SQL query builder method
And the example in page 65 which demonstrating Entity SQL query builder:
var contacts = context.Contacts.Where("it.FirstName = 'Robert'").OrderBy("it.LastName");
This also doesn't work in EF5.0+ any more. In order to make it work, you need to do it in this way:
ObjectContext objectContext = ((IObjectContextAdapter)context).ObjectContext;ObjectSet<Contact> objectContact = objectContext.CreateObjectSet<Contact>("Contact");var contacts = objectContact.Where("it.FirstName = 'Robert'").OrderBy("it.LastName");
Chapter 4: Exploring LINQ to Entities in Greater Depth
Example 4-3. Anonymous types as properties
And, in order to prevent Exception when running the sample code at Page 83: (because self referencing loop is detected when serializing the entities to JSON format)
var contacts = from c in context.Contactswhere c.FirstName == "Robert"let foo = new {ContactName = new {c.Title, c.LastName, c.FirstName},c.Addresses}orderby foo.ContactName.LastNameselect foo;
You need to add the line to WebApiConfig.cs
config.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
Example 4-30. Filtering related data in a query using projections
Sample code at page 99 fails in LINQPad:
from pr in ProductRegistrationsselect new { pr, pr.RegistrationApproveRecords.Where(ra => ra.status = 1) }
Error:
Invalid anonymous type member declarator. Anonymous type members must be declared with a member assignment, simple name or member access.
Chapter 5: Exploring Entity SQL in Greater Depth
Example 5-10. An aggregate in a subquery
string query = "SELECT e.invoice_number, (SELECT VALUE MAX(a.status) FROM e.RegistrationApproveRecords as a) " +"FROM samsunginternalEntities.ProductRegistrations AS e ";ObjectQuery<DbDataRecord> records = this.objEntity.CreateQuery<DbDataRecord>(query);foreach (DbDataRecord record in records){Trace.WriteLine("model_code: " + record[0]);var statusList = record[1] as List<short?>;//var statusList = (record[1] as List<short?>).Where(o=>o.HasValue).Select(o=>((int)o)).ToList();foreach (var x in statusList){Trace.WriteLine("x: " + x);}}
Chapter 6:Modifying Entities and Saving Changes
Install and use SQL Server Profiler:
Go to its official download page to download the package, its called Express Profiler now, and its current version is 2.0: https://expressprofiler.codeplex.com/
After unzipping the file, you will see:
Run ExpressProfiler.EcosystemInstall.msi. And then you are ready, run the exe under /Standalone.
Now you see it:
Click File->Start Trace->Run/Run with filters:
And you can execute queries from Entity Framework against the SQL Server:
- Note On <Programming Entity Framework 2nd Edition> -02: How to Use SqlExpress Profiler 2.0
- Note On <Programming Entity Framework 2nd Edition> -01: How to Use LINQPad 4.5
- Note On <Programming Entity Framework 2nd Edition> -03
- Note On <Programming Entity Framework 2nd Edition> -04
- Note On <Programming Entity Framework 2nd Edition> -05
- Concrete Mathematics(2nd Edition) - A Note on Notation
- Windows Forms 2.0 Programming (2nd Edition)
- Windows Forms 2.0 Programming (2nd Edition)
- How To: Use CLR Profiler
- How To: Use CLR Profiler
- Programming C# (2nd Edition)
- Programming C# (2nd Edition)
- 《Programming Pearls》 2nd Edition
- programming php,2nd Edition
- Windows Azure Platform 2nd Edition Note
- Essentials of Programming Languages - 2nd Edition
- Programming Jakarta Struts, 2nd Edition
- Programming .NET Components, 2nd Edition [ILLUSTRATED]
- android中图片压缩以及图片旋转的方法
- 设置UFT环境变量值
- [CSS] 拋棄原生checkbox用CSS3自己簡單動手做
- linux下nginx与tomcat整合
- make 调试和传参
- Note On <Programming Entity Framework 2nd Edition> -02: How to Use SqlExpress Profiler 2.0
- armv6 armv7 armv7s架构的区别
- 《深入理解Java虚拟机》读书笔记1——Java内存区域与内存溢出异常
- 天声人語 20141208
- 第一章:什么是Cocos2d-x
- fork函数的详细解释
- 前端开发到底需要掌握哪些知识?
- sublime Text 3实用功能和常用快捷键收集
- 《用户体验要素》笔记