© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•2y ago•
48 replies
juks

Problem while "dotnet ef migrations add Init"

When I'm trying to add data base migration i get error "The seed entity for entity type 'Contract' cannot be added because a non-zero value is required for property 'ContractId'. Consider providing a negative value to avoid collisions with non-seed data."
table (look screenshot)
insert :
   new Contract
            {
                CustomerId = 1, SoftwareId = 1, Version = "1.0", StartDate = new DateTime(2024, 1, 1),
                EndDate = new DateTime(2024, 1, 10), Price = 1000.00m, DiscountId = 1, SupportYears = 1,
                IsSigned = false
            },
   new Contract
            {
                CustomerId = 1, SoftwareId = 1, Version = "1.0", StartDate = new DateTime(2024, 1, 1),
                EndDate = new DateTime(2024, 1, 10), Price = 1000.00m, DiscountId = 1, SupportYears = 1,
                IsSigned = false
            },

table has autoincrementation:
modelBuilder.Entity<Contract>()
            .Property(c => c.ContractId)
            .ValueGeneratedOnAdd();
modelBuilder.Entity<Contract>()
            .Property(c => c.ContractId)
            .ValueGeneratedOnAdd();


table code:
using System.ComponentModel.DataAnnotations.Schema;

namespace Company_APBD.Models;

public class Contract
{
    public int ContractId { get; set; }
    public int CustomerId { get; set; }
    public int SoftwareId { get; set; }
    public string Version { get; set; }
    public DateTime StartDate { get; set; }
    public DateTime EndDate { get; set; }
    public decimal Price { get; set; }
    public int DiscountId { get; set; }
    public int SupportYears { get; set; }
    public bool IsSigned { get; set; }

   
}
using System.ComponentModel.DataAnnotations.Schema;

namespace Company_APBD.Models;

public class Contract
{
    public int ContractId { get; set; }
    public int CustomerId { get; set; }
    public int SoftwareId { get; set; }
    public string Version { get; set; }
    public DateTime StartDate { get; set; }
    public DateTime EndDate { get; set; }
    public decimal Price { get; set; }
    public int DiscountId { get; set; }
    public int SupportYears { get; set; }
    public bool IsSigned { get; set; }

   
}
image.png
image.png
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

dotnet ef migrations add init returns: 'Unable to retrieve project metadata.'
C#CC# / help
10mo ago
✅ Dotnet ef problem
C#CC# / help
3mo ago
dotnet-ef error creating migrations
C#CC# / help
2y ago
✅ dotnet ef scaffold problem
C#CC# / help
3y ago