1

Sry for my bad English. I'm currently learning ASP.NET Core with Angular and I have some problems.

I have a database of two tables with these fields(I deleted some fields that are not necessary to show). AccountId in Employee is a Foreign Key to PrimaryKey AccountId in Account table.

public class Employee
    {
        public int EmployeeId{get;set ;}
        public string EmployeeFullName { get; set; }
        public string PhoneNumber { get; set; }
        public int AccountId { get; set; }
    }
public class Account
    {
        public int AccountId {get;set;}
        public string Username { get; set; }
        public string Password { get; set; }
    }

How can I create models with the relationship above using Code first EF? I tried something I read on some pages like:

public Account AccountId{get;set;}

but it didn't work.

In Angular, I already have a form with those fields in Employee(except EmployeeId and AccountId). I can insert, delete, adjust, show data from SQL server Database but I can interact with one table only (Employee). Cause I created API Controller with Employee model and DbSet from DbContext.

How can I use one form from Angular but insert(API Post) to two tables at once. I want to use the form to insert employee data into the database (Employee table) and create the account (Account table) using for login with Username is PhoneNumber and a Default Password which they can change later.

3 Answers 3

2

I think you need the foreign key annotation:

  [ForeignKey(nameof(AccountId))]
  public Account Account { get; set; }
Sign up to request clarification or add additional context in comments.

Comments

2

For associating two models you could use Attribute annotation.

public class Employee
{
    public int EmployeeId{get;set ;}
    public string EmployeeFullName { get; set; }
    public string PhoneNumber { get; set; }
    public int AccountId { get; set; }
    
    [ForeignKey("AccountId")]
    public Account EmployeeAccount {get; set;}
}

public class Account
{
    public int AccountId {get;set;}
    public string Username { get; set; }
    public string Password { get; set; }
}

After changing models you have to create and update migrations.

Comments

2

Just like actionmethod you created to insert data in Employee table, create another one for insert data into account Table. Also create a Model that contains all property of both account and employee model. Then first Call the method you create for insert in account table from angular and use your newly created model(which contains both table properties ) to that method. Catch all data fron api and insert necessary data into account table. Then call the method you create for insert into employee table from your create account method and pass accountId to that method. Don't forget to add Account model in your Dbcontext also. Hope you understand.

1 Comment

I don't quite understand the process. With the Foreign Key DataAnnotation, I can create 2 Models: Employee and Account with foreign key relationship mentioned above. Using API Controller to create API methods for Employee, I can use Swagger to POST a json to Employee(include those Account field inside) and reach both table values. However in Angular, do I need two create two models and services, too? Cause I create a form, use ngForm, get formData from one Employee models only. I tried include those fields but it not working.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.