Base solution for your next web application
Open Closed

Name and Surname optional #321


User avatar
0
cosmic created

Hello, is it possible to set the User.Name and User.Surname as optional? How can I achieve this? Currently both are marked as Required. I need only UserName and EmailAddress to be required.

I have tried to override and to create new properties without Required attribute in a User class, but without success.

public class User : AbpUser<Tenant, User>
{
    ...
    public override string Name { get; set; }  // Not working
    ...
    public new string Name { get; set; }  // Not working
    ...
}

I have also tried to set properties as nullable through the EF Fluent API.

modelBuilder.Entity<User>().Property(e => e.Name).IsOptional();  // Not working

It will always return an exception:

Abp.Web.Mvc.Controllers.AbpHandleErrorAttribute|System.Data.Entity.Validation.DbEntityValidationException: Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.

5 Answer(s)
  • User Avatar
    0
    hikalkan created
    Support Team

    Write a '?' to Name and Surname. It will not be a problem if you will now show they anywhere.

  • User Avatar
    0
    cosmic created

    If you mean this

    public override string? Name { get; set; }
    

    then it doesn't work. Only non-nullable value type could be underlying of System.Nullable.

  • User Avatar
    0
    hikalkan created
    Support Team

    No, just in your User constructor;

    Name = "?"; Surname = "?";

    That's all. Maybe it's not a perfect solution but it works :)

  • User Avatar
    0
    AstarIT created

    Can you share where exactly we should use Surname="?"; I do not see where, I try it here and its not working Core\Authorization\Users\User.cs

    I need the Host and Tenants do not use Require Settings on Surname.

    All help will be really appreciated.

  • User Avatar
    0
    AstarIT created

    I also find out in the link before that in the UserManager can be done this public override Task<IdentityResult> CreateAsync(User user)

    {
    user.Name = user.Name ?? String.Empty; user.Surname = user.Surname ?? String.Empty; return base.CreateAsync(user); }

    I used this but still have no results.

    https://github.com/aspnetboilerplate/aspnetboilerplate/issues/1274