Code First Migrations and Stored Procedures

前端 未结 4 881
青春惊慌失措
青春惊慌失措 2020-12-05 10:19

I have just created a database and done my first migration (just a simple table add). Now I want to add some stored procedures which I have just added by writing the sql an

4条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-05 10:49

    namespace QuickProject.Migrations
    {
        using System;
        using System.Data.Entity.Migrations;
    
    
    public partial class CreateStoredProcedure_GellAllAgents : DbMigration
    {
        public override void Up()
        {
            CreateStoredProcedure("dbo.GellAllAgents", c => new
            {
                DisplayLength = c.Int(10),
                DisplayStart = c.Int(0),
                UserName = c.String(maxLength: 255, defaultValueSql: "NULL"),
                FullName = c.String(maxLength: 255, defaultValueSql: "NULL"),
                PhoneNumber = c.String(maxLength: 255, defaultValueSql: "NULL"),
                LocationDescription = c.String(maxLength: 255, defaultValueSql: "NULL"),
                AgentStatusId = c.Int(defaultValueSql: "NULL"),
                AgentTypeId = c.Int(defaultValueSql: "NULL")
            }, StoredProcedureBody);
        }
    
        public override void Down()
        {
            DropStoredProcedure("dbo.GellAllAgents");
        }
    
    
        private const string StoredProcedureBody = @"
    Declare @FirstRec int, @LastRec int
    Set @FirstRec = @DisplayStart;
    Set @LastRec = @DisplayStart + @DisplayLength;
    
    With CTE_AspNetUsers as
    (
         Select ROW_NUMBER() over (order by AspNetUsers.Id) as RowNum,
             COUNT(*) over() as TotalCount, AspNetUsers.Id, AspNetUsers.FullName, AspNetUsers.UserName, AspNetUsers.PhoneNumber, Locations.Desciption as LocationDescription, Cities.Name as LocationCity, AgentStatus.Name as AgentStatusName, AgentTypes.Name as AgentTypeName
             from AspNetUsers
         join Locations on AspNetUsers.LocationId = Locations.id
         join Cities on Locations.CityId = Cities.Id
         join AgentStatus on AspNetUsers.AgentStatusId = AgentStatus.Id
         join AgentTypes on AspNetUsers.AgentTypeId = AgentTypes.Id
         where (Discriminator = 'Agent' 
             and (@UserName is null or UserName like '%' + @UserName + '%')
             and (@FullName is null or FullName like '%' + @FullName + '%')
             and (@PhoneNumber is null or PhoneNumber like '%' + @PhoneNumber + '%')
             and (@LocationDescription is null or  @LocationDescription like '%' + (select Cities.Name from Cities where Locations.CityId = Cities.Id) + '%' or  @LocationDescription like '%' + Desciption + '%')
             and (@AgentStatusId is null or AgentStatusId = @AgentStatusId)
             and (@AgentTypeId is null or AgentTypeId = @AgentTypeId)
         )
         group by AspNetUsers.Id, AspNetUsers.FullName,AspNetUsers.UserName, AspNetUsers.PhoneNumber, Locations.Desciption, Cities.Name, AgentStatus.Name, AgentTypes.Name
    )
    Select *
    from CTE_AspNetUsers
    where RowNum > @FirstRec and RowNum <= @LastRec
    ";
    }
    }
    

    Result, When you view/modify the SP in SQL server, that's why it shows "ALTER PROCEDURE"

提交回复
热议问题