Base solution for your next web application
Open Closed

Problem with initial Seed #54


User avatar
0
gvb created

hey,

i'm trying to put my initial data in Seed method and the creatorId from IFullAudited nevver get filled with my data.... And I dont understand why my context is getting 0 user from the context when i just saved 3 person in it.... and my db show them....

here is my code in a Seeder class... in the database the CreatorUserId = null .... like WTF? i tried with putting 1.... same result i get null in db... how is it fucking possible that my ID that i put become null ? or why i cant get my users from my context.... i did my SaveChange....

i tried with a _context.Users.First() after the save.... and still it throw no item in the sequence....

and the adminUserForDefaultTenant give me ID 3 when i put a throw new Exception(adminUserForDefaultTenant.Id.ToString()) after the SaveChange... so why it doesn't register as CreatorId 3 when i put it in my NewsCategory?

can anyone help ?

adminUserForDefaultTenant = _context.Users.Add(
                    new User
                    {
                        TenantId = defaultTenant.Id,
                        UserName = "admin",
                        Name = "System",
                        Surname = "Administrator",
                        EmailAddress = "[email protected]",
                        IsEmailConfirmed = true,
                        Password = "AM4OLBpptxBYmM79lGOX9egzZk3vIQU3d/gFCJzaBjAPXzYIK3tQ2N7X4fcrHtElTw==" //123qwe
                    });
                _context.SaveChanges();

                _context.NewsCategory.Add(new NewsCategory
                {
                    Category = "Framework",
                    CreationTime = DateTime.Now,
                    CreatorUserId = adminUserForDefaultTenant.Id,
                    IsDeleted = false,
                });
                _context.SaveChanges();

5 Answer(s)
  • User Avatar
    0
    gvb created

    Ok so i found my solution in the AbpZeroSample

    The solution is to create your Entity First then SaveChange and finaly put the CreatorUserId to SaveChange again.

    If someone else get this problem!

    But someone can explain why i cannot find any user in my _context.Users? i want to be able to get my user created by Seeder in my other Seeding class without keeping them in a SeederDto :S

    Here is the modified code :

    adminUserForDefaultTenant = _context.Users.Add(
                        new User
                        {
                            TenantId = defaultTenant.Id,
                            UserName = "admin",
                            Name = "System",
                            Surname = "Administrator",
                            EmailAddress = "[email protected]",
                            IsEmailConfirmed = true,
                            Password = "AM4OLBpptxBYmM79lGOX9egzZk3vIQU3d/gFCJzaBjAPXzYIK3tQ2N7X4fcrHtElTw==" //123qwe
                        });
                    _context.SaveChanges();
    
                    var category  = _context.NewsCategory.Add(new NewsCategory
                    {
                        Category = "Framework",
                        CreationTime = DateTime.Now,
                        IsDeleted = false,
                    });
                    _context.SaveChanges();
    
                    category.CreatorUserId = adminRoleForDefaultTenant.Id;
    
                    _context.SaveChanges();
    
  • User Avatar
    0
    hikalkan created
    Support Team

    Call dbContext.DisableAllFilters() in your seed method.

  • User Avatar
    0
    gvb created

    Calling dbContext.DisableAllFilters() doesn't work either

    i wan't to be able to put the creatorid IN the entity creation....

    here is my code and it still doesn't create my entity with my adminId..... plz help me on this....

    if (!_dto.Context.NewsCategory.Any())
                {
                   _dto.Context.DisableAllFilters();
    
                    var framework = _dto.Context.NewsCategory.Add(new NewsCategory
                    {
                        Category = "Framework",
                        CreationTime = DateTime.Now,
                        CreatorUserId = _dto.AdminId.Value,
                        IsDeleted = false
                    });
    
                    _dto.Context.SaveChanges();
    

    CreatorUserId = NULL in the DB......

    and if i save the entity without the ID and save it after the Context.SaveChanges() then the LastModifiedTime is fullfilled and if i set the LastModifierUserId to the admin while assigning the CreatorId ...... the fucking LastModifierUserId = null

    Serious....is there a way to achieve it ..?

  • User Avatar
    0
    hikalkan created
    Support Team

    I did not understand that: Is initial data run by a user? Which user? How do you run the migration. I think, if you create an entity in seed, it's CreatorId should be null because it's not created by a user, it's created by seed. If you want to expcility set user id, then you can set DbContext.AbpSession to an object that implements IAbpSession. Because, it gets UserId from the session. You can custom implement IAbpSession similar to TestAbpSession (<a class="postlink" href="https://github.com/aspnetboilerplate/aspnetboilerplate/blob/master/src/TestBase/Abp.TestBase/TestBase/Runtime/Session/TestAbpSession.cs">https://github.com/aspnetboilerplate/as ... Session.cs</a>), set it's UserId and set this session object to DbContext.AbpSession.

  • User Avatar
    0
    gvb created

    Ok i think i understand!

    -Create an Object and inherit IAbpSession -Set the property i need in this object then _context.AbpSession = Object that implement IAbpSession -then Insert in the DB.

    that was easy -_- but didnt know AbpSession was in the context... sorry ... i should have remembered that i inherit from a different context than normal Entity framework context...

    And thx for pointing out the logic for the seed and creatorId. May be i'm wrong to try to associate any entity created to a UserId.

    I will try to think about it if i change my logic to put null to entity that are created by SystemSeed.