Base solution for your next web application
Open Closed

Error in Test - Simple DDL table #4108


User avatar
0
[email protected] created

This table is not referenced in any other table

 Test_AddUsersToOrganizationUnit [0:00.001] Failed: System.InvalidOperationException : The property 'Id' on entity type 'SelectItemValue' has a temporary value while attempting to change the entity's state to 'Unchanged'. Either set a permanent value explicitly or ensure that the database is configured to generate values for this property.

System.InvalidOperationException : The property 'Id' on entity type 'SelectItemValue' has a temporary value while attempting to change the entity's state to 'Unchanged'. Either set a permanent value explicitly or ensure that the database is configured to generate values for this property. at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntry.SetEntityState(EntityState oldState, EntityState newState, Boolean acceptChanges) at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntry.SetEntityState(EntityState entityState, Boolean acceptChanges, Boolean forceStateWhenUnknownKey) at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntry.AcceptChanges() at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.AcceptAllChanges(IEnumerable1 changedEntries) at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChanges(Boolean acceptAllChangesOnSuccess) at Microsoft.EntityFrameworkCore.DbContext.SaveChanges(Boolean acceptAllChangesOnSuccess) at Abp.EntityFrameworkCore.AbpDbContext.SaveChanges() in D:\Github\aspnetboilerplate\src\Abp.EntityFrameworkCore\EntityFrameworkCore\AbpDbContext.cs:line 199 at Furiosotoro.Furiosotoro.Migrations.Seed.Host.DefaultSelectItemValuesCreator.AddValueIfNotExists(SelectItemValue value) in C:\Users\julian\Source\Repos\Furiosotoro\Furiosotoro\src\Furiosotoro.Furiosotoro.EntityFrameworkCore\Migrations\Seed\Host\DefaultSelectItemValues.cs:line 103 at Furiosotoro.Furiosotoro.Migrations.Seed.Host.DefaultSelectItemValuesCreator.CreateValues() in C:\Users\julian\Source\Repos\Furiosotoro\Furiosotoro\src\Furiosotoro.Furiosotoro.EntityFrameworkCore\Migrations\Seed\Host\DefaultSelectItemValues.cs:line 90 at Furiosotoro.Furiosotoro.Migrations.Seed.Host.DefaultSelectItemValuesCreator.Create() in C:\Users\julian\Source\Repos\Furiosotoro\Furiosotoro\src\Furiosotoro.Furiosotoro.EntityFrameworkCore\Migrations\Seed\Host\DefaultSelectItemValues.cs:line 83 at Furiosotoro.Furiosotoro.Migrations.Seed.Host.InitialHostDbBuilder.Create() in C:\Users\julian\Source\Repos\Furiosotoro\Furiosotoro\src\Furiosotoro.Furiosotoro.EntityFrameworkCore\Migrations\Seed\Host\InitialHostDbBuilder.cs:line 20 at Furiosotoro.Furiosotoro.Migrations.Seed.SeedHelper.SeedHostDb(FuriosotoroDbContext context) in C:\Users\julian\Source\Repos\Furiosotoro\Furiosotoro\src\Furiosotoro.Furiosotoro.EntityFrameworkCore\Migrations\Seed\SeedHelper.cs:line 26 at Furiosotoro.Furiosotoro.Migrations.Seed.SeedHelper.WithDbContext[TDbContext](IIocResolver iocResolver, Action1 contextAction) in C:\Users\julian\Source\Repos\Furiosotoro\Furiosotoro\src\Furiosotoro.Furiosotoro.EntityFrameworkCore\Migrations\Seed\SeedHelper.cs:line 42 at Furiosotoro.Furiosotoro.Migrations.Seed.SeedHelper.SeedHostDb(IIocResolver iocResolver) in C:\Users\julian\Source\Repos\Furiosotoro\Furiosotoro\src\Furiosotoro.Furiosotoro.EntityFrameworkCore\Migrations\Seed\SeedHelper.cs:line 18 at Furiosotoro.Furiosotoro.EntityFrameworkCore.FuriosotoroEntityFrameworkCoreModule.PostInitialize() in C:\Users\julian\Source\Repos\Furiosotoro\Furiosotoro\src\Furiosotoro.Furiosotoro.EntityFrameworkCore\EntityFrameworkCore\FuriosotoroEntityFrameworkCoreModule.cs:line 49 at System.Collections.Generic.List1.ForEach(Action1 action) at Abp.AbpBootstrapper.Initialize() in D:\Github\aspnetboilerplate\src\Abp\AbpBootstrapper.cs:line 152 at Abp.TestBase.AbpIntegratedTestBase`1.InitializeAbp() in D:\Github\aspnetboilerplate\src\Abp.TestBase\TestBase\AbpIntegratedTestBase.cs:line 47 at Furiosotoro.Furiosotoro.Tests.AppTestBase..ctor() in C:\Users\julian\Source\Repos\Furiosotoro\Furiosotoro\test\Furiosotoro.Furiosotoro.Tests\AppTestBase.cs:line 27 at Furiosotoro.Furiosotoro.Tests.Organizations.OrganizationUnitAppService_Tests..ctor() in C:\Users\julian\Source\Repos\Furiosotoro\Furiosotoro\test\Furiosotoro.Furiosotoro.Tests\Organizations\OrganizationUnitAppService_Tests.cs:line 19

I have create this entity using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema;

using Abp.Domain.Entities;

namespace Furiosotoro.Furiosotoro.Configuration { [Table("SelectItemValue")] public class SelectItemValue : Entity

    [Required]
    [MaxLength(MaxCategoryLength)]
    public virtual string Category { get; set; }

    [Required]
    [MaxLength(MaxNameLength)]
    public virtual string Name { get; set; }


    [Required]
    [MaxLength(MaxValueLength)]
    public virtual string Value { get; set; }


    public virtual int Order { get; set; }
}

}


6 Answer(s)
  • User Avatar
    0
    aaron created
    Support Team

    Can you show the relevant methods? Especially: Furiosotoro.Furiosotoro.Migrations.Seed.Host.DefaultSelectItemValuesCreator.AddValueIfNotExists(SelectItemValue value).

  • User Avatar
    0
    [email protected] created

    ah

    You pointed my in the right direction.

    Confused

    I fixed the test errors by setting the id to a value - but why should this be required as the field in an identity and therefore auto incremented, won't this now break the seed step as part on database update?

    static DefaultSelectItemValuesCreator()
        {
            InitialValues = new List<SelectItemValue>
            {
                new SelectItemValue
                {
                    Id = 1,
                    Name = "NSW",
                    Order = 1,
                    Value = "NSW",
                    Category = "State"
                },
                new SelectItemValue
                {
                    Id = 2,
                    Name = "VIC",
                    Order = 2,
                    Value = "VIC",
                    Category = "State"
                },
                new SelectItemValue
                {
                    Id = 3,
                    Name = "WA",
                    Order = 4,
                    Value = "WA",
                    Category = "State"
                },
                new SelectItemValue
                {
                    Id = 4,
                    Name = "NT",
                    Order = 5,
                    Value = "NT",
                    Category = "State"
                },
                new SelectItemValue
                {
                    Id = 5,
                    Name = "SA",
                    Order = 6,
                    Value = "SA",
                    Category = "State"
                },
                new SelectItemValue
                {
                    Id = 6,
                    Name = "TAS",
                    Order = 7,
                    Value = "TAS",
                    Category = "State"
                },
                new SelectItemValue
                {
                    Id = 7,
                Name = "QLD",
                Order = 3,
                Value = "QLD",
                Category = "State"
            }
            };
    
    
        }
    
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi @[email protected],

    You are right, there shouldn't be a need to set Id properties. Can you share your seed code ?

    Thanks.

  • User Avatar
    0
    [email protected] created

    using System.Collections.Generic; using System.Linq; using Abp.Application.Features; using Furiosotoro.Furiosotoro.Configuration; using Microsoft.EntityFrameworkCore; using Furiosotoro.Furiosotoro.Editions; using Furiosotoro.Furiosotoro.EntityFrameworkCore; using Furiosotoro.Furiosotoro.Features;

    namespace Furiosotoro.Furiosotoro.Migrations.Seed.Host { public class DefaultSelectItemValuesCreator { public static List<SelectItemValue> InitialValues { get; private set; }

        private readonly FuriosotoroDbContext _context;
    
        static DefaultSelectItemValuesCreator()
        {
            InitialValues = new List&lt;SelectItemValue&gt;
            {
                new SelectItemValue
                {
                    Id = 1,
                    Name = "NSW",
                    Order = 1,
                    Value = "NSW",
                    Category = "State"
                },
                new SelectItemValue
                {
                    Id = 2,
                    Name = "VIC",
                    Order = 2,
                    Value = "VIC",
                    Category = "State"
                },
                new SelectItemValue
                {
                    Id = 3,
                    Name = "WA",
                    Order = 4,
                    Value = "WA",
                    Category = "State"
                },
                new SelectItemValue
                {
                    Id = 4,
                    Name = "NT",
                    Order = 5,
                    Value = "NT",
                    Category = "State"
                },
                new SelectItemValue
                {
                    Id = 5,
                    Name = "SA",
                    Order = 6,
                    Value = "SA",
                    Category = "State"
                },
                new SelectItemValue
                {
                    Id = 6,
                    Name = "TAS",
                    Order = 7,
                    Value = "TAS",
                    Category = "State"
                },
                new SelectItemValue
                {
                    Id = 7,
                Name = "QLD",
                Order = 3,
                Value = "QLD",
                Category = "State"
            },
                new SelectItemValue
                {
                    Id = 8,
                    Name = "DL1",
                    Order = 1,
                    Value = "DL1",
                    Category = "DownloadUpload"
                },
                new SelectItemValue
                {
                    Id = 8,
                    Name = "DL2",
                    Order = 2,
                    Value = "DL2",
                    Category = "DownloadUpload"
                },
                new SelectItemValue
                {
                    Id = 9,
                    Name = "R1",
                    Order = 2,
                    Value = "R1",
                    Category = "Repository"
                },
                new SelectItemValue
                {
                    Id = 10,
                    Name = "R2",
                    Order = 3,
                    Value = "R2",
                    Category = "Repository"
                }
            };
    
    
        }
    
        public DefaultSelectItemValuesCreator(FuriosotoroDbContext context)
        {
            _context = context;
        }
    
        public void Create()
        {
            CreateValues();
        }
    
        private void CreateValues()
        {
            foreach (var value in InitialValues)
            {
                AddValueIfNotExists(value);
            }
        }
    
        private void AddValueIfNotExists(SelectItemValue value)
        {
            if (_context.SelectItemValues.Any(l => l.Name == value.Name))
            {
                return;
            }
    
            _context.SelectItemValues.Add(value);
    
            _context.SaveChanges();
        }
    }
    

    }

  • User Avatar
    0
    aaron created
    Support Team

    Can you try if return new List<> works, instead of holding on to instance variables created in a static constructor?

    public static List<SelectItemValue> InitialValues => GetInitialValues();
    
    private static List<SelectItemValue> GetInitialValues()
    {
        return new List<SelectItemValue>
        {
            // ...
        }
    }
    
  • User Avatar
    0
    [email protected] created

    Thanks!

    I change the seed to create the list like this public static List<SelectItemValue> InitialValues => GetInitialValues();

        private readonly FuriosotoroDbContext _context;
    
    
        private static List&lt;SelectItemValue&gt; GetInitialValues()
        {
            return new List&lt;SelectItemValue&gt;
            {
    

    .... } } and all the tests are now working

    thanks you :D