异步等待并继续使用C#中的Entity Framework数据库处理方法 [英] Async await and continuewith for a Entity Framework database Process Method in C#

查看:251
本文介绍了异步等待并继续使用C#中的Entity Framework数据库处理方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Save方法来添加/更新联系人.我设计了一个异步方法.但是我无法从数据库中获取记录.

I have a Save method to add/update a contact. I designed an asynchronous method. But I'm not able to get the records from the database.

请看一下代码:

public async Task<bool> SaveContact(Contact contact)
{
    bool flag = false;

    try
    {
        if(contact != null)
        {
            using(var dbContext = DBContext())
            {
                ContactEDB contactObj = new ContactEDB();

                if(contact.Id > 0)
                {
                    contactObj = await dbContext.Contact.FirstOrDefaultAsync(a => a.Id == contact.Id);

                    // The local variable "contactObj" always return NULL while on debugging
                    if(contactObj != null)
                    {
                        contactObj.FirstName = "John";
                        contactObj.LastName = "Smith";
                    }
                }
                else
                {
                    contactObj = new contactObj()
                    {
                        FirstName = "John";
                        LastName = "Smith";
                    };

                    dbContext.Contact.Add(contactObj);
                }

                dbContext.SaveChanges();
            }
        }
    }
    catch(Exception ex)
    {
        // log error
    }

    return flag;
}

在上面的代码中,它总是返回NULL

In the above code, it always returns NULL

if(contactObj != null)
{
        contactObj.FirstName = "John";
        contactObj.LastName = "Smith";
};

请协助我满足要求.

注意:所述方法在库中.请协助我如何异步使用此方法.

Note: the said method is in a library. Kindly assist me how to utilize this method using asynchronously.

推荐答案

在上面的编码中始终返回NULL

In the above coding always returns NULL

这意味着在数据库中不存在ID为contact.Id的联系人.与是否使用异步方法从数据库异步获取结果无关.

This means that there is no contact with an id equal to contact.Id present in the database. It has nothing do with whether you are using an async method to get the results from the database asynchronously.

您可以临时尝试从表中获取所有记录:

You could temporarily try to fetch all records from the table:

var allContacts = await dbContext.Contact.ToListAsync();

...或尝试指定一个您肯定知道的数据库中存在的ID:

...or try to specify an id that you know for sure exists in the database:

var contact = await dbContext.Contact.FirstOrDefaultAsync(a => a.Id == 1);

使用调试器确定从上面的第一行代码返回的任何联系人的ID,然后在第二个查询中使用此ID,您应该返回一个contactObj对象.

Use the debugger to determine the id of any contact returned from the first code line above and then use this id in the second query and you should get a contactObj object back.

一旦确认数据库中确实有一些记录,就可以开始检查传递给SaveContact的Contact对象,并确定为什么它在数据库中没有ID.也许应该在将contactObj的Id属性插入数据库之前对其进行设置:

Once you have confirmed that that you actually have some records in the database you can begin to examine the Contact object that you are passing to the SaveContact and determine why it doesn't have an id that exists in the database. Maybe you should set the Id property of a contactObj before you insert it into the database:

...
            else
            {
                contactObj = new contactObj()
                {
                    Id = 10,
                    FirstName = "John";
                    LastName = "Smith";
                };

                dbContext.Contact.Add(contactObj);
            }

还要确保获取和添加相同类型的对象到表中.在您发布的示例代码中,涉及三种不同的类型,即Contact,ContactEDB和contactObj.可能应该只有一个.

Also make sure that you fetch and add the same type of objects to your table. In the sample code you have posted there is three different types involved, Contact, ContactEDB and contactObj. It should probably only be one.

请协助我如何异步使用此方法.

Kindly assist me how to utilize this method using Asynchronously.

除了使用异步重载来获取记录之外,还应该使用SaveChangesAsync方法来保留更改:

Apart from fetching records using the async overloads, you should also use the SaveChangesAsync method to perist the changes: https://msdn.microsoft.com/en-us/library/dn220070(v=vs.113).aspx

await dbContext.SaveChangesAsync();

这篇关于异步等待并继续使用C#中的Entity Framework数据库处理方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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