EF模型优先或code第一种方法? [英] EF Model First or Code First Approach?

查看:497
本文介绍了EF模型优先或code第一种方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这个问题已经被问过无数遍了,因为我已经阅读有关的利弊等话题相当多的职位,但我现在还不能确定哪种方法更适合我。我很新的网页编程和来自SQL数据库管理员/报告写作背景。我决定尝试建立自己的网站,这可能最终不得不在未来的30 -40表,也许更多。

I know this question has been asked numerous times before as I’ve read quite a few posts on the topic about the pros and cons etc but I still can’t decide which approach is right for me. I’m very new to web programming and come from a SQL DB Admin / report writing background. I’ve decided to try build my own website which may end up having 30 -40 tables maybe more in the future.

我看这两种方法,我这样做有利于实体模型的方法,只是因为我喜欢设计简单,我喜欢看到整个模型在我面前,它显示了在一个快照大局。另外,我不是一个强大的程序员,我IM pressed用它生成的POCO的使用的DbContext生成模板,并做了类之间的所有链接的方式。

I’ve looked at both approaches and I do favour Entity Model approach only because I like simplicity of the designer and I like seeing the whole model in front me, it shows the overall picture in one snapshot. Also, me not being a strong programmer I am impressed with the way it generates the POCO’s using the DbContext generator template and does all the linking between the classes.

不过,虽然我喜欢的模型第一种方法,我觉得有一些画背,我不知道他们是实际的缺点还是我只是不知道有足够的了解模型第​​一种方法和code第一种方法因为我还是很新的这一点。

However, although I like the Model First Approach I feel there are some draw backs, I’m not sure if they are actual drawbacks or I just don’t know enough about the model first approach and code first approach as I’m still very new to this.

我很犹豫使用模式第一种方法的原因是:

- 主要是因为我在努力寻找如何使用MVC 3,我用的DbContext找到最好的教程模式第一种方法教程是由朱莉勒曼,但她不包括哥们类,它们是重要的,使用数据的注释,使当你重新生成波苏斯未丢失其他更改。大多数教程MVC 3相关似乎使用了code第一种方法。大多数人说这是因为老师不希望把重点放在E​​F而是在TUTS表现出更多的MVC。我个人认为这是因为微软倡导在别人code首先方法:)

-Mainly because I am struggling to find tutorials on the Model first approach using MVC 3. The best tutorial I’ve found using the DbContext is by Julie Lerman but she doesn’t cover buddy classes which are important for using data annotations and making other changes that aren’t lost when you regenerate the POCOs . Most tutorials MVC 3 related seem to use the Code first approach. Most people say this is because the tutor doesn’t want to focus on EF but rather show more MVC in the tuts. I personally think it’s because Microsoft is championing the Code First methodology over the others :)

- 如果这是很好的做法,创造哥们班为什么我不能找到许多教程表示MVC 3呢?是巴迪班别称视图模型?为什么我找不到微软表示在使用这些好友/视图模型的任何教程与MVC 3?

-If it’s good practice to create buddy classes why can’t I find many tutorials showing this for MVC 3? Are Buddy Classes another name for View Models? And why can’t I find any tutorials by Microsoft showing these buddy/view models in use with MVC 3?

- 我试图做2表之间的基本的1比1的关系。在模型中首先你必须设置每个表同场的身份密钥,而不是在其中一个表中使用FK,当你有3个或多个表通过1对1的关系相互关联的可能会有点混乱。在code首先解决的办法是使用模型构建器和手动设置。我想在MF,你可以进入其中,我并不热衷于做的事XML改变的关系。

-I was trying to do a basic 1 to 1 relationship between 2 tables. In model first you have to set the identity keys of each table to the same field rather than using a FK in one of the tables, which may get a little confusing when you have 3 or more tables linked to each other by 1 to 1 relationships. In code first a way around this is use the model builder and set it up manually. I think in MF you can change the relationship by going into the XML which I am not keen on doing at all.

在code首先的问题 ​​- 更多的支持/帮助

-More support/help on code first problems

我很犹豫使用code第一种方法的原因是:

-I'm新手codeR。

-I’m a novice coder.

- 我看到它变得相当困难的跟踪表和关系作为项目的扩展。

-I see it getting quite difficult to keep track of tables and relationships as the project expands.

- 存在没有模型图,我不得不说,我真的很喜欢这个想法。

-There is no Model diagram and I have to say I really do like this idea.

-Mapping实体通过配置类数据库,我觉得是不可能的:)

-Mapping entities to the database via configuration classes I find impossible :).

-Updating表将需要更改为code和DB。在模型第一只有一个变化,这将自动更新数据库和code模型话说回来,如果你使用的是哥们类,你可能必须更新这些孩子的。

-Updating a table will require a change to the code and the DB. In Model first only one change to the model which will automatically update the DB and Code having said that if you’re using buddy classes you may have to update these as well.

另外现在我看到的人都有些组合$ C C第一和数据库首先接近$,因为你不让code首先生成数据库,但手动创建一个数据库,并使用code第一的API EF得到它。

Also now I see people are somewhat combining Code First and Database first approaches, in that you don’t let Code First generate your database but manually create a database and use code first API to EF to get to it.

我的头在旋转与所有的选项和缺点和优点和缺点。我只是想获得与建立自己的网站,并就采取哪种方法不是思考。 任何人都可以给我自己认为是最好的依据是什么,我说这办法的一些见解和/或他们认为会在未来更主流?

My head is spinning with all the options and drawbacks and pros and cons. I just want to get on with creating my website and not ponder on which approach to take. Can anyone give me some insight on which approach they think is best based on what I’ve said and/or what they think will be more main stream in the future?

非常感谢戴夫

推荐答案

这是过长的问题。你应该打破你的问题分解成多个独立的问题,下一次。

This is too long question. You should break your problem into multiple separate questions next time.

您是一个数据库的家伙,所以你最好的办法是你定义的东西在DB(或VS数据库工具),并更新从数据库模型的增量数据库优先的方式。这会给你一个很大的控制你的数据库,并允许您构建应用程序和数据库增量。为什么我想你会喜欢它:

You are a database guy so the best approach for you is an incremental database-first approach where you define the stuff in DB (or VS Database tools) and update your model from the database. This will give you a big control over your database and allow you building the application and the DB incrementally. Why I think you will like it:

  • 您没有SQL数据库管理员面前 - 你大概知道一些有关数据库以及如何设计他们的表现 - EF不会从此为你做任何事情。 EF不会为你等创建索引。
  • 在30-40桌意味着你不会在一个射击建立模型。你会开始与小模型,并不断地成长吧。一旦你开始做的数据库修改或增加的初始化数据,你不会希望失去这些变化和数据。 code-第一只允许删除整个数据库并从头开始重新创建它。模型首先,请允许逐步建立数据库,但是你需要实体设计数据库发电包并VS 2010 premium或Ultimate($ 5.000- $ 10.000)。
  • You did SQL DB Admin before - you probably know something about DB and how to design them for a performance - EF will not do anything from this for you. EF will not create indexes for you etc.
  • 30-40 tables means that you will not build the model in one shoot. You will start with the small model and continuously grow it. Once you start making changes in DB or adding initialization data you will not want to lose these changes and the data. Code-first allows only deleting the whole database and recreating it from scratch. Model-first allow building the DB incrementally but you need Entity Designer Database Generation Power pack and VS 2010 Premium or Ultimate ($5.000-$10.000).

更多关于<一个href="http://stackoverflow.com/questions/5446316/ef-4-1-$c$c-first-vs-model-database-first/5446587#5446587">differences DB之间的第一,型号第一,code首先。另<一href="http://stackoverflow.com/questions/5086548/ef-poco-$c$c-only-vs-ef-poco-with-entity-data-model/5089398#5089398">answer描述间的差异code-先和设计师的工作。

我会叫这个走最难的路。您将在第一定义数据库,您将使用的DbContext流利的API或数据注解来定义映射。这需要很好的了解EF的和的DbContext API使用默认惯例的映射+理解后面的所有原则。它会给你很好的和明确的控制映射,但它是最工作要做。这绝对是走最难的路。此外,它不应该使用,因为的DbContext API为code优先的方法主要是创建。

I would call this the hardest way to go. You will the define database first and you will use DbContext fluent API or data annotations to define mapping. This requires good understanding of EF and all principles behind the mapping + understanding of default convention used in DbContext API. It will give you nice and explicit control over mapping but it is the most work to do. It is definitely the hardest way to go. Also it is not supposed usage because DbContext API was primarily created for code-first approach.

一旦你开始使用EDMX(实体设计),你有一个选择为使用的DbContext生成T4模板或POCO发电机T4模板。决策是由你 - 您可以使用的DbContext API(第一模板)或ObjectContext的API(第二个模板),这是更好的记录,你也可以使用两个伟大的著作:

Once you start using EDMX (entity designer) you have a choice to use either DbContext Generator T4 template or POCO Generator T4 template. Decision is up to you - you can use either DbContext API (first template) or ObjectContext API (second template) which is much better documented and you can also use two great books:

  • Programming Entity Framework
  • Entity Framework Recepies

我所知道的关于ObjectContext的API是从这些书,作者的博客和实践+反射。

All I know about ObjectContext API is from these books, authors' blogs and practice + Reflector.

的DbContext API目前没有任何一本书。您可以检查一些主要网站获得有关它的信息:

DbContext API doesn't currently have any book. You can check some main sites to get info about it:

  • ADO.NET Team blog
  • Julia Lerman's blog
  • Morteza Manavi's blog

我所知道的关于的DbContext API从这些博客和实践+反射。

All I know about DbContext API is from these blogs and practice + Reflector.

即使您正在使用code首先你仍然可以使用类图来可视化你的类图(这是不一样的EDMX,但它足以为获得大图)。

Even if you are using code first you can still use class diagram to visualize your class diagram (it is not the same as EDMX but it is enough for getting the big picture).

在搜索上的堆栈溢出或 MSDN论坛会给你的大部分问题的答案你将有两个API。

Searching on Stack Overflow or MSDN forum will give you answers on most problems you will have with both APIs.

没有什么特定的使用与MVC 3好友类数据验证的注释被认为是不好的做法,实体框架。一哥们类是用来作为施加于实体元数据持有者单独的类。甲视图模型是用于控制器和视图之间传输数据的类。查看模式应该是按次具体有它自己的验证注解,因为你通常用相同的实体类型工作时,需要在应用程序的不同屏幕不同的验证 - 例如编辑,插入屏幕可以有不同的验证要求

There is nothing specific with using entity framework with MVC 3. Buddy classes for data validation annotations are considered bad practice. A buddy class is separate class used as a metadata holder applied on the entity. A view model is the class used to transfer data between controller and view. View model should be specific per view with its own validation annotations because you usually need different validations in different screens of your application when working with the same entity type - for example edit and insert screen can have different validation requirements.

尽管它不认为是很好的做法,将验证实体是可能的事实 - 你可以<一href="http://stackoverflow.com/questions/5128303/how-to-add-validation-to-my-pocotemplate-classes/5128373#5128373">create哥们类为每个实体手动的,或者你可以尝试修改T4模板直接生成标注为你(嗯,这是硬的)。

Despite the fact it is not considered as the good practice, adding validation to entities is possible - you can either create buddy class for each your entity manually or you can try to modify T4 template to generate annotations for you directly (well this is hard).

是EF需要创建仅在主键上方的一比一的关系。其原因是,EF不支持的唯一密钥/约束。有解决这个并使用数据库中是唯一的键不会改变它没办法。

Yes EF requires creating one-to-one relation only on top of primary keys. The reason is that EF doesn't support unique keys / constraints. There is no way around this and using unique keys in database will not change it.

这篇关于EF模型优先或code第一种方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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