EF 模型优先还是代码优先方法? [英] EF Model First or Code First Approach?

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

问题描述

我知道这个问题之前已经被问过很多次了,因为我已经阅读了很多关于利弊等主题的帖子,但我仍然无法决定哪种方法适合我.我对 Web 编程非常陌生,并且具有 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.

我看过这两种方法,我确实喜欢实体模型方法,因为我喜欢设计器的简单性,我喜欢在我面前看到整个模型,它在一个快照中显示了整体画面.此外,我不是一个强大的程序员,我对它使用 DbContext 生成器模板生成 POCO 并在类之间进行所有链接的方式印象深刻.

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.

然而,虽然我喜欢模型优先方法,但我觉得有一些缺点,我不确定它们是否是实际的缺点,或者我对模型优先方法和代码优先方法的了解不够,因为我'我对此还是很陌生.

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 的最佳教程是 Julie Lerman 的,但她没有涵盖对使用数据注释很重要的伙伴类并进行重新生成 POCO 时不会丢失的其他更改.大多数与 MVC 3 相关的教程似乎都使用代码优先方法.大多数人说这是因为导师不想专注于EF,而是在tuts中展示更多的MVC.我个人认为这是因为 Microsoft 比其他方法更支持 Code First 方法:)

-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 展示这一点的教程?好友类是视图模型的另一个名称吗?为什么我找不到 Microsoft 的任何教程,展示这些与 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 关系相互链接时,这可能会有点混乱.在代码中,一种解决方法是使用模型构建器并手动设置它.我认为在 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.

-更多关于代码优先问题的支持/帮助

-More support/help on code first problems

我对使用代码优先方法犹豫不决的原因是:

-我是新手编码员.

-我发现随着项目的扩展,跟踪表格和关系变得非常困难.

-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 entities to the database via configuration classes I find impossible :).

-更新表将需要更改代码和数据库.在 Model first 中,模型只有一个更改,它将自动更新 DB 和代码,并说过如果您使用伙伴类,您可能也必须更新这些.

-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.

此外,现在我看到人们在某种程度上将 Code First 和 Database first 方法结合起来,因为您不让 Code First 生成您的数据库,而是手动创建一个数据库并使用 Code First API to 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 DB Admin - 您可能对 DB 以及如何设计它们以提高性能有所了解 - EF 不会为您做任何事情.EF 不会为您等创建索引.
  • 30-40 个表意味着您不会一次性构建模型.您将从小型模型开始,并不断扩大它.一旦您开始在 DB 中进行更改或添加初始化数据,您就不会希望丢失这些更改和数据.Code-first 只允许删除整个数据库并从头开始重新创建它.模型优先允许增量构建数据库,但您需要 实体设计器数据库生成能力pack 和 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).

更多关于 DB first 之间的差异,模型优先,代码优先.另一个 答案描述了代码之间的差异-首先与设计师合作.

More about differences between DB first, Model first and Code first. Another answer describes differences between code-first and working with designer.

我认为这是最难走的路.您将首先定义数据库,然后使用 DbContext fluent API 或数据注释来定义映射.这需要很好地理解 EF 和映射背后的所有原则 + 理解 DbContext API 中使用的默认约定.它将为您提供对映射的良好和明确的控制,但这是要做的最多的工作.这绝对是最难走的路.也不应该使用它,因为 DbContext API 主要是为代码优先方法创建的.

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 Generator T4 模板或 POCO Generator 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:

我对 ObjectContext API 的了解仅来自这些书籍、作者的博客和实践 + Reflector.

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:

我对 DbContext API 的了解仅来自这些博客和实践 + Reflector.

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

即使您首先使用代码,您仍然可以使用类图来可视化您的类图(它与 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).

在 Stack Overflow 或 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.

尽管它不被视为良好做法,但可以向实体添加验证 - 您可以 手动为每个实体创建好友类 或者您可以尝试修改 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 模型优先还是代码优先方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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