我可以将ADO.NET Entity Framework与现有Model一起使用吗?

时间:2022-04-22 00:30:09

I have an existing model for my project. I would like to switch to Entity Framework but I don't want to replace my model with the model generated from EF.

我的项目现有模型。我想切换到实体框架,但我不想用EF生成的模型替换我的模型。

Additionally, my model is very different from my database schema so any chances to use the model generated from EF and do all the required refactoring seems too hard.

另外,我的模型与我的数据库模式非常不同,因此任何使用EF生成的模型并进行所有必需重构的机会似乎都太难了。

Does exist any way to have an existing domain, an existing database and use EF only to add methods to my domain to include the O/RM layer?

是否存在任何方式来拥有现有域,现有数据库并仅使用EF向我的域添加方法以包含O / RM层?

2 个解决方案

#1


In .NET 3.5SP1, then the answer is a big fat "no" I'm afraid. In .NET 4.0 the story looks more promising, with POCO support being introduced (which should hopefully let you use your own objects, although I haven't tried it on the public beta yet).

在.NET 3.5SP1中,答案是一个很大的“不”,我害怕。在.NET 4.0中,故事看起来更有前途,引入了POCO支持(希望能让你使用自己的对象,尽管我还没有在公共测试版上试过它)。

Note that you can do POCO (with your own objects) with LINQ-to-SQL, by using an external mapping file (and supplying a DataContext object) - but the tools don't exactly help you to do this (it wants to use code generation by default).

请注意,您可以使用LINQ-to-SQL通过使用外部映射文件(并提供DataContext对象)来执行POCO(使用您自己的对象) - 但这些工具并不能完全帮助您执行此操作(它希望使用默认情况下生成代码)。

For now, maybe NHibernate (and similar) is your best choice.

现在,也许NHibernate(和类似的)是你最好的选择。

#2


You can download the beta release of Entity Framework if you are an MSDN subscriber. It has support for POCO object which is what you are referring to and for which EF 1.0 came under heavy criticism for.

如果您是MSDN订阅者,则可以下载实体框架的测试版。它支持POCO对象,这是你所指的,并且EF 1.0受到了严厉的批评。

The ADO.NET Design Team has just recently posted @ POCO in the Entity Framework: Part 1 - The Experience which will help you get right on track.

ADO.NET设计团队最近刚刚在实体框架中发布了@ POCO:第1部分 - 体验,它将帮助您正确地走上正轨。

From my experience with using EFPocoAdapter (precursor to EF 4.0) I am happy to suggest EF 4.0.

根据我使用EFPocoAdapter(EF 4.0的前身)的经验,我很乐意为您推荐EF 4.0。

You also get support for some cool n-tier scenarios which means you can pass your objects to another layer, which can modify and return them and you can still persist them to the database. And the other layers don't have to know anything about the entity framework so you get clean separation of concerns.

您还可以获得一些很酷的n层方案的支持,这意味着您可以将对象传递给另一个层,该层可以修改并返回它们,您仍然可以将它们保存到数据库中。而其他层不必了解实体框架,因此您可以清楚地分离关注点。

#1


In .NET 3.5SP1, then the answer is a big fat "no" I'm afraid. In .NET 4.0 the story looks more promising, with POCO support being introduced (which should hopefully let you use your own objects, although I haven't tried it on the public beta yet).

在.NET 3.5SP1中,答案是一个很大的“不”,我害怕。在.NET 4.0中,故事看起来更有前途,引入了POCO支持(希望能让你使用自己的对象,尽管我还没有在公共测试版上试过它)。

Note that you can do POCO (with your own objects) with LINQ-to-SQL, by using an external mapping file (and supplying a DataContext object) - but the tools don't exactly help you to do this (it wants to use code generation by default).

请注意,您可以使用LINQ-to-SQL通过使用外部映射文件(并提供DataContext对象)来执行POCO(使用您自己的对象) - 但这些工具并不能完全帮助您执行此操作(它希望使用默认情况下生成代码)。

For now, maybe NHibernate (and similar) is your best choice.

现在,也许NHibernate(和类似的)是你最好的选择。

#2


You can download the beta release of Entity Framework if you are an MSDN subscriber. It has support for POCO object which is what you are referring to and for which EF 1.0 came under heavy criticism for.

如果您是MSDN订阅者,则可以下载实体框架的测试版。它支持POCO对象,这是你所指的,并且EF 1.0受到了严厉的批评。

The ADO.NET Design Team has just recently posted @ POCO in the Entity Framework: Part 1 - The Experience which will help you get right on track.

ADO.NET设计团队最近刚刚在实体框架中发布了@ POCO:第1部分 - 体验,它将帮助您正确地走上正轨。

From my experience with using EFPocoAdapter (precursor to EF 4.0) I am happy to suggest EF 4.0.

根据我使用EFPocoAdapter(EF 4.0的前身)的经验,我很乐意为您推荐EF 4.0。

You also get support for some cool n-tier scenarios which means you can pass your objects to another layer, which can modify and return them and you can still persist them to the database. And the other layers don't have to know anything about the entity framework so you get clean separation of concerns.

您还可以获得一些很酷的n层方案的支持,这意味着您可以将对象传递给另一个层,该层可以修改并返回它们,您仍然可以将它们保存到数据库中。而其他层不必了解实体框架,因此您可以清楚地分离关注点。