EntityFramework核心ClassLibrary项目从SQL无法正常工作

时间:2018-11-16 16:15:47

标签: .net-core entity-framework-core entity-framework-core-2.1

步骤:1 创建了一个类库项目

步骤:2

        var activity = turnContext.Activity;
        // Download the activities from the Transcript (blob store) and send them over to the channel when a request to upload history arrives.
        // This could be an event or a special message acctivity as above.
        var connectorClient = turnContext.TurnState.Get<ConnectorClient>(typeof(IConnectorClient).FullName);

        // Get all the message type activities from the Transcript.
        //string continuationToken = null;
        var count = 0;
       // do
        try
        {
            var pagedTranscript = await _transcriptStore.GetTranscriptActivitiesAsync(activity.ChannelId, activity.Conversation.Id);

            var activities = pagedTranscript.Items
                .Where(a => a.Type == ActivityTypes.Message)
                .Select(ia => (Activity)ia)
                .ToList();

            // This count takes only 20 activities max
            var activityCount = activities.Count;

            var user = await _accessors.UserProfile.GetAsync(turnContext);

            var transcriptContents = new StringBuilder();


            foreach (var transcript1 in pagedTranscript.Items.Where(i => i.Type == ActivityTypes.Message))
            {
                transcriptContents.AppendLine("<div><b>(" + transcript1.Timestamp + ") " + transcript1.From.Name + "</b>: " + transcript1.AsMessageActivity().Text + "</div>");
            }

            emailContent = transcriptContents.ToString();

            var transcript = new Transcript(activities);

            await connectorClient.Conversations.SendConversationHistoryAsync(activity.Conversation.Id, transcript, cancellationToken: cancellationToken);

           // continuationToken = pagedTranscript.ContinuationToken;
        }catch(Exception ex)
        // while (continuationToken != null);
        {
            await HandleDialogExceptionsAsync(sc, ex);
        }

步骤:3

public class DbContextFactory : IDesignTimeDbContextFactory<MyContext>
{
    public MyContext CreateDbContext(string[] args)
    {
        var optionsBuilder = new DbContextOptionsBuilder<MyContext>();

        return new MyContext(optionsBuilder.Options);
    }
}

步骤:4 ==>为SearchResult创建了模型。

public class MyContext : DbContext
{
    public MyContext()
    {
    }

    public MyContext(DbContextOptions<MyContext> options)
        : base(options)
    {
    }

    //Proc Results
    public DbSet<SearchResult> SearchResults { get; set; }
}

步骤:5 ==>创建了存储库

public class SearchResult
{
    [Key]
    public int Id { get; set; }
    [Required]
    public string Title { get; set; }
}

步骤:6 ==>在Asp.Net webapi核心项目中,添加了对类库项目的引用。

public class SearchRepository  : BaseRepository
{
    public SearchRepository(MyContext context) : base(context)
    {

    }

    public async Task<IEnumerable<SearchResult>> GetSearchByIdAsync(int id)
    {

        SqlParameter param = new SqlParameter() {
                            ParameterName = "@Id",
                            SqlDbType =  System.Data.SqlDbType.Int,
                            Direction = System.Data.ParameterDirection.Input,
                            Value = id
        };

        var result = await _ocmsContext.SearchResults.FromSql("ProcName @Id", param).ToListAsync();
        return result;
    }
}

完成所有上述代码后,在步骤5中,对于FromSql,我得到了编译错误(下面给出了错误消息)

错误CS1061'DbSet'不包含'FromSql'的定义,并且找不到可以接受的扩展方法'FromSql'接受类型为'DbSet'的第一个参数(您是否缺少using指令或程序集引用?)

注意,如果我将SearchRepository移至Web.Api Core项目,则FromSql没有显示任何错误,它工作正常。

请指教,我想念的是什么。

0 个答案:

没有答案