【问题标题】:how to Create Stored Procedure with database migration in Asp.Net如何在 Asp.Net 中使用数据库迁移创建存储过程
【发布时间】:2018-05-04 17:51:20
【问题描述】:

我正在使用迁移来创建我的数据库模型。我已经通过如下方式覆盖 OnModelCreating 创建了所有表。

protected override void OnModelCreating(System.Data.Entity.DbModelBuilder modelBuilder){
modelBuilder.Entity<AccessLevel>().Map(c =>
{
    .ToTable("AccessLevel", "HelpSystem");
    c.Properties(p => new
        {
            p.Id,
            p.Name,
            p.Description
        });
}).HasKey(x => x.Id);
modelBuilder.Entity<AccessLevel>().Property(x => x.Id).HasColumnAnnotation("DatabaseGenerated", DatabaseGeneratedOption.Identity);}

现在我想像这样为我的数据库创建存储过程。我该怎么做?

这是我的存储过程的 Sql 脚本

CREATE PROCEDURE [HelpSystem].[sp_GetLanguageContentByObject] @InfoObjectId AS BIGINT AS
SELECT  io.Id InfoObjectId ,
io.Name InfoObject ,
cc.Id ComChannelId ,
cc.Name ComChannel ,
cc2.Id ContentCategoryId ,
cc2.Name ContentCategory ,
c.Id ContentId ,
c.Name ContentName ,
c.Description ,
c.AuthorPartyId ,
c.DefaultLanguageId ,
c3.CultureCode DefaultLanguage ,
lc.Id LanguageContentId ,
lc.LanguageId ,
c2.CultureCode Language ,
lc.Title ,
lc.Content FROM    HelpSystem.InfoPointInfoObject ipio
    JOIN HelpSystem.InfoObject io ON io.Id = ipio.InfoObjectId
    JOIN HelpSystem.InfoPointComChannel ipcc ON ipcc.InfoPointId = ipio.InfoPointId
    JOIN HelpSystem.ComChannel cc ON cc.Id = ipcc.ComChannelId
    JOIN HelpSystem.InfoPointContent ipc ON ipc.InfoPointId = ipcc.InfoPointId
    JOIN HelpSystem.Content c ON c.Id = ipc.ContentId
    JOIN HelpSystem.ContentCategory cc2 ON cc2.Id = c.ContentCategoryId
    JOIN HelpSystem.LanguageContent lc ON lc.ContentId = ipc.ContentId
    JOIN Localization.Culture c2 ON lc.LanguageId = c2.Id
    JOIN Localization.Culture c3 ON c.DefaultLanguageId = c3.Id WHERE ipio.InfoObjectId = @InfoObjectId AND (ipio.ExpireDateTime IS NULL OR ipio.ExpireDateTime > GETUTCDATE()) AND (ipcc.ExpiredDateTime IS NULL OR ipcc.ExpiredDateTime > GETUTCDATE()) AND (ipc.ExpiredDateTime IS NULL OR ipc.ExpiredDateTime > GETUTCDATE())

【问题讨论】:

    标签: asp.net entity-framework stored-procedures entity-framework-migrations


    【解决方案1】:

    您可以使用ExecuteSqlCommand API 直接在您的dbcontext 实例上编写存储过程代码,如下所示:

    _dataContext.Database.ExecuteSqlCommand("stored procedure sql");
    

    您还可以使用CreateStoredProcedure 函数直接从DbMigration 文件创建存储过程。 CreateStoredProcedure API

    modelBuilder没有扩展方法可以让您创建存储过程。

    【讨论】:

    • 非常感谢.. 这对我很有帮助.. :)
    猜你喜欢
    • 2014-04-11
    • 2019-03-02
    • 1970-01-01
    • 2016-06-03
    • 2011-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多