首先使用代码自定义CRUD存储过程 [英] Custom CRUD stored procedures using code first

查看:52
本文介绍了首先使用代码自定义CRUD存储过程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在一个新项目中使用EF6代码优先方法,它很精彩,但现在我在如何定制CRUD存储过程方面苦苦挣扎,以便在数据库中计算一些列。 br $>


流程(摘要):

I've been using EF6 code-first approach on a new project, and it has been wonderful, but now I struggling a bit on how to customized the CRUD stored procedures, so that some columns are computed in the database.

Process (abstract):

public abstract class Process
{
    public int Id {get; set}
    public int Name {get; set}
    public DateTime CreateDate{get; private set}
    public DateTime UpdateDate{get; private set}

    public virtual ICollection<ProcessContact> Contacts { get; set; }
}



ProcessContact:


ProcessContact:

public class ProcessContact
{
    public string Name {get; set}
    public string Value {get; set}
}



ProcessA:


ProcessA:

public class ProcessA: Process
{
    public int A {get; set}
}



ProcessB:


ProcessB:

public class ProcessB: Process
{
    public int B {get; set}
}



以上面的4个类为例,我如何在EF6代码优先,自定义生成的存储过程,以便它可以例如:



- >在插入Process时,将Id设置为count(*)+ 1和CreateDate = GETUTCTIME()

- >在更新ProcessContact时,在Process表中设置UpdateDate = GETUTCTIME()


谢谢



我尝试过的事情:



我一直在关注迁移,但说实话,我正在苦苦挣扎。


Given the 4 classes above as an example how can I in EF6 code-first, customized the generated stored procedure so it can for example:

-> On insert of Process, set the Id as count(*)+1 and CreateDate = GETUTCTIME()
-> On update of ProcessContact, set UpdateDate = GETUTCTIME() in the Process table

Thanks

What I have tried:

I´ve been looking at migrations, but to be honest I´m struggling a bit with it.

推荐答案

首先,EF不生成存储过程。一切都是通过普通的SQL语句完成的。



接下来,你想要count(*)+ 1来生成记录ID值是荒谬的,并且性能会下降随着记录数量的增加而增加的时间。记录ID值应该是数据库生成的,不是通过公式编程的。这很容易在EF中完成,因为它只是添加到您正在使用的字段的属性,例如:

First, EF does not generate stored procedures. Everything is done through normal SQL statements.

Next, your idea of "count(*) + 1" to generate a record ID value is ridiculous and degrades in performance over time as the number of records increases. Record ID values should be database generated, not programmed via formula. This is quite easily done in EF as it's just an attribute to add to the field you're using, like:
public abstract class Process
{
    [Key]
    public int Id {get; set}
    public int Name {get; set}
    public DateTime CreateDate{get; private set}
    public DateTime UpdateDate{get; private set}

    public virtual ICollection<ProcessContact> Contacts { get; set; }
}



或按惯例。 EF假设密钥由类名称指定,并附加Id:


or by convention. EF assumes the key is specified by the class name with "Id" appended to it:

public abstract class Process
{
    public int ProcessId {get; set}
    public int Name {get; set}
    public DateTime CreateDate{get; private set}
    public DateTime UpdateDate{get; private set}

    public virtual ICollection<ProcessContact> Contacts { get; set; }
}





更新日期时间字段可以通过几种不同的方式完成。 EF6可以使用存储过程来执行数据库操作。您必须编写存储过程,将它们烘焙到数据库中,然后告诉EF如何使用它。



或者,您可以在C#中设置字段代码并使用EF上下文中的普通SaveChanges将更新发送到数据库。



The Update datetime field can be done in a few different ways. EF6 can use stored procedures to perform database operations. YOU have to write the stored procedures, bake them into the database, and then tell EF how to use it.

Or, you can just set the field in your C# code and send the update to the database using normal SaveChanges in your EF context.


这篇关于首先使用代码自定义CRUD存储过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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