简体   繁体   English

是否可以使用代码优先实体框架在数据库上生成基本的CRUD存储过程?

[英]Is it possible to generate basic CRUD stored procedures on database with code-first entity framework?

I have searched but couldnt find any useful thing about generating automatically basic CRUD stored procedures when creating the database in code first approach. 我已经搜索过,但是找不到以代码优先方式创建数据库时自动生成基本CRUD存储过程的任何有用的信息。 I am wondering is there a way to do this simply or i should create all the CRUD stored procedure by old fashion? 我想知道是否有一种方法可以简单地做到这一点,或者我应该以旧方式创建所有CRUD存储过程?

It is indeed possible to use Migrations with a Code First Database to generate stored procedures for CRUD operations in Entity Framework 6 and beyond. 实际上,可以将“迁移”与“代码优先”数据库一起使用,以生成用于实体框架6及更高版本中CRUD操作的存储过程 Note that this is not possible in this manner with Entity Framework 5 and earlier, though you could still use raw SQL calls. 请注意,尽管您仍然可以使用原始SQL调用,但对于Entity Framework 5和更早版本,这是不可能的。

  1. Define your entity in the normal manner, and add a DbSet for the entity to your DbContext class. 以常规方式定义实体,并将该实体的DbSet添加到DbContext类。
  2. Modify the OnModelCreating method of the DbContext to add the following entry: modelBuilder.Entity<SomeEntity>().MapToStoredProcedures(); 修改OnModelCreating所述的方法DbContext添加以下条目: modelBuilder.Entity<SomeEntity>().MapToStoredProcedures(); .
  3. Using the Package Manager Console, generate a new migration with add-migration , ie add-migration SPGenerate . 使用Package Manager控制台,使用add-migration生成一个新add-migration ,即add-migration SPGenerate
  4. Inspect the Up method of your migration to tweak the Stored Procedures to your liking. 检查迁移的Up方法,以根据自己的喜好调整存储过程。
  5. Commit the migration to the database by issuing update-database in the Package Manager Console. 通过在Package Manager控制台中发出update-database ,将迁移提交到数据库。

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

相关问题 先在Entity Framework代码中自动生成存储过程 - Generate stored procedures automatically in Entity Framework code-first 是否可以在不使用Entity Framework中以代码优先方式使用C#属性的情况下生成数据库字段? - Is it possible to generate database fields without using C# properties in Entity Framework, code-first approach? 实体框架代码优先数据库未更新 - Entity Framework Code-First Database Not Updating 实体框架 5 代码优先不创建数据库 - Entity Framework 5 code-first not creating database 实体框架优先代码和现有数据库 - Entity Framework code-first and existing database 使用代码优先的存储过程? - Using stored procedures in code-first approach? 实体框架代码优先:使用&#39;Update-Database&#39;生成SQL脚本在解析时会产生XML错误 - Entity Framework Code-First: Generate SQL script with 'Update-Database' produces XML error while parsing 首先从Entity Framework 6.1.3调用存储过程 - Call stored procedure from Entity Framework 6.1.3 code-first 如何使用Entity Framework代码优先调用存储过程? - How to call stored procedure using Entity Framework code-first? 实体框架6代码优先的存储过程映射到复杂对象 - Entity Framework 6 code-first stored procedure map to complex objects
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM