2009年6月12日星期五

Entity Framework Tips

by AlexJ : http://blogs.msdn.com/alexj/archive/2009/03/26/index-of-tips.aspx

Hopefully if you're reading this you've noticed that I've started a series of Tips recently. The Tips will mostly apply to Entity Framework.

Seeing as I expect to have a lots of tips, it probably makes sense to have some sort of index. That is what this is post is, as I add a new tip I will add it to this page too.

If you have any suggested topics for tips please let me know by leaving a comment or emailing me directly at Microsoft (Alexj is my alias at Microsoft, and emails at Microsoft are in the form alias@microsoft.com)

Without further ado here are what I have so far:

Tip 25 – How to get Entities by key the easy way

Tip 24 – How to get the ObjectContext from an Entity

Tip 23 – How to fake Enums in EF 4

Tip 22 - How to make Include really Include

Tip 21 – How to use the Single() operator – EF 4.0 only

Tip 20 – How to deal with Fixed Length Keys

Tip 19 – How to use Optimistic Concurrency with the Entity Framework

Tip 18 – How to decide on a lifetime for your ObjectContext

Tip 17 – How to do one step updates with AttachAsModified(..)

Tip 16 – How to mimic .NET 4.0’s ObjectSet today

Tip 15 - How to avoid loading unnecessary Properties

Tip 14 - How to cache Entity Framework Reference Data

Tip 13 - How to Attach an Entity the easy way

Tip 12 - How to choose an Inheritance Strategy

Tip 11 - How to avoid Relationship Span

Tip 10 - How to understand Entity Framework jargon

Tip 9 - How to delete an object without retrieving it

Tip 8 - How to write 'WHERE IN' style queries using LINQ to Entities

Tip 7 - How to fake Foreign Key Properties in .NET 3.5 SP1

Tip 6 - How and when to use eager loading

Tip 5 - How to restrict the types returned from an EF Query

Tip 4 - Conceptual Schema Definition Language Rules

Tip 3 - How to get started with T4

Tip 2 - Entity Framework Books

Tip 1 - How to sort Relationships in the Entity Framework

Enjoy.

2009年6月9日星期二

指定程序运行时的CultureInfo

程序的执行结果有时候跟Culture密切相关,比如在格式化“钱”的时候:

textBoxMoneyWonFiltered.Text = moneyWon.ToString("C");

假设moneyWon的值是24.1,在不同的Culture下显示的结果会不同,比如¥24.1(中国)或者$24.1(美国)。对于一个支持Globalization的程序而言,设定基于应用程序或者线程的Culture是一个非常好的习惯,可以避免一些怪异的情况出现。

对于一个Windows程序,默认使用与系统相同的Culture设定,要修改程序特有的Culture很简单,只需要在启动的时候添加如下代码:

//覆盖系统默认的 zh-CHS
Application.CurrentCulture = new System.Globalization.CultureInfo("en-US");


2009年6月4日星期四

Linq to SQL要复活吗?

自从微软推出了EntityFramework(EF)之后,linq to sql的开发就几乎是中断了,原先开发linq to sql的人转而为EF编写代码,甚至连微软ado.net team的人也推荐开发人员转向EF。.Net社区里基本上认为linq to sql已经被微软枪毙了。这里有一篇关于linq to sql是否已死的讨论:Is Linq to SQL truly dead? .

然而令人意想不到的是,微软数据开发组的Damien Guard却突然给出了一个.net 4.0中Linq to SQL的更新列表,涉及到性能、可用性、稳定性、SqlMetal(从数据库生成dbml文件的工具,也可以生成代码)、类设计器和代码生成等方面的问题。



2009年6月3日星期三

当?: 操作遇到可空值类型(Nullable Value Type)


double? PE = String.IsNullOrEmpty(txtHistoryPE.Text)?
null:Double.Parse(txtHistoryPE.Text)

上面的代码试图达到这样一个目的:如果文本框没有输入,则让变量PE保持为 'null' 。但是这段代码无法通过编译,编译错误如下:
Error: Type of conditional expression cannot be determined because there is no implicit conversion between '' and 'double'
null是引用类型,而double是值类型的,?:操作符无法应用到这两者上面。这可能跟大多数人预想的不太一样,一般会认为如果条件为true,那么PE等于null,否则执行Double.Parse的操作并就将结果赋给PE。翻译成代码就是这样的:

double? PE;

if (string.IsNullOrEmpty(txtHistoryPE.Text))
{
PE = null;
}
else
{
PE = double.Parse(txtHistoryPE.Text);
}

但是实际上如果我们真的把代码写成上面那样,恰恰是没有问题的。然而?:操作在这里需要做转型操作,需要对 'null' 做一个转型,将它转换为Nullable :

double? PE = String.IsNullOrEmpty(txtHistoryPE.Text)
? (double?) null : Double.Parse(txtHistoryPE.Text)

或者使用default关键字,取double?的默认值null:


double? PE = String.IsNullOrEmpty(txtHistoryPE.Text)
? default(double?) : double.Parse(txtHistoryPE.Text)
值得注意的是这里使用的是default(double?) 而非 default(double),这是因为后者返回0.0,而不是我们所需要的null

2009年6月2日星期二

开始在八一中学体育馆打羽毛球了

昨天终于又去打羽毛球了,这次可能会长久一点,因为一次性订了3个月的场地,每个星期一下午6:00~8:00,两个小时。场地是订在八一中学的体育馆,就在我们公司的楼下,下班直接过去,真近,呵呵。昨天是第一次去那个地方打球,跟公司的3个同事,一个同事的老婆,外加一个微软的哥们,没算错的话应该是6个人一起。八一中学场地是30块一个小时,在北京还算是便宜的了,昨天打了2个小时感觉那的场地还不错,不过灯光稍稍有点暗。

昨天打球用的是亮羽的羽毛球,那是我在北邮门口临时买的,以前经常用的诺地波尔502没有买的。亮羽的球好像没有诺地波尔好用,可是价钱差不多。主要是亮羽的球头太软,打一会头都扁了……