使用具有唯一 class 属性的 GetAwaiting 在 MVC 5 中引发错误

Using GetAwaiting with unique class attribute is raising error in MVC 5

亲爱的,我正在尝试使用唯一索引 class 属性来防止应用程序保存保存数据

public class Ports
{
    public short PortsID { get; set; }

    [Required] [Column(TypeName = "NVARCHAR")][StringLength(10)] [Index(IsUnique = true)]
    [Remote("IsPortExist", "Ports", AdditionalFields = "PortsID",
            ErrorMessage = "Port name already exists")]
    public string Portname { get; set; }
    public string address { get; set; }
    public string Country { get; set; }
    public string City { get; set; }

    //[InverseProperty("POL")]
    //public virtual ICollection<RateRequests> Portofloading { get; set; }

    //[InverseProperty("POD")]
    //public virtual ICollection<RateRequests> Portofdischarge { get; set; }
}

public async Task<JsonResult> IsPortExist(string PortName, int? Id)
    {
        bool isPortExist = await db.ports.Any(x => x.Portname == PortName && x.PortsID != Id);

        return Json(!isPortExist, JsonRequestBehavior.AllowGet);
    }

错误是 'bool' 不包含 'GetAwaiter' 的定义并且无法找到接受 bool 类型的第一个参数的可访问扩展方法 'Getawaiter',注意我使用的是 frame工作 4.6.1

提前致谢

你的问题出在这一行

bool isPortExist = await db.ports.Any(x => x.Portname == PortName && x.PortsID != Id);

Any 不是异步方法,您将不得不使用它的异步版本 AnyAsync。请将代码更改为

bool isPortExist = await db.ports.AnyAsync(x => x.Portname == PortName && x.PortsID != Id);