简体   繁体   English

什么是实体框架流利的 api?

[英]What is Entity Framework fluent api?

I keep hearing about the Entity Framework fluent-api but I am struggling to find a good reference on this.我不断听到有关实体框架 fluent-api 的消息,但我正在努力寻找一个好的参考。 What is it?它是什么?

We use the entity framework and the modeling tool provided.我们使用提供的实体框架和建模工具。 Is that all that is?这就是全部吗? Or is it something different?还是有什么不同?

Similarly, if it's not too broad a question, what is POCO?同样,如果不是太宽泛的问题,那么 POCO 是什么? I know it stands for Plain Old CLR Objects, but what does that mean to me as somebody who uses EF already with the designer model tool?我知道它代表普通旧 CLR 对象,但是对于已经使用设计器 model 工具的人来说,这对我意味着什么? If that question is too vague then please ignore it.如果这个问题太模糊,请忽略它。 I'm just learning here and any information you are willing to provide is helpful.我只是在这里学习,您愿意提供的任何信息都是有帮助的。

Entity Framework 4.1 introduces the code first approach of writing database models. Entity Framework 4.1 引入了编写数据库模型的代码优先方法。 This is also called POCO (Plain Old CLR Objects).这也称为 POCO(普通旧 CLR 对象)。 The idea is that you can build your database from these classes, rather then building the database first and creating a model from that.这个想法是您可以从这些类构建数据库,而不是先构建数据库并从中创建 model。

There are tons of good blog articles and MSDN documentation on this.有大量关于此的优秀博客文章和 MSDN 文档。 A good place to start would be一个好的起点是

http://blogs.msdn.com/b/adonet/archive/2010/12/14/ef-feature-ctp5-fluent-api-samples.aspx http://blogs.msdn.com/b/adonet/archive/2010/12/14/ef-feature-ctp5-fluent-api-samples.aspx

http://weblogs.asp.net/scottgu/archive/2010/12/08/announcing-entity-framework-code-first-ctp5-release.aspx http://weblogs.asp.net/scottgu/archive/2010/12/08/announcing-entity-framework-code-first-ctp5-release.aspx

http://weblogs.asp.net/manavi/archive/2011/03/27/associations-in-ef-4-1-code-first-part-1-introduction-and-basic-concepts.aspx http://weblogs.asp.net/manavi/archive/2011/03/27/associations-in-ef-4-1-code-first-part-1-introduction-and-basic-concepts.aspx

Regards the fluent API, this is basically using the EF classes to build your database eg:关于流利的 API,这基本上是使用 EF 类来构建您的数据库,例如:

modelBuilder.Entity<Category>().HasKey(c => c.CategoryCode);

So you're manually stating that the Category table has a primary key named `CategoryCode'.因此,您手动声明Category表有一个名为“CategoryCode”的主键。 You can also declare the PK like this:您还可以像这样声明 PK:

public class Category
{
    [Key]    
    public int CategoryCode { get; set;}
}

The [Key] attribute comes from Data Annotations [Key]属性来自Data Annotations

POCO stands for Plain Old CLR Object. POCO 代表普通旧 CLR Object。

Article on Fluent API. Fluent API上的文章。

You can also check the Code First Fluent API section on MSDN here http://msdn.microsoft.com/en-us/library/hh295844您还可以在此处查看 MSDN 上的 Code First Fluent API 部分http://msdn.microsoft.com/en-us/library/hh295844

Responding to your POCO question: in the application I'm currently working on I'm using POCO's to pass data to my Silverlight front end (EF just wasn't cutting it).回答您的 POCO 问题:在我目前正在处理的应用程序中,我正在使用 POCO 将数据传递到我的 Silverlight 前端(EF 只是没有削减它)。 Essentially, I use the entities that the EF modeler created, massage them into a serializable-friendly version, and then send them back and forth over the wire.本质上,我使用 EF 建模器创建的实体,将它们按摩成可序列化友好的版本,然后通过网络来回发送它们。 POCO's are there to provide a layer of abstraction when needed. POCO 可以在需要时提供抽象层。 I think of it as an adaption of the DAO pattern to serialization, instead of using it for DB access like the DAO pattern normally does.我认为它是 DAO 模式对序列化的一种改编,而不是像 DAO 模式通常那样将其用于 DB 访问。

See the paragraph on POCO classes in http://www.asp.net/entity-framework/tutorials/creating-an-entity-framework-data-model-for-an-asp-net-mvc-application请参阅http 中关于 POCO 类的段落://www.asp.net/entity-framework/tutorials/creating-an-entity-framework-data-model-for-an-asp-net-mvc-application

Essentially, in the context of EF, POCO classes are entity classes that do not inherit from the Entity Framework EntityObject class (which is what you get by default in Database First or Model First).本质上,在 EF 的上下文中,POCO 类是不从实体框架 EntityObject class 继承的实体类(这是您在 Database First 或 Model First 中默认获得的)。 As one of the other answers mentions, this makes it easier to serialize the objects, but also some development and automated testing methodologies prefer to work with objects that have no reference to the Entity Framework.正如其他答案之一所提到的,这使得序列化对象变得更加容易,而且一些开发和自动化测试方法更喜欢使用不引用实体框架的对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM