add one to one and one to many relationship

Asjad Butt 66 Reputation points
2023-02-06T10:14:32.9833333+00:00

Hi So a little weird but im trying to add a one to one and one to many relatioship between two entites lets say i have two entities 1 is branch 2 is AppUser now a branch can have a app user as the branch admin(done through roles not the elephant in the room here)and branch can have multiple appusers as a iCollection(which will be appusers with teacher role) no i try to add one to one relationship by getting appuser as a foreign key(for the one supervisor) and multiple appusers through ICollection(for the multiple teachers) no i tried doing this

    public class Branch
    {
        public int Id { get; set; }
        public string BranchName { get; set; }
        public string City { get; set; }
        public string Address { get; set; }
        public int BranchCode { get; set; }
        public Guid AppUserId { get; set; }
        public AppUser AppUser { get; set; }
        public ICollection<AppUser> AppUsers { get; set; }
        public ICollection<StudyClass> studyClasses { get; set;}
        public ICollection<Student> Students { get; set; }
    }
but i get an error when i try to add migration it creates a shadow foriegn key appuserid 
now how cain do such that i can have appuser as a foreign key in branches also have a icollection of appuser without his conflits also i have tried using many to many mmigration and i doesnot work as i try to map id to dto it returns null any help 
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,133 questions
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,240 questions
ASP.NET API
ASP.NET API
ASP.NET: A set of technologies in the .NET Framework for building web applications and XML web services.API: A software intermediary that allows two applications to interact with each other.
292 questions
{count} votes

3 answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 54,711 Reputation points
    2023-02-06T18:18:46.9166667+00:00

    the first question is can a user have a different rules in two branches? if a user has access to two branches are there two logins?

    typically in authentication, the user has roles. you also need the user to be associated with a branch. this could be an additional role or a filter on database queries.

    if the user can have different roles per branch, then you need the roles to be dynamic based on the branch.


  2. Mustafa Alakuş 0 Reputation points
    2023-02-06T18:57:09.34+00:00

    Evet satıyorum

    0 comments No comments

  3. AgaveJoe 26,181 Reputation points
    2023-02-06T20:27:56.2266667+00:00

    The problems inst roles the problems is a single branch having one appuser having role admin as the admin of the brnach and multiple other appusers with roles teachers as a list of teachers teaching at that branch

    As I understand, one branch can have many AppUsers. Each AppUser can be assigned a role; admin or teacher. The use case is each branch must have one and only one AppUser assigned to the admin role. If my understanding is correct, this is logical problem and it is up to you to come up with a design.

    Let's image you are creating a new Branch and there are no AppUser's in the Branch. What do you do in this situation? If there are no AppUsers in the Branch then how is it possible to select a admin? Do you have to create an AppUser in the admin role first? Or maybe the the application flow will force you to create an AppUser? But what about the password? Do you create or use a dummy account as a place holder?

    How is a Branch admin determined and who selects the Branch admin? A SuperAdmin?

    These are basic programming problems that you need to solve if I understand the current situation.

    0 comments No comments