任何流利的API教程,使用EF数据库 - 第一种方法来解释主题? [英] Any Fluent API tutorials that use EF Database-First approach to explain the subject?

查看:69
本文介绍了任何流利的API教程,使用EF数据库 - 第一种方法来解释主题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有很多关于Fluent API的教程,但是他们都使用Entity Framework Code-First代码示例来解释它。由于我不知道Code-First,您是否知道使用EF数据库优先方法解释主题的任何Fluent API教程?



谢谢

解决方案

没有任何教程将解释Fluent API与Database-First方法,因为Fluent API仅用于Code-First方法。如果要通过Database-First创建您的模型,则不需要Fluent API。



Fluent API(连同Code-First数据注释和约定)是一个在代码中定义模型细节的工具,例如字符串长度,如果属性是必需的,或者是关系类型 - 多对多,一对多等。使用数据库优先或模型优先时,EDMX文件具有相同的目的 - 它包含您的模型的所有细节和映射定义。流畅的API(+数据注释和约定)仅在使用Code-First时替换EDMX文件。



如果通过Database-First或Model-First创建模型,将有一个表示您的模型的EDMX文件。您可以将T4 DbContext生成器应用于此EDMX文件。生成的文件有两个特征与Code-First不同:




  • 生成的连接字符串包含引用EDMX元数据的部分这将嵌入到您的程序集中:

      connectionString =metadata = res://*/Model.csdl 
    | res://*/Model.ssdl
    | res://*/Model.msl;
    ...


  • 生成的上下文 DbContext 将覆盖 OnModelCreating 方法只是抛出一个异常:

      protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
    throw new UnintentionalCodeFirstException();
    }




只要你将元数据部分留在连接字符串中,EF将不会在此方法中调用 OnModelCreating 或Fluent API中的任何代码。元数据部分告诉EF,您的模型是DB-或Model-First,元数据在嵌入式EDMX中定义,而不是Fluent API。



但是,您可以从连接字符串中删除元数据部分,删除 UnintentionalCodeFirstException ,并使用Fluent API在 OnModelCreating 中编写代码。您可以按照此过程通过Database-First创建初始模型,然后基于此初始模型进行开发,以通过Code-First进行进一步开发。



此时您不是使用数据库优先工作,但代码优先,而您阅读的有关Fluent API的所有内容对您有效。


There are many tutorials on Fluent API, but they all explain it using Entity Framework Code-First code examples. Since I don't know Code-First, do you know of any Fluent API tutorials that would explain the subject using EF Database-First approach?

Thank you

解决方案

There are no tutorials which would explain the Fluent API together with Database-First approach because Fluent API is made only for Code-First approach. You don't need the Fluent API if you want to create your model via Database-First.

Fluent API (together with Code-First data annotations and conventions) is a tool to define model details in code, such as string length, if a property is required or the type of relationship - many-to-many, one-to-many, etc. When using Database-First or Model-First the EDMX file has the same purpose - it contains all the details and mapping definitions of your model. Fluent API (+ data annotations and conventions) replaces the EDMX file only when using Code-First.

If you create the model via Database-First or Model-First, you will have an EDMX file representing your model. You can apply the T4 DbContext Generator to this EDMX file. The generated files have two characteristics being different from Code-First:

  • The generated connection string contains a section refering to the EDMX metadata which will be embedded into your assembly:

    connectionString="metadata=res://*/Model.csdl
                              |res://*/Model.ssdl
                              |res://*/Model.msl;
                      ..."
    

  • The generated context DbContext will have an overridden OnModelCreating method which just throws an exception:

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        throw new UnintentionalCodeFirstException();
    }
    

As long as you leave the metadata section in the connection string, EF won't even call OnModelCreating or any code in Fluent API in this method. The metadata section tells EF that your model is DB- or Model-First and that the metadata are defined in the embedded EDMX and not in Fluent API.

However, you can remove the metadata section from the connection string, remove the UnintentionalCodeFirstException and write code with Fluent API in OnModelCreating. You can follow this procedure to create an initial model via Database-First and then build on this initial model for further development with Code-First.

At this point you are not working anymore with Database-First, but Code-First instead and everything you read about Fluent API is valid for you.

这篇关于任何流利的API教程,使用EF数据库 - 第一种方法来解释主题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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