如何使用实体框架生成和自动递增 Id [英] How to generate and auto increment Id with Entity Framework

查看:22
本文介绍了如何使用实体框架生成和自动递增 Id的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

修订整个帖子.

我正在尝试通过 Fiddler 发布以下 JSON POST 请求:

I'm trying to post the following JSON POST request via Fiddler:

{Username:"Bob", FirstName:"Foo", LastName:"Bar", Password:"123", Headline:"Tuna"}

但是我收到此错误:

Message "Cannot insert the value NULL into column 'Id', table 'xxx_f8dc97e46f8b49c2b825439607e89b59.dbo.User'; column does not allow nulls. INSERT fails.
The statement has been terminated." string

虽然如果我手动发送一个随机 ID 和请求,那么一切都很好.像这样:

Though if I manually send a random Id along with the request then all is good. Like so:

{Id:"1", Username:"Bob", FirstName:"Foo", LastName:"Bar", Password:"123", Headline:"Tuna"}

为什么实体框架不生成和自动递增 Id?我的POCO类如下:

Why does Entity Framework not generate and auto increment the Id's? My POCO class is as follows:

public class User
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public string Id { get; set; }
    public string Username { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Password { get; set; }
    public string Headline { get; set; }
    public virtual ICollection<Connection> Connections { get; set; }
    public virtual ICollection<Address> Addresses { get; set; }
    public virtual ICollection<Phonenumber> Phonenumbers { get; set; }
    public virtual ICollection<Email> Emails { get; set; }
    public virtual ICollection<Position> Positions { get; set; }
}

public class Connection
{
    public string ConnectionId { get; set; }
    public int UserId { get; set; }
    public virtual User User { get; set; }
}

public class Phonenumber
{
    public string Id { get; set; }
    public string Number { get; set; }
    public int Cycle { get; set; }
    public int UserId { get; set; }
    public User User { get; set; }
}

这里是控制器方法.在调试模式下,我通过 Fiddler 发送请求时,它会在 db.SaveChanges(); 处中断并给出上面看到的错误.

Here is the controller method. When in debug mode and I send the request via Fiddler it breaks at db.SaveChanges(); and gives the error seen a bit above.

    // POST api/xxx/create
    [ActionName("create")]
    public HttpResponseMessage PostUser(User user)
    {
        if (ModelState.IsValid)
        {
            db.Users.Add(user);
            db.SaveChanges();

            HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, user);
            response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = user.Id }));
            return response;
        }
        else
        {
            return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
        }
    }

怎么了?

解决方案

将字符串 Id 改为 int 并删除数据注释.将 Id 重命名为 UserId,仍然遵循惯例,并在其他 POCO 中进行必要的更改以匹配更改.

Change string Id to int instead and remove Data annotations. Renamed the Id to UserId, still following convention, and made changes where necessary in other POCO's to match up with the changes.

推荐答案

这是一个猜测 :)

是不是因为ID是字符串?如果将其更改为 int 会发生什么?

Is it because the ID is a string? What happens if you change it to int?

我的意思是:

 public int Id { get; set; }

这篇关于如何使用实体框架生成和自动递增 Id的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆