Cannot insert explicit value for identity column when IDENTITY_INSERT is set to OFF. (Entity Framework Core)

asp.net-core c# entity-framework-core

Question

I'm getting this error when I attempt to add a new entity into the Database. The entity name is DestuffedContainer. The definition of this entity and related entities is below:

DestuffedContainer:

[Table("DestuffedContainer")]
public class DestuffedContainer
{
    public long DestuffedContainerId { get; set; }
    public int Index { get; set; }
    public string Description { get; set; }
    public int? PackageQuantity { get; set; }
    public string PackageType { get; set; }
    public double? CBM { get; set; }
    public string Location { get; set; }
    public string MarksAndNumber { get; set; }
    public int? ManifestWeight { get; set; }
    public int? FoundWeight { get; set; }
    public int? ManifestQuantity { get; set; }
    public string ConsigneeName { get; set; }
    public string Remarks { get; set; }
    public string InvoiceFound { get; set; }
    public string PackageFound { get; set; }
    public long TellySheetId { get; set; }
    public TellySheet TellySheet { get; set; }
    public long ContainerIndexId { get; set; }
    public ContainerIndex ContainerIndex { get; set; }
}

TellySheet:

[Table("TellySheet")]
public class TellySheet
{
    public TellySheet()
    {
        DestuffedContainers = new List<DestuffedContainer>();
    }

    public long TellySheetId { get; set; }
    public string TellyClerk { get; set; }
    public DateTime? DestuffDate { get; set; }
    public string DayNight { get; set; }
    public long ShippingAgentId { get; set; }
    public ShippingAgent ShippingAgent { get; set; }

    public List<DestuffedContainer> DestuffedContainers { get; set; }
}

ContainerIndex:

[Table("ContainerIndex")]
public class ContainerIndex
{
    public long ContainerIndexId { get; set; }
    public string BLNo { get; set; }
    public int? IndexNo { get; set; }
    public double? BLGrossWeight { get; set; }
    public string Description { get; set; }
    public string MarksAndNumber { get; set; }
    public string ShippingLine { get; set; }
    public bool? IsDestuffed { get; set; }
    public string AuctionLotNo { get; set; }
    public long? ContainerId { get; set; }

    public Container Container { get; set; }
    public DeliveryOrder DeliveryOrder { get; set; }
    public OrderDetail OrderDetail { get; set; }
    public DestuffedContainer DestuffedContainer { get; set; }
    public Auction Auction { get; set; }
}

The error occurs in the below lines of code when I try to add the list of destuffed containers:

 var dstfContainers = new List<DestuffedContainer>();
 _tellySheetRepo.Add(tellySheet);

 foreach (var container in containers)
 {
     var destuff = new DestuffedContainer
            {
                TellySheetId = tellySheet.TellySheetId,
                ContainerIndexId = container.ContainerIndexId,
                Index = container.IndexNumber ?? 0,
                Description = container.Description,
                PackageQuantity = container.Package,
                PackageType = container.PackageType,
                CBM = container.CBM,
                ManifestWeight = container.ManifestWeight > 0 ? Convert.ToInt32(container.ManifestWeight) : 0,
                FoundWeight = container.FoundWeight > 0 ? Convert.ToInt32(container.FoundWeight) : 0,
                MarksAndNumber = container.MarksAndNumber,
                Location = container.Location,
                Remarks = container.Remarks,
                InvoiceFound = container.InvoiceFoud,
                PackageFound = container.PackageFoud
            };

    dstfContainers.Add(destuff);

    var index = _cIndexRepo.Find(container.ContainerIndexId);

    if (index != null)
    {
        index.IsDestuffed = true;
        _cIndexRepo.Update(index);
    }
}

_destuffRepo.AddRange(dstfContainers);

I'm not sure what this error means as I'm not explicitly specifying the primary key value of the destuffedcontainer entity and by default it's 0. Entity Framework should pick this up as an insert but instead it throws an error.

It was working fine few days ago but I'm not sure what has changed since causing this error.

I'm using Entity Framework Core for modeling my entities. I've tried several solutions but none of them seem to work. I'd appreciate any help to resolve this issue.

EDIT

It seems that when I assign the ContainerIndexId value which is the foreign key in DestuffedContainer table to the entity, I get this error. I'm not sure how it's relevant.

1
1
7/21/2019 6:35:00 PM

Popular Answer

For future readers.

I had the reverse problem. I did NOT want IDENTITY to be auto-on.

So something like this: (very simple fluent mapping)

        builder.HasKey(ent => ent.MyColumnWhichIsTheKey);

I had to add this line (ValueGeneratedNever) to keep IDENITY to be off (on purpose "off"):

        builder.HasKey(ent => ent.MyColumnWhichIsTheKey);
        builder.Property(ent => ent.MyColumnWhichIsTheKey).ValueGeneratedNever();

See:

https://docs.microsoft.com/en-us/dotnet/api/microsoft.entityframeworkcore.metadata.builders.propertybuilder.valuegeneratednever?view=efcore-3.1

Other hints:

using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

and

<PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="3.1.3" />
0
4/14/2020 2:35:11 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow