How can I fix error (The property 'Guid' cannot be configured as 'ValueGeneratedOnUpdate' or 'ValueGeneratedOnAddOrUpdate'...)?

847 views Asked by At

I have a .NET Core 2.1 class library that is using Microsoft.EntityFrameworkCore Database first. I was able to get it to create the models from the Db but any time I try to read from the database I get an error. There are no Guids in the table that I'm reading from and no foreign keys. The class has an int as its primary key. So, I'm not sure what is causing this issue. Any help would be appreciated.

Side note: The SQL user is configured with read only access. I have no need to create, update, or delete records.

I was following the instructions here:

https://www.entityframeworktutorial.net/efcore/create-model-for-existing-database-in-ef-core.aspx

ERROR:

The property 'Guid' cannot be configured as 'ValueGeneratedOnUpdate' or 'ValueGeneratedOnAddOrUpdate' because the key value cannot be changed after the entity has been added to the store.

STACK TRACE:

at Microsoft.EntityFrameworkCore.Infrastructure.ModelValidator.ValidateNoMutableKeys(IModel model, IDiagnosticsLogger1 logger) at Microsoft.EntityFrameworkCore.Infrastructure.ModelValidator.Validate(IModel model, IDiagnosticsLogger1 logger) at Microsoft.EntityFrameworkCore.Infrastructure.RelationalModelValidator.Validate(IModel model, IDiagnosticsLogger1 logger) at Microsoft.EntityFrameworkCore.SqlServer.Internal.SqlServerModelValidator.Validate(IModel model, IDiagnosticsLogger1 logger) at Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal.ConventionDispatcher.ImmediateConventionScope.OnModelFinalized(IConventionModelBuilder modelBuilder) at Microsoft.EntityFrameworkCore.Metadata.Internal.Model.FinalizeModel() at Microsoft.EntityFrameworkCore.Infrastructure.ModelSource.GetModel(DbContext context, IConventionSetBuilder conventionSetBuilder) at Microsoft.EntityFrameworkCore.Internal.DbContextServices.CreateModel() at Microsoft.EntityFrameworkCore.Internal.DbContextServices.get_Model()
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitFactory(FactoryCallSite factoryCallSite, RuntimeResolverContext context) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor2.VisitCallSiteMain(ServiceCallSite callSite, TArgument argument) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitCache(ServiceCallSite callSite, RuntimeResolverContext context, ServiceProviderEngineScope serviceProviderEngine, RuntimeResolverLock lockType) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScopeCache(ServiceCallSite singletonCallSite, RuntimeResolverContext context) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor2.VisitCallSite(ServiceCallSite callSite, TArgument argument) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, RuntimeResolverContext context) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor2.VisitCallSiteMain(ServiceCallSite callSite, TArgument argument) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitCache(ServiceCallSite callSite, RuntimeResolverContext context, ServiceProviderEngineScope serviceProviderEngine, RuntimeResolverLock lockType) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScopeCache(ServiceCallSite singletonCallSite, RuntimeResolverContext context) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor2.VisitCallSite(ServiceCallSite callSite, TArgument argument) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.Resolve(ServiceCallSite callSite, ServiceProviderEngineScope scope) at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType) at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider provider) at Microsoft.EntityFrameworkCore.DbContext.get_DbContextDependencies()
at Microsoft.EntityFrameworkCore.DbContext.get_InternalServiceProvider() at Microsoft.EntityFrameworkCore.DbContext.get_DbContextDependencies() at Microsoft.EntityFrameworkCore.DbContext.get_Model() at Microsoft.EntityFrameworkCore.Internal.InternalDbSet1.get_EntityType() at Microsoft.EntityFrameworkCore.Internal.InternalDbSet1.get_EntityQueryable() at Microsoft.EntityFrameworkCore.Internal.InternalDbSet1.System.Linq.IQueryable.get_Provider() at System.Linq.Queryable.Where[TSource](IQueryable1 source, Expression`1 predicate) at Live_Db.LiveDbHelper.GetAllClients() in D:\VSProjects\Dashboard\Live_Db\LiveDbHelper.cs:line 18

DbContext:

public partial class ApplicationContext : DbContext
{
    public ApplicationContext(DbContextOptions<ApplicationContext> options)
        : base(options)
    { }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        if (!optionsBuilder.IsConfigured)
        {
            optionsBuilder.UseSqlServer("Server=****; Database=****; User Id=****; Password=****");
        }
    }

    public virtual DbSet<Client> Client { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Client>(entity =>
        {
            entity.ToTable("Client", "app");

            entity.HasIndex(e => e.ClientIdpublic)
                .HasName("IX_ctbl.sys.Client_PublicIDKey")
                .IsUnique();

            entity.Property(e => e.ClientId).HasColumnName("ClientID");

            entity.Property(e => e.BillingAddress1).HasMaxLength(200);

            entity.Property(e => e.BillingAddress2).HasMaxLength(200);

            entity.Property(e => e.BillingCity).HasMaxLength(200);

            entity.Property(e => e.BillingContactName).HasMaxLength(200);

            entity.Property(e => e.BillingCountry).HasMaxLength(200);

            entity.Property(e => e.BillingEmail)
                .HasMaxLength(200)
                .IsUnicode(false);

            entity.Property(e => e.BillingPhoneNumber).HasMaxLength(25);

            entity.Property(e => e.BillingReferenceId)
                .HasColumnName("BillingReferenceID")
                .HasMaxLength(50)
                .IsUnicode(false);

            entity.Property(e => e.BillingState).HasMaxLength(200);

            entity.Property(e => e.BillingStatusCode)
                .HasMaxLength(20)
                .IsUnicode(false);

            entity.Property(e => e.BillingZip).HasMaxLength(200);

            entity.Property(e => e.ClientIdpublic)
                .IsRequired()
                .HasColumnName("ClientIDPublic")
                .HasMaxLength(15)
                .IsUnicode(false);

            entity.Property(e => e.ClientName)
                .IsRequired()
                .HasMaxLength(100);

            entity.Property(e => e.ClientSecret).HasMaxLength(300);

            entity.Property(e => e.DefaultAuthTypeId).HasColumnName("DefaultAuthTypeID");

            entity.Property(e => e.EvalEndDate).HasColumnType("datetime");

            entity.Property(e => e.Ftppwd)
                .HasColumnName("FTPPwd")
                .HasMaxLength(50)
                .IsUnicode(false);

            entity.Property(e => e.InactiveMessage)
                .HasMaxLength(200)
                .IsUnicode(false);

            entity.Property(e => e.LogLocalDbcalls).HasColumnName("LogLocalDBCalls");

            entity.Property(e => e.RenewalDate).HasColumnType("datetime");

            entity.Property(e => e.TokenForAutoNtauth)
                .IsRequired()
                .HasColumnName("TokenForAutoNTAuth")
                .HasMaxLength(50)
                .IsUnicode(false);
        });
    }

Read method:

public static List<Client> GetAllClients()
{
    try
    {
        List<Client> clients = new List<Client>();
        using (var db = new ApplicationContext())
        {
            clients = db.Client.ToList();
        }
        return clients;
    }
    catch (Exception e)
    {
        throw;
    }
}
0

There are 0 answers