简体   繁体   English

首先在实体框架代码中更改CUD存储过程的命名约定

[英]Change naming convention of CUD stored procedures in entity framework code first

I am using EF Code First against an existing database (aka "Code Second") 我对现有数据库使用EF Code First(又名“ Code Second”)

Many of the objects will use stored procedures for CUD operations. 许多对象将使用存储过程进行CUD操作。 In code first I can specify that like this: 首先在代码中,我可以这样指定:

modelBuilder.Entity<Widget>
   .MapToStoredProcedures()

This will assume there are stored procedures named Widget_Insert , Widget_Update and Widget_Delete . 这将假定存在名为Widget_InsertWidget_UpdateWidget_Delete存储过程。

If I want to use different names, I can do this: 如果要使用其他名称,可以执行以下操作:

modelBuilder.Entity<Widget>
   .MapToStoredProcedures(s => s.Insert(i => i.HasName("pr_Widget_Merge"))
                                .Update(u => u.HasName("pr_Widget_Merge"))
                                .Delete(d => .HasName("pr_Widget_Delete")));

My question is, using custom conventions, is there a way to tell EF what my stored procedure naming convention is so I don't have to explicitly declare each stored procedure name for each class that I want to use them? 我的问题是,使用自定义约定,是否有办法告诉EF我的存储过程命名约定是什么,因此我不必为要使用它们的每个类显式声明每个存储过程名称?

To change the stored procedure naming convention, you do something like the following: 若要更改存储过程的命名约定,请执行以下操作:

modelBuilder.Types().Configure(c => c.MapToStoredProcedures(p => p.Update(u => u.HasName("pr_" + c.ClrType.Name + "_Merge"))));
modelBuilder.Types().Configure(c => c.MapToStoredProcedures(p => p.Insert(i => i.HasName("pr_" + c.ClrType.Name + "_Merge"))));
modelBuilder.Types().Configure(c => c.MapToStoredProcedures(p => p.Delete(d => d.HasName("pr_" + c.ClrType.Name + "_Delete"))));

With this, all of your entities will be mapped to stored procedures with the naming convention you specified. 这样,您所有的实体都将按照您指定的命名约定映射到存储过程。

Let's say that you don't want all of your entities to be mapped to stored procedures. 假设您不希望将所有实体都映射到存储过程。 You can create an interface and implement it on entities that you want to be mapped to stored procedures. 您可以创建一个接口,并在要映射到存储过程的实体上实现该接口。

public interface IMapToProcs {}

Then, add that interface to the entities you want stored procedures to be mapped. 然后,将该接口添加到要映射存储过程的实体。

public class Widget : IMapToProcs

Now, in your DbContext implementation you can do this: 现在,在您的DbContext实现中,您可以执行以下操作:

    modelBuilder.Types().Where(t => t.IsInstanceOfType(typeof(IMapToProcs))).Configure(c => c.MapToStoredProcedures(p => p.Update(u => u.HasName("pr_" + c.ClrType.Name + "_Merge"))));
    modelBuilder.Types().Where(t => t.IsInstanceOfType(typeof(IMapToProcs))).Configure(c => c.MapToStoredProcedures(p => p.Insert(i => i.HasName("pr_" + c.ClrType.Name + "_Merge"))));
    modelBuilder.Types().Where(t => t.IsInstanceOfType(typeof(IMapToProcs))).Configure(c => c.MapToStoredProcedures(p => p.Delete(d => d.HasName("pr_" + c.ClrType.Name + "_Delete"))));

Finally, this can be reduced to one line: 最后,这可以简化为一行:

    modelBuilder.Types().Where(t => t.IsInstanceOfType(typeof(IMapToProcs)))
        .Configure(c => c.MapToStoredProcedures(p => p.Update(u => u.HasName("pr_" + c.ClrType.Name + "_Merge"))
                                                      .Insert(i => i.HasName("pr_" + c.ClrType.Name + "_Merge"))
                                                      .Delete(d => d.HasName("pr_" + c.ClrType.Name + "_Delete"))));

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

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