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

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

问题描述

修订整个帖子。

我试图通过发布提琴手以下JSON POST请求:

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

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

但是我得到这个错误:

However I'm getting this error:

Message "Cannot insert the value NULL into column 'Id', table 'xxx_f8dc97e46f8b49c2b825439607e89b59.dbo.User'; column does not allow nulls. INSERT fails.\r\nThe 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"}

为什么实体框架不会产生和经销商的递增标识的?我的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; }
}

下面是控制器的方法。在调试模式下,我通过提琴手发送请求它打破在 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用户ID,依然遵循惯例,并取得必要在其他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; }

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

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