技术控

    今日:41| 主题:49369
收藏本版 (1)
最新软件应用技术尽在掌握

[其他] Implementing Seeding, Custom Conventions and Interceptors in EF Core 1.0

[复制链接]
致命的是习惯 发表于 2016-9-30 10:06:08
147 0

立即注册CoLaBug.com会员,免费获得投稿人的专业资料,享用更多功能,玩转个人品牌!

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
This post was written by    Alina Popa, a software engineer on the .NET team.  
  Introduction

  Entity Framework Core (EF Core) is a lightweight and extensible version of the Entity Framework (EF) data access technology which is cross-platform and supports multiple database providers. You can find a comparison of EF Core vs. EF6 under the    Entity Framework documentation.  
  When moving an application from EF6 to EF Core, you may encounter features that existed in EF6 but either are not present or are not yet implemented in EF Core. For many of those features, however, you can implement equivalent functionality.
  Seeding

  With EF6 you can seed a database with initial data by overriding one of the following    Seed()methods:  
  
       
  •               DbMigrationsConfiguration<TContext>.Seed()   
  •               DropCreateDatabaseIfModelChanges<TContext>.Seed()   
  •               DropCreateDatabaseAlways<TContext>.Seed()   
  •               CreateDatabaseIfNotExists<TContext>.Seed()  
  EF Core does not provide similar APIs, and database initializers also no longer exist in EF Core. To seed the database, you would put the database initialization code in the application startup. If you are using migrations, call    context.Database.Migrate(), otherwise use    context.Database.EnsureCreated()/EnsureDeleted().  
  The patterns for seeding the database are discussed in issue    3070in the Entity Framework Core repository on GitHub. The recommended approach is to run the seeding code within a service scope in    Startup.Configure():  
  You can find    herean example of database initialization that uses migrations. The    MusicStoresample also uses this pattern for seeding.  
  Custom Conventions

  In Entity Framework 6 we can create custom configurations of properties and tables by using model-based conventions. For example, the following code in EF6 creates a convention to throw an exception when the column name is longer than 30 characters:
  EF Core does not provide the    IStoreModelConventioninterface; however, we can create this convention by accessing internal services (extending lower level components in EF Core). In the following example we implement a model validator which checks for very long table and column names:  
  Registering and using the ModelValidator created here is explained later in this article.
  Interceptors

  Entity Framework 6 provides the ability to intercept a context using    IDbCommandInterceptor. Interceptors let you to get into the pipeline just before and just after a query or command is sent to the database. Entity Framework Core doesn’t have any interceptors yet. The functionality can be achieved by accessing internal services, in a similar way as the example described above for the model validator. The following example implements    IEntityStateListenerto modify an entity just before it is added to the database:  
  To use the StateListener and the ModelValidator in your context, create a ServiceProvider and use it in OptionsBuilder:
  Notes

  The APIs for accessing internal services may change in the future releases, and there is a risk that the application will break when updated to a new version of Entity Framework Core. The approaches described above should not be considered as long-term solutions, but as workarounds until we have a first class way of achieving the functionality.
  Interceptors and seeding are high on the feature backlog and the Entity Framework team plans to address them in the near future.
友荐云推荐




上一篇:十一生快!数人云开源项目Crane携新版本献礼啦
下一篇:关于Android so文件你所需要了解的
酷辣虫提示酷辣虫禁止发表任何与中华人民共和国法律有抵触的内容!所有内容由用户发布,并不代表酷辣虫的观点,酷辣虫无法对用户发布内容真实性提供任何的保证,请自行验证并承担风险与后果。如您有版权、违规等问题,请通过"联系我们"或"违规举报"告知我们处理。

*滑动验证:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

我要投稿

推荐阅读

扫码访问 @iTTTTT瑞翔 的微博
回页顶回复上一篇下一篇回列表手机版
手机版/CoLaBug.com ( 粤ICP备05003221号 | 文网文[2010]257号 )|网站地图 酷辣虫

© 2001-2016 Comsenz Inc. Design: Dean. DiscuzFans.

返回顶部 返回列表