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

时间:2019-02-26 09:32:08

标签: asp.net-mvc-5

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

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'的定义,并且找不到可访问的扩展方法'Getawaiter'接受bool类型的第一个参数,请注意我正在使用4.6.1版框架

预先感谢

1 个答案:

答案 0 :(得分:1)

您的问题在这一行

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

任何都不是异步方法,您将必须使用其异步版本AnyAsync。请将代码更改为

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