从LINQ迁移到SQL实体框架4.0 - 提示,文档等 [英] Migrating from LINQ to SQL to Entity Framework 4.0 - Tips, Documentation, etc

查看:125
本文介绍了从LINQ迁移到SQL实体框架4.0 - 提示,文档等的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了英孚早在.NET 3.5 SP1,而我是谁感到沮丧,并决定学习的LINQ to SQL,而不是许多之一。现在我知道EF就是选择的前进道路,加上EF 4.0有一些激动人心的新功能,我想我的应用程序迁移到EF 4.0。

I tried out EF back in .NET 3.5 SP1, and I was one of the many who got frustrated and decided to learn LINQ to SQL instead. Now that I know EF is the "chosen" path forward, plus EF 4.0 has some exciting new features, I'd like to migrate my app to EF 4.0.

任何人都可以认为是对4.0的的L2S迁移专门针对任何好的资源?注:我可以找到大量的博客和相关的从L2S迁移到EF的.NET 3.5的文章,但我觉得其中许多人显然过时和无用的人使用4.0

Can anyone suggest any good resources that are specifically targeted towards 4.0 and L2S migration? NOTE: I can find plenty of blogs and articles related to migrating from L2S to EF on .NET 3.5, but I feel like many of those were obviously dated and unhelpful to someone using 4.0.

我真的想尽可能多深,在引擎罩的东西,我可以得到;我想真的来时却感到我知道 EF 4.0的方式,我现在知道L2S 3.5。

I'd really like as much deep, under-the-hood stuff as I can get; I want to really come away feeling like I know EF 4.0 the way I currently know L2S 3.5.

TIA!

推荐答案

我这样做很有型的转换和FWIW的负荷,我会说有大于分歧更多的相似之处。我不认为有任何明确的文档,这将让你感觉像在EF4的专家,超越了东西,是已经在那里......

I have done loads of this very type of conversion and FWIW, I would say there are more similarities than differences. I don't think there is any definitive documentation that will make you feel like an expert in EF4, beyond the stuff that is already out there...

<一个href="http://msdn.microsoft.com/en-us/library/ex6y04yf(VS.100).aspx">http://msdn.microsoft.com/en-us/library/ex6y04yf(VS.100).aspx

我可以给你是比较明显的陷阱。具体来说,LINQ2SQL想在业务层和数据层多了很多明显的结合。这真的推你创建你自己的部分类。我可以去和有关的方式,但最具体的原因是这样的一对一映射器将创造一切关系公共父母和孩子的属性。

What I can give you are the more obvious "gotchas." Specifically, Linq2Sql wanted to combine the business layer and the data layer a lot more obviously. It really pushed you to create your own partial classes. I could go on and on about way, but the most specific reason is the way the one-to-one mapper will create public parent and child properties for all relations.

如果您尝试使用对这个模型任何类型的系列化,你会喜欢遇到循环引用的问题从父串行器移动到一个孩子,再回到父母的LINQ2SQL序列化行为自动包含在所有儿童的曲线图。这也可能是真的很烦人,当你试图抓住一个客户记录,检查名称属性,并自动获取包括图中的所有相关的订单记录。您可以设置这些父母和孩子的导航性能是不是公开或内部,这意味着,如果你想与他们接触,但不希望序列化器自动创建循环引用,则pretty的多少要访问他们在部分类。

If you attempt to use any type of serialization against this model, you will like run into circular reference problems as a serializer moves from a parent to a child and then back to the parent as the Linq2Sql serialization behavior automatically includes all children in the graph. This can also be really annoying when you try to grab a customer record to check the "Name" property and automatically get all the related order records included in the graph. You can set these parent and child navigation properties to be either "public" or "internal" which means if you want access to them, but don't want the serializers to automatically create circular references, you pretty much have to access them in partial classes.

一旦你开始下局部类路径中,你一般只是继续的格局,最终将开始添加辅助方法来访问您的数据到您的个性化实体类。此外,在LINQ2SQL的DataContext更加轻巧,你经常会发现使用某种Singleton模式或库模式为他们的环境的人。你不认为这是很多在所有与EF 3.5 / 4。

Once you start down the partial class path you generally just continue the pattern and eventually will start to add helper methods for accessing your data into your individual entity classes. Also, with the Linq2Sql DataContext being more lightweight, you often find people using some kind of Singleton pattern or Repository pattern for their context. You don't see this as much at all with EF 3.5 / 4.

因此​​,让我们说你有类似描述的一些环境,并要开始转换。那么,你需要找出当你的DataContext将是创建/销毁......有些人会刚开始每一个业务层方法与使用()语句,并让上下文pretty的很多活的生命周期方法。显然,这意味着你可以进入这需要增加.ToList()或一些其他的扩展方法来对你的问题的目的有些发毛情况下,你可以有一个完全内存收集你的对象传递给孩子的方法或什么,甚至那么你可以有问题,尝试在一个环境,他们原本不是从中检索更新的实体。

So let's say you have some environment similar to the one described and you want to start converting. Well, you need to find out when your DataContext is going to be create/destroyed...some people will just start each Business Layer method with a using() statement and let the context pretty much live for the lifetime of the method. Obviously this means you can get into some hairy situations that require adding .ToList() or some other extension method to the ends of your questions you can have a fully in-memory collection of your objects to pass to a child method or whatever and even then you can have problems with attempting to update entities on a context that they weren't originally retrieved from.

你还需要弄清楚如何在多少你LINQ2SQL纳入部分班列到另一层的BusinessLogic,如果它没有明确的数据运算处理。为你找出当你需要/不需要你的情况下这会不会是无痛的,但它是最好的。

You'll also need to figure out how to much of the BusinessLogic incorporated in your Linq2Sql partial classes out into another layer if it doesn't deal explicitly with the data operations. This will not be painless as you figure out when you need/don't need your context, but it is for the best..

接下来,您将要处理的对象图的情况。因为在这样的延迟加载的作品的差异(他们提出这个配置在EF 4.0使它更像LINQ2SQL对于那些谁想要它),你可能会需要从LINQ2SQL检查子对象的图中的任何暗示用途实现并验证它目前并不需要一个明确的.INCLUDE()或.Load()来获得在图中的子对象。

Next, you will want to deal with the object graph situation. Because of the difference in the way lazy-loading works (they made this configurable in EF 4.0 to make it behave more like Linq2Sql for those who wanted it) you will probably need to check any implied uses of child objects in the graph from your Linq2Sql implementation and verify that it doesn't now require an explicit .Include() or a .Load() to get the child objects in the graph.

最后,你需要决定,一般一个序列化的解决方案。默认情况下,所产生的,作为一个EF模型工大与WCF的一部分,但不是在所有伟大的XmlSerializer的的DataContracts和DataMember属性用于像旧的.asmx Web服务。即使在这种情况下,你也许能够逃脱它,如果你永远不需要通过线路序列化的子对象。由于通常情况并非如此,你会想要移动到WCF,如果你有更多的SOA,这将增加创造了机会,但头痛的一个全新的主机。

Finally, you will need to decide on a serialization solution in general. By default, the DataContracts and DataMember attributes that are generated as part of an EF model work great with WCF, but not at all great with the XmlSerializer used for things like old .asmx WebServices. Even in this circumstance you might be able to get away with it if you never need to serialize child objects over the wire. Since that usually isn't the case, you are going to want to move to WCF if you have a more SOA, which will add a whole new host of opportunies, yet headaches.

为了应对部分类情况,以及沉重的DataContext甚至是序列化的问题,有许多可用的EF 4.0新的code-生成模板。在POCO实体模板有很多人兴奋,因为它创造POCO类,正如你所期望的(麻烦的是,不包括任何类或WCF等等等等成员属性)。此外,自跟踪实体模型$ P ​​$ ptty的多少解决了环境问题,因为你可以围绕通过你的实体,让他们记得,他们是如何更新,所以你可以创建/更自由处置的环境(如LINQ2SQL )。另一个好处是,这个模板是去到模板WCF或任何建立在WCF像RIA服务或WCF数据服务,所以他们有[DataContract],[数据成员]和[KnownType]已经想通了的属性。

In order to deal with the partial classes situation, and the hefty DataContext and even the serialization issues, there are a number of new code-generation templates available with EF 4.0. The POCO-Entity template has a lot of people excited as it creates POCO classes, just as you'd expect (the trouble is that excludes any class or member attributes for WCF etc etc). Also, the Self-Tracking Entities model pretty much solves the context issue, because you can pass your entities around and let them remember when and how they were updated, so you can create/dispose your contexts much more freely (like Linq2Sql). As another bonus, this template is the go-to template for WCF or anything that builds on WCF like RIA Services or WCF Data Services, so they have the [DataContract], [DataMember], and [KnownType] attributes already figured out.

下面是一个链接到POCO模板(不包括开箱即用的): (编辑:我不能发布两个超级链接,所以只需访问的VisualStudio库网站,搜索ADO.NET C#POCO实体发生器)

Here is a link to the POCO template (not included out of the box): ( I cannot post two hyperlinks, so just visit the visualstudio gallery website and search for "ADO.NET C# POCO Entity Generator")

请务必阅读上的ADO.net团队博客实现此链接。你可能会喜欢你的分裂情况和您的企业合并形成单独的项目/集的位,如果你掉进web服务与WCF服务类别。在添加服务引用...代理生成不做命名空间中使用,以同样的方式添加Web引用...,所以你可能想实际引用你的实体类组件的客户端应用程序,所以你可以排除从参考图书馆或任何您的服务引用,这样你就不会得到很多从使用相同的EF模型中的多个服务暧昧参考,并揭露那些实体类型...

Be sure to read the link on the ADO.net team blog about implementing this. You might like the bit about splitting your context and your entities into separate projects/assemblies if you fall into the WebService vs. WCF Service category. The "Add Service Reference..." proxy generation doesn't do namespaces the same way "Add Web Reference..." used to, so you might like to actually reference your entity class assembly in your client app so you can "exclude types from reference libraries" or whatever on your service references so you don't get a lot of ambiguous references from multiple services which use the same EF model and expose those entities...

我知道这是漫长而散漫,但这些小陷阱是waaay更大的问题对我来说比记住要使用,而不是context.EntityCollection.InsertOnSubmit()和context.SaveChanges context.EntityCollection.AddObject()(),而不是context.SubmitChanges()的...

I know this is long and rambling, but these little gotchas were waaay more of an issue for me than remembering to use context.EntityCollection.AddObject() instead of context.EntityCollection.InsertOnSubmit() and context.SaveChanges() instead of context.SubmitChanges()...

这篇关于从LINQ迁移到SQL实体框架4.0 - 提示,文档等的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆