Zależności pomiedzy modelami

0

Cześć Mam takie dwa modele Aplication User oraz User Profile wyglądają tak:

    public class ApplicationUser : IdentityUser
    {
        public string FirstName { get; protected set; }
        public string LastName { get; protected set; }
        public long UserProfileId { get; protected set; }
        public virtual UserProfile UserProfile { get; protected set; }

        protected ApplicationUser()
        {

        }

        public ApplicationUser(string email, string userName, string firstName, string lastName)
        {
            EmailConfirmed = false;
            SecurityStamp = Guid.NewGuid().ToString();
            Email = email;
            UserName = StringFormatter.UppercaseFirst(userName);
            FirstName = StringFormatter.UppercaseFirst(firstName);
            LastName = StringFormatter.UppercaseFirst(lastName);
        }
}
    public class UserProfile
    {
        public long Id { get; protected set; }
        public string City { get; protected set; }
        public string Address { get; protected set; }
        public string AvatarUrl { get; protected set; }
        public DateTime DateOfBirth { get; protected set; }
        public UserGender UserGender { get; protected set; }
        public string UserId { get; protected set; }
        public virtual ApplicationUser User { get; protected set; }
    }

oraz tutaj jest plik konfiguracyjny:

    public class UserConfiguration : IEntityTypeConfiguration<ApplicationUser>
    {
        public void Configure(EntityTypeBuilder<ApplicationUser> builder)
        {
            builder.HasOne(x => x.UserProfile).WithOne(x => x.User).HasForeignKey<UserProfile>(x => x.Id).HasPrincipalKey<ApplicationUser>(x=>x.Id).OnDelete(DeleteBehavior.Restrict);
        }
    }
    public class UserProfileConfiguration : IEntityTypeConfiguration<UserProfile>
    {
        public void Configure(EntityTypeBuilder<UserProfile> builder)
        {
            builder.HasOne(x=>x.User).WithOne(x=>x.UserProfile).HasForeignKey<ApplicationUser>(x=>x.Id).HasPrincipalKey<UserProfile>(x => x.UserId).OnDelete(DeleteBehavior.Restrict);
        }
    }

Kurcze moje pytanie jest tak jak mogę przy tworzeniu Usera stworzyć automatycznie User Profil nie wypełniająć ten profil żadnymi danymi. TYlko utworzyć kolumnę w bazię z id usera ?

1

Zależy. Jeśli to CRUD i utworzenie profilu jest częścią tworzenia użytkownika, to pewnie w tej samej metodzie, która odpowiada za utworzenie użytkownika. Możesz też utworzyć sobie jakąś klasę UserCreator, która obsłuży cały proces tworzenia użytkownika (czyli też jego profilu).

Jak używasz MediatR, to po prostu opublikuj event UserCreated i w handlerze utwórz profil. Tak będzie najczyściej.

1 użytkowników online, w tym zalogowanych: 0, gości: 1