首页 > 解决方案 > 我们是否应该等待这行代码,因为我们没有在这里访问数据库?

问题描述

我的要求是在签入 DB 之前检查是否在 dbcontext 对象中添加了 contactType 对象。我已经通过使用 ChangeTracker 方法完成了该操作。我有一个关于使用 await 的问题。
Q1。由于我们没有访问数据库,我们是否需要等待该条目被添加到内存中的对象(即 dbcontext)中,并且我们正在对其进行检查。

 

    public async Task<ContactType> GetOrCreateContactType(string contactTypeName)
                {
                    if (string.IsNullOrWhiteSpace(contactTypeName))
                    {
                        return null;
                    }
                    ContactType contactType;
                    contactType = _dbContext.ChangeTracker.Entries()
                                                       .Where(x => x.State == EntityState.Added && x.Entity is ContactType)
                                                       .Select(x => x.Entity as ContactType)
                                                       .FirstOrDefault(t => t.Name.ToUpper() == contactTypeName.ToUpper());
                    if (contactType != null)
                    {
                        return contactType;
                    }
                    else
                    {
                        contactType = await _dbContext.ContactTypes.SingleOrDefaultAsync(x => x.Name.ToUpper() == contactTypeName.ToUpper());
                        if (contactType != null)
                        {
                            return contactType;
                        }
                    }
                    //create if ContactType does not exists:
                    contactType = new ContactType()
                    {
                        Name = contactTypeName
                    };
                    _dbContext.ContactTypes.Add(contactType);
                    return contactType;
                }

或这个

public async Task<ContactType> GetOrCreateContactType(string contactTypeName)
        {
            if (string.IsNullOrWhiteSpace(contactTypeName))
            {
                return null;
            }
            ContactType contactType=null;
             await Task.Run(() =>
            {
                contactType = _dbContext.ChangeTracker.Entries()
                                                 .Where(x => x.State == EntityState.Added && x.Entity is ContactType)
                                                 .Select(x => x.Entity as ContactType)
                                                 .FirstOrDefault(t => t.Name.ToUpper() == contactTypeName.ToUpper());
            });
            if (contactType != null)
            {
                return contactType;
            }
            else
            {
                contactType = await _dbContext.ContactTypes.SingleOrDefaultAsync(x => x.Name.ToUpper() == contactTypeName.ToUpper());
                if (contactType != null)
                {
                    return contactType;
                }
            }
            //create if ContactType does not exists:
            contactType = new ContactType()
            {
                Name = contactTypeName
            };
            _dbContext.ContactTypes.Add(contactType);
            return contactType;
        }

标签: c#asynchronousasync-awaitentity-framework-core

解决方案


推荐阅读