7

In a new WPF project that I am writing using c#, I want to attempt on using Entity Framework Core to interact with my SQL Server database.

Every time I try to add model to my context, I get the following error

Cannot insert explicit value for identity column in table 'Orders' when IDENTITY_INSERT is set to OFF.

I am using a Repository and UnitOfWork which wraps the Entity Framework Core methods to perform the work needed.

But at its simplest, I am executing the following code

var order = new Order();
order.Title = "some";
....
Context.Orders.Add(order);
Context.SaveChanges();

Here is my model

public class Order
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public Int64 Id { get; set; }

    public string Status { get; set; }
    public int? CustomerId { get; set; }
    public DateTime? Birthdate { get; set; }
    public int UtcOffset { get; set; }\
    public DateTime CreatedAt { get; set; }
    public DateTime? UpdatedAt { get; set; }
    public int? UpdatedBy { get; set; }

    [ForeignKey(nameof(Creator))]
    public int CreatedBy { get; set; }

    public Order()
    {
        CreatedAt = DateTime.UtcNow;
    }

    public virtual User Creator { get; set; }
    public virtual Customer Customer { get; set; }
}

What could be causing this problem?

Updated

Here is how my table is created

CREATE TABLE [dbo].[Orders](
    [Id] [bigint] IDENTITY(1,1) NOT NULL,
    [Status] [varchar](50) NOT NULL,
    [CustomerId] [int] NULL,
    [Birthdate] [datetime] NULL,
    [CreatedBy] [int] NOT NULL,
    [CreatedAt] [datetime] NOT NULL,
    [UpdatedBy] [int] NULL,
    [UpdatedAt] [datetime] NULL,
    [UtcOffset] [int] NOT NULL,
 CONSTRAINT [PK_Orders] PRIMARY KEY CLUSTERED 
(
    [Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

Here is the method that creates the Order model

public Order Create(int? customerId, DateTime? birthdate)
{
    var order = new Order();
    order.CustomerId = customerId;
    order.Birthdate = birthdate;
    order.Status = OrderStatus.Sold.ToString();
    order.CreatedBy = Passport.Identity.Id;

    var updatedOrder = Orders.Add(order);
    Orders.Save();

    return updatedOrder;
}

Here is my repository implementation

public class EntityRepository<TEntity, TKeyType> : IRepository<TEntity, TKeyType>
    where TEntity : class
    where TKeyType : struct
{
    protected readonly DbContext Context;
    protected readonly DbSet<TEntity> DbSet;

    public EntityRepository(DbContext context)
    {
        Context = context;
        DbSet = context.Set<TEntity>();
    }

    public TEntity Get(TKeyType id)
    {
        return DbSet.Find(id);
    }

    public IEnumerable<TEntity> GetAll()
    {
        return DbSet.ToList();
    }

    public bool Any(Expression<Func<TEntity, bool>> predicate)
    {
        return DbSet.Any(predicate);
    }

    public IQueryable<TEntity> Find(Expression<Func<TEntity, bool>> predicate)
    {
        return DbSet.Where(predicate);
    }

    public TEntity SingleOrDefault(Expression<Func<TEntity, bool>> predicate)
    {
        return DbSet.SingleOrDefault(predicate);
    }

    public virtual TEntity Add(TEntity entity)
    {
        DbSet.Add(entity);

        return entity;
    }

    public virtual IEnumerable<TEntity> AddRange(IEnumerable<TEntity> entities)
    {
        DbSet.AddRange(entities);

        return entities;
    }

    public void Remove(TEntity entity)
    {
        DbSet.Remove(entity);
    }

    public void RemoveRange(IEnumerable<TEntity> entities)
    {
        DbSet.RemoveRange(entities);
    }

    public void Update(TEntity entity)
    {
        DbSet.Attach(entity);
        var record = Context.Entry(entity);
        record.State = EntityState.Modified;
    }

    public IQueryable<TEntity> Query()
    {
        return DbSet;
    }

    public void Save()
    {
        Context.SaveChanges();
    }
}

public class EntityRepository<TEntity> : EntityRepository<TEntity, int>
   where TEntity : class
{
    public EntityRepository(DbContext context)
        : base(context)
    {
    }
}

Additionally, this question is not a duplicate on Entity Framework error: Cannot insert explicit value for identity column in table because I am decorating my Id property with the [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]. Moreover, I am using database first approach, so I created my database manually using MSSMS

Junior
  • 11,602
  • 27
  • 106
  • 212
  • Hi, please refer to this https://stackoverflow.com/questions/11173562/entity-framework-error-cannot-insert-explicit-value-for-identity-column-in-tabl I think it matches your question – jmesolomon May 21 '18 at 01:23
  • Show us the complete code that set Order. The issue comes from there. Remove Id assignation if you it. – CodeNotFound May 21 '18 at 03:36
  • What is the T-SQL of `Order` table? – JohnyL May 21 '18 at 05:34
  • I updated the question – Junior May 21 '18 at 14:35
  • Possible duplicate of [Entity Framework error: Cannot insert explicit value for identity column in table](https://stackoverflow.com/questions/11173562/entity-framework-error-cannot-insert-explicit-value-for-identity-column-in-tabl) – SomeGuy May 21 '18 at 15:05
  • @SomeGuy you repeated the link provided by @jmesolomon, As you can see, I am using `[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]` for my Id property. + I am using Database-first approach – Junior May 21 '18 at 15:32
  • @MikeA yeah, the responses there should still match your question. In particular, at some point, you are explicitly setting a value to your identity column, and entity framework is kicking back an error saying it expects you to leave that column alone. – SomeGuy May 21 '18 at 15:43
  • @SomeGuy as you can see in my code, I am not setting the value. The property type in Int64, so the default value is 0, could that be the problem? In EntityFrramework 6 this is how I used to doing it. – Junior May 21 '18 at 15:44

1 Answers1

2

In my case I had to add entity.Property(e => e.TranId).ValueGeneratedOnAdd(); to my context file.

modelBuilder.Entity<PersonalTrades>(entity =>
{
     entity.Property(e => e.TranId).ValueGeneratedOnAdd();
}

I was not updating the identity field(TranId) from Automapper. Finally the above change worked for me which, as the name specifies, generates the value for the identity field while inserting the record.

Sachin Parashar
  • 1,067
  • 2
  • 18
  • 28