通过HTTP PUT方法存储数据

时间:2018-12-04 23:02:39

标签: c# asp.net web-services postman put

这是我的HTTP PUT方法。这段代码没有给出任何异常,但是仍然没有将数据存储在数据库中,任何人都可以帮忙。模型是我的getter setters类。

[HttpPut]
        public HttpResponseMessage Addfood(int id,[FromBody] Model model)
        {
            Food food = new Food();
            var userid = db.Users.FirstOrDefault(e => e.ID == id);
            if (userid == null)
            {
                return Request.CreateErrorResponse(HttpStatusCode.NotFound, "No Record Found");
            }

            food.UID = id;
            food.Name = model.Foodname;
            food.Price = model.Foodprice;
            food.Image = model.Foodimage;
            food.Date_Time = DateTime.Today;
            Category category = new Category();
            category.Name = model.Categoryname;
            db.Categories.Add(category);
            food.CatID = category.ID;
            db.Foods.Add(food);
            db.SaveChangesAsync();
            return Request.CreateResponse(HttpStatusCode.OK, food);
        }
    }

我的数据库表。

 [dbo].[Food](
        [ID] [int] IDENTITY(1,1) NOT NULL,
        [Name] [varchar](50) NULL,
        [Image] [image] NULL,
        [Price] [varchar](10) NULL,
        [CatID] [int] NOT NULL,
        [UID] [int] NOT NULL,
        [Date/Time] [datetime] NULL,


[dbo].[Category](
    [ID] [int] IDENTITY(1,1) NOT NULL,
    [Name] [varchar](50) NULL,

[dbo].[User](
    [ID] [int] IDENTITY(1,1) NOT NULL,
    [Name] [varchar](50) NOT NULL,
    [Phone] [numeric](18, 0) NOT NULL,
    [Email] [varchar](50) NOT NULL,
    [AID] [int] NOT NULL,
    [Password] [varchar](50) NOT NULL,
    [Date/time] [datetime] NULL,
    [IsVerify] [bit] NULL,
    [ProfilePicture] [image] NULL,

当我通过邮递员达到此要求时,这些就是我在邮递员中提供的参数。

{"Foodname":"Bir","Foodprice":210,"Categoryname":"H000"}

我知道了,但是我的数据库没有更新。我也将这种方法用于邮寄通话,但仍然无法正常工作

{
    "ID": 0,
    "Name": "Bir",
    "Image": null,
    "Price": "210",
    "CatID": 0,
    "UID": 52,
    "Date_Time": "2018-12-05T00:00:00+05:00",
    "Category": null,
    "User": {
        "ID": 52,
        "Name": "h999",
        "Phone": 3332158086,
        "Email": "a.b.c@yahooooo.com",
        "AID": 36,
        "Password": "030021",
        "Date_time": "2018-12-05T03:26:13.82",
        "IsVerify": null,
        "ProfilePicture": null,
        "Address": null,
        "Flags": [],
        "Foods": [],
        "Order_Tracking": [],
        "Order_Tracking1": []
    },
    "FoodRecommendations": [],
    "Order_Tracking": [],
    "Ratings": []
}

1 个答案:

答案 0 :(得分:1)

您正在检查任何用户是否有此电子邮件:

if (db.Users.Any(x => x.Email == model.Email))

但是听起来您想检查其他用户是否有此电子邮件。也许是这样的:

if (db.Users.Any(x => x.Id != id && x.Email == model.Email))