简体   繁体   English

带有PostgreSQL的实体框架不保存数据

[英]Entity Framework with PostgreSQL not saving data

I have a connection to PostgreSQL using Entity Framework. 我使用实体框架连接到PostgreSQL。

When retrieving data, everything works okay, also migrations are working well, the problem is during insertion of the data. 检索数据时,一切正常,迁移也正常,问题出在插入数据期间。

I assume its due to the ID not being automatically generated, but since I'm new to Entity Framework, I do not know how to move forward. 由于ID是不会自动生成的,因此我认为它是自动生成的,但是由于我是Entity Framework的新手,所以我不知道该如何前进。

Here is my AddUser function: 这是我的AddUser函数:

public Task<EntityEntry<UserModel>> AddUser(UserModel user) => this.context.Users.AddAsync(user);

This is the corresponding model class: 这是相应的模型类:

using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace WishareIntegrationApi.Entities
{
    public class UserModel
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int Id { get; set; }
        public string token { get; set; }
        public string displayName { get; set; }
        public string photoURL { get; set; }
        public string email { get; set; }
        public DateTime registeredAt { get; set; }
        public string birthday { get; set; }
    }
}

When I add data into table manually and fill in the ID it will insert the data, if the ID is not part of the insert query, it will fail even tho it should be auto_increment. 当我手动将数据添加到表中并填写ID时,它将插入数据,如果ID不是插入查询的一部分,即使它应为auto_increment,也会失败。

The database is generated from the code using migrations. 数据库是使用迁移从代码生成的。

Any ideas? 有任何想法吗?

如Evk所述,我没有调用SaveChanges函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM