How to Create Function in Code First?

Blaise

Is it possible to use Code First to create a database function in sql server?

Like

CREATE FUNCTION [dbo].[fnIsPaid] ...

Here is how I have worked it out based on ChrFin's suggestion. I marked it as the answer. But let me keep a more detailed record here.

In the Package Manager Console,Add-Migration AddFnIsPaid`.

This will create a DbMigration class prefixed with timestamp.

In this migration class, I have a create script and drop script for Up() and Down() method:

public partial class AddFnIsPaid : DbMigration
{
    public override void Up()
    {
        Sql(_createFunctionScript);
    }

    public override void Down()
    {
        Sql(DropFunctionScript);
    }


    #region SQL Scripts
    private readonly string _createFunctionScript = string.Concat(
            "CREATE FUNCTION [dbo].[fnIsPaid] ",
            "(",
            ...
            ") ",
            "RETURNS bit ",
            "AS ",
            "BEGIN ",
            ...
            "END"
            );

    private const string DropFunctionScript = "DROP FUNCTION [dbo].[fnIsPaid]";  
    #endregion
}
Christoph Fink

No, you can only tell it to map CRUD actions to stored procedures (EF 6+) with e.g.:

modelBuilder
   .Entity<Post>()
   .MapToStoredProcedures();

But you can execute custom SQL in your migration:

public override void Up()
{
    Sql("CREATE FUNCTION [dbo].[fnIsPaid] ...");
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to create a lookup function or VBA code which can return value against only first found result?

From Dev

How to create clustered keys in code first

From Dev

How to create the database when using code first

From Dev

how to create function for this code in objective c

From Dev

How to create a function which eliminates repeated code

From Dev

How to create a function which eliminates repeated code

From Dev

'CREATE FUNCTION' must be the first statement in a query batch. Entity Framework Code First

From Dev

How to create a table corresponding to enum in EF6 Code First?

From Java

How to create index in Entity Framework 6.2 with code first

From Dev

How to correctly create a database using entity framework code first approach

From Dev

How to create the database with Entity Framework Code First and DDD?

From Dev

How to create an expression on a child collection in Entity Framework Code First

From Dev

How to create 2 dbsets with the same type in Entity Framework code first?

From Dev

How to call default constructor first before function code?

From Dev

Code First: Create Tables but not database

From Dev

how to create a code 128 barcode with a function 3 character?

From Dev

How to create a function Javascript pop up box with php code?

From Dev

How to create a function to hold one block of code and then execute?

From Dev

EntityFramework 6.0 CreateDatabaseIfNotExists Code first to create database

From Dev

Code first still trying to create database

From Dev

Create db and tables with EF Code First

From Dev

create text column with Entity Framework Code First

From Dev

Create database with Entity Framework code first

From Dev

How do I force drop and re-create a a selected table in code-first migration?

From Dev

How can I create and use views using EF6 Code First?

From Dev

How to dynamically create db tables with code first in a prism wpf modular app?

From Dev

How to create one-to-one relationship between same table in Entity Framework 6 Code First?

From Dev

How do I create multiple 1:1 foreign key relationships in Entity Framework 6 Code First?

From Dev

How to programatically create Sql Azure database of type Basic/Standard edition through Enity Framework code first

Related Related

  1. 1

    How to create a lookup function or VBA code which can return value against only first found result?

  2. 2

    How to create clustered keys in code first

  3. 3

    How to create the database when using code first

  4. 4

    how to create function for this code in objective c

  5. 5

    How to create a function which eliminates repeated code

  6. 6

    How to create a function which eliminates repeated code

  7. 7

    'CREATE FUNCTION' must be the first statement in a query batch. Entity Framework Code First

  8. 8

    How to create a table corresponding to enum in EF6 Code First?

  9. 9

    How to create index in Entity Framework 6.2 with code first

  10. 10

    How to correctly create a database using entity framework code first approach

  11. 11

    How to create the database with Entity Framework Code First and DDD?

  12. 12

    How to create an expression on a child collection in Entity Framework Code First

  13. 13

    How to create 2 dbsets with the same type in Entity Framework code first?

  14. 14

    How to call default constructor first before function code?

  15. 15

    Code First: Create Tables but not database

  16. 16

    how to create a code 128 barcode with a function 3 character?

  17. 17

    How to create a function Javascript pop up box with php code?

  18. 18

    How to create a function to hold one block of code and then execute?

  19. 19

    EntityFramework 6.0 CreateDatabaseIfNotExists Code first to create database

  20. 20

    Code first still trying to create database

  21. 21

    Create db and tables with EF Code First

  22. 22

    create text column with Entity Framework Code First

  23. 23

    Create database with Entity Framework code first

  24. 24

    How do I force drop and re-create a a selected table in code-first migration?

  25. 25

    How can I create and use views using EF6 Code First?

  26. 26

    How to dynamically create db tables with code first in a prism wpf modular app?

  27. 27

    How to create one-to-one relationship between same table in Entity Framework 6 Code First?

  28. 28

    How do I create multiple 1:1 foreign key relationships in Entity Framework 6 Code First?

  29. 29

    How to programatically create Sql Azure database of type Basic/Standard edition through Enity Framework code first

HotTag

Archive