Base solution for your next web application
Ends in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

Extended User Entity - default value when create new user? #3471


User avatar
0
alukaszewski created

I have extended the User Entity using the development guide, to add a new user setting for 'refresh interval' and a new migration is created OK, and the user can get/set this value OK. The Users table is extended OK with new column with default integer value of 300. However, when new users are created how do I set default value there instead of 0 (zero).

I think something in this line is required in the _CreateOrEditModal.cshtml to specify a default value of 300 if no value already set? I'd also wonder how to prevent value enter less than 60?

<input id="EditUser_RefreshInterval" type="text" name="RefreshInterval" class="form-control edited" value="@Model.User.RefreshInterval" maxlength="5">

Also, when new user is created by LDAP authentication - how to ensure default value of 300 is set?

If I create row manually in the database, default value is set to 300 as per the 'default value' attribute of the column - so must be in abp I am missing something?

Thanks,

Andy


9 Answer(s)
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    If you use your new entity when creating a user instead of User entity, the default value must be inserted. You can validate min value on the client side in user creation & update dtos, see <a class="postlink" href="https://aspnetboilerplate.com/Pages/Documents/Validating-Data-Transfer-Objects">https://aspnetboilerplate.com/Pages/Doc ... er-Objects</a>.

    Of course, you also need to validate it on the client side if you are allowing user to enter value for this field.

    Thanks.

  • User Avatar
    0
    alukaszewski created

    I'm not creating a new Entity, I'm extending the existing User Entity by adding additional property.

    If I set a value of 300, this is saved and retrieved fine by the user settings modal and the user edit modal. Here is what I have in my Migration;

    public partial class Added_RefreshInterval_To_User : DbMigration
        {
            public override void Up()
            {
                AddColumn("dbo.AbpUsers", "RefreshInterval", c => c.Int(nullable: false, defaultValue: 300));
            }
            
            public override void Down()
            {
                DropColumn("dbo.AbpUsers", "RefreshInterval");
            }
        }
    

    The design of the table for the "RefreshInterval" column added to the database shows as having default value of 300, but a value of 300 is not displayed by new user modal or set when a new user is created by LDAP Auth.

    I believe problem is that the LDAP user creation user process and the user create modal need to know what default value to display/use and insert into the table, otherwise it inserts 0 (zero). Does that make sense? It is no use having default value set on database table column design as the creation process is overriding this with 0.

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    I'm not creating a new Entity, I'm extending the existing User Entity by adding additional property.
    

    Sorry, I didn't understand it at the beginning.

    I think this is an EF related problem but you can set default value in constructor of User.

  • User Avatar
    0
    alukaszewski created

    What is "EF" ?

    Where do I set a default value in the constructor of a User? Is there an example? I was looking to see how existing code works but I cannot find any existing property which uses an integer and sets a default value.

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    EF is Entity Framework, sorry :). The constructor is here <a class="postlink" href="https://github.com/aspnetzero/aspnet-zero-core/blob/dev/aspnet-core/src/MyCompanyName.AbpZeroTemplate.Core/Authorization/Users/User.cs#L27">https://github.com/aspnetzero/aspnet-ze ... ser.cs#L27</a>. You can set the default value in it.

    Thanks.

  • User Avatar
    0
    alukaszewski created

    Do you please have an example of setting a default value for a new integer property in the constructor?

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    If I understand your case correctly, it should be something like this.

    public class User : AbpUser<User>
    {
        .... Other fields
        
        public User()
        {
            RefreshInterval = 300;
        }   
    
        ... other code
    }
    

    If this is not the case, can you explain it a bit more detailed.

    Thanks.

  • User Avatar
    0
    alukaszewski created

    In User.cs I already tried this;

    public static User CreateTenantAdminUser(int tenantId, string emailAddress, string password)
            {
                return new User
                       {
                           TenantId = tenantId,
                           UserName = AdminUserName,
                           Name = AdminUserName,
                           Surname = AdminUserName,
                           EmailAddress = emailAddress,
                           Password = new PasswordHasher().HashPassword(password),
                           RefreshInterval = 300
                       };
            }
    

    ...but when new user accounts are created by LDAP or by admin user, the RefreshInterval is not set to 300. It is still 0 (zero).

    As a temporary workaround in my _CreateOrEditModal.cshtml I have this;

    <input id="EditUser_RefreshInterval" type="text" name="RefreshInterval" class="form-control edited" @Html.Raw(Model.User.RefreshInterval < 60 ? "value=\"300\"" : "value=\"" + Model.User.RefreshInterval + "\"") maxlength="5">
    

    It's not ideal.

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    But I offer you to set it in this constructor <a class="postlink" href="https://github.com/aspnetzero/aspnet-zero-core/blob/dev/aspnet-core/src/MyCompanyName.AbpZeroTemplate.Core/Authorization/Users/User.cs#L30">https://github.com/aspnetzero/aspnet-ze ... ser.cs#L30</a>.

    Have you tried it ?

    Thanks.