0

I try to generate random data in the database.

I have this class:

public class TeacherGenerator : BaseDataGenerator
    {


        //PortfolioGenerator portfolioGenerator = new PortfolioGenerator(unitOfWork);
        public TeacherGenerator(UnitOfWork unitofWork) : base(unitofWork)
        {
        }

        public void LoginTeacher(string bsn)
        {
            var generator = new PortfolioGenerator(new UnitOfWork());
            FirstName firstname = FirstNames.OrderBy(a => Guid.NewGuid()).First();
            var brpPersoon = HelperManager.BrpPersoonReposHelper2.GetByBsn(bsn);

            var leraar = HelperManager.LeraarReposHelper.GetByBsn(bsn);

            if (leraar == null)
            {
                throw new InvalidOperationException($"Leraar bestaat niet in database");
            }

            if (ProbabilityBool(40))
            {

                leraar.WizardStap = 1;
                brpPersoon.Roepnaam = ProbabilityBool(80) ? firstname.Name : null;
                leraar.Email = ProbabilityBool(90) ? GenerateUniqueEmailAddress() : null;


                if (ProbabilityBool(38))
                {
                    leraar.WizardStap = 2;
                }
                if (ProbabilityBool(38))
                {
                    leraar.WizardStap = 3;
                    leraar.IsGeheim = (ProbabilityBool(60) ? true : false);
                    leraar.RegistratieDatumUtc = ProbabilityBool(90) ? DateTime.UtcNow.AddDays(new Random().Next(90)) : (DateTime?)null;
                    leraar.AanmeldDatumUtc = ProbabilityBool(90) ? DateTime.UtcNow.Date : (DateTime?)null;

                    if (ProbabilityBool(50))
                    {
                        generator.CreatePortfolioActivities(leraar, false);
                    }
                }
            }
            else
            {
                leraar.WizardStap = 0;
                brpPersoon.Roepnaam = null;
                leraar.Email = null;
            }

            UnitOfWork.SaveChanges();
        }




    }
}

But on this line:

if (ProbabilityBool(50))
                    {
                        generator.CreatePortfolioActivities(leraar, false);
                    }

The method is like this:

 internal void CreatePortfolioActivities(Leraar teacher, bool saveToDb)
        {
            int numberOfPortfolioItems = RandomNumber(1, 50);
            int portfolioCount = teacher.Portfolio.Count() + numberOfPortfolioItems;
            if (portfolioCount > MaxPortfolioCount)
            {
                return;
            }

            for (int i = 0; i < numberOfPortfolioItems; i++)
            {
                //try
                //{
                    Portfolio portfolio = CreatePortfolioActivity();
                    AddPortfolioToTeacher(teacher, portfolio);

                    count++;
                    Trace.Write($"Portfolio aanmaken: {count}\r");
                //}
                //catch (Exception exp)
                //{
                //    Trace.WriteLine($"Portfolio is NIET aangemaakt. Details: {exp}");
                //}
            }
            if (saveToDb)
            {
                UnitOfWork.SaveChanges();
            }
        }

then I will get on this line:

AddPortfolioToTeacher(teacher, portfolio);

this error:

System.InvalidOperationException: 'An entity object cannot be referenced by multiple instances of IEntityChangeTracker.'

Don't understand this.

How to fix this?

Thank you

this is for generating leraar:

  private string GenerateXmlContent(string bsn)
        {
            RequestXmlBuilder requestBuilder = new RequestXmlBuilder();
            requestBuilder.WithIdentificatiecodeBedrijfsdocument($"BulkGenerator_{Guid.NewGuid()}")
                .WithSender("DUO")
                .WithReceiver("CIBG")
                .At(DateTime.Now.AddSeconds(1).ToString("yyyy-MM-ddThh:mm:ss", new CultureInfo("nl-NL")));

            LeraarXmlBuilder leraar = GenerateLeraarXml(bsn);
            requestBuilder.Including(leraar);

            string content = requestBuilder.Build();
            return content;
        }

and this method:

 private LeraarXmlBuilder GenerateLeraarXml(string bsn)
        {
            LeraarXmlBuilder leraarBuilder = new LeraarXmlBuilder();
            IdentificatieMensXmlBuilder identificatieMensBuilder = new IdentificatieMensXmlBuilder();
            identificatieMensBuilder.WithValue(bsn);

            leraarBuilder.Including(identificatieMensBuilder);

            if (ProbabilityBool(20))
            {
                //woon in buitenland
                BuitenlandsWoonadresXmlBuilder buitenlandsWoonadresBuilder = new BuitenlandsWoonadresXmlBuilder();
                leraarBuilder.Including(buitenlandsWoonadresBuilder);

                buitenlandsWoonadresBuilder.InCountryWithCode(Countries.OrderBy(c => Guid.NewGuid()).First().Code);
                buitenlandsWoonadresBuilder.WithAddressLine1("Address regel 1");

                if (ProbabilityBool(50))
                {
                    //2e regel
                    buitenlandsWoonadresBuilder.WithAddressLine2("Address regel 2");

                    if (ProbabilityBool(50))
                    {
                        //3e regel
                        buitenlandsWoonadresBuilder.WithAddressLine3("Address regel 3");
                    }
                }
            }

            BevoegdGezagXmlBuilder bevoegdGezagBuilder = GenerateBevoegdGezagXml();
            leraarBuilder.Including(bevoegdGezagBuilder);

            if (ProbabilityBool(30))
            {
                //ook werkzaam bij andere bevoegdgezag
                bevoegdGezagBuilder = GenerateBevoegdGezagXml();
                leraarBuilder.Including(bevoegdGezagBuilder);
            }

            return leraarBuilder;
        }
niceToExist
  • 55
  • 1
  • 10
  • I can not see teh code where you instantiate the Leerar instance. And as far as I can tell, it is complaining about *some* instance being used more then once when it should be unique. So that is your first bet. – Christopher Jan 07 '19 at 03:54
  • Is this what you mean? – niceToExist Jan 07 '19 at 04:21
  • Afterr a bit more reading, it might be that you are reusing hte Portfolio instance. Wich is apparently not allowed with that Design Pattern/Interface: https://stackoverflow.com/a/10194299/3346583 And I am still not 100% sure it is not just you retrieving the same Teacher/Leerar twice. In any case, this exception complains about **something** being used twice when it should not be. – Christopher Jan 07 '19 at 05:07
  • Oke, I solved: private UnitOfWork _unitOfWork; and then this: var generator = new PortfolioGenerator(_unitOfWork); var duoDataGenerator = new DuoDataGenerator(_unitOfWork); so using the same context – niceToExist Jan 07 '19 at 06:30
  • So thank you for the tip – niceToExist Jan 07 '19 at 06:31

0 Answers0