Asked by:
issues in defining relationship between entities in EntityFramework core code first

Question
-
User1838940990 posted
Hi Aall,
I am completely new to Entity framework, working on sample app.
Following the .entity framework CodeFirst approach for asp.net core web api
we created two entities
public class Account { public int AccountId { get; set; } public int AccountNumber { get; set; } public DateTime CreatedDate { get; set; } public DateTime ModifiedDate { get; set; } public decimal Balance { get; set; } public int AccountTypeId { get; set; } public AccountType AccountType { get; set; } }
public class AccountType { public int AccountTypeId { get; set; } public string Type { get; set; } public string Name { get; set; } public string Description { get; set; } public DateTime CreatedDate { get; set; } public DateTime ModifiedDate { get; set; } public List<Account> Accounts { get; set; } }
These are the two entities to for simulating a bank account
We have created a APi endpint for Creating account like below
[HttpPost] public async Task<ActionResult<Account>> CreateAccount(Account account) { return await _accountService.AddAccountAsync(account); }
But the problem is the swagger is showing the parameter like below
{ "accountId": 0, "accountNumber": 0, "createdDate": "2021-04-19T04:08:22.058Z", "modifiedDate": "2021-04-19T04:08:22.058Z", "balance": 0, "accountTypeId": 0, "accountType": { "accountTypeId": 0, "type": "string", "name": "string", "description": "string", "createdDate": "2021-04-19T04:08:22.058Z", "modifiedDate": "2021-04-19T04:08:22.058Z", "accounts": [ "string" ] } }
how do I pass only the account details for creation ? because it has both account and accountype fields are present
am doing anything wrong while defining the relationship between entities.
My intention is to reference the AccountTypeId mentioned in the account so that it wont insert accountypeid other than in the accountype table
please correct me if I am doing anything wrong
thanks
Monday, April 19, 2021 4:15 AM
All replies
-
User-1044770503 posted
Hello,
You need to define a Data Transfer object (DTO) to get only the details you want from the client side. You can create a DTO class similar to the following with the required fields only.
using System.ComponentModel.DataAnnotations.Schema; [NotMapped] public class AccountDTO { public int AccountId { get; set; } public int AccountNumber { get; set; } public DateTime CreatedDate { get; set; } public DateTime ModifiedDate { get; set; } public decimal Balance { get; set; } public int AccountTypeId { get; set; } }
Then you can use that class for data transfer
public async Task<ActionResult<Account>> CreateAccount(AccountDTO account) { return await _accountService.AddAccountAsync(account); }
You can use automapper to map the AccountDTO object with the Account object on the server-side.
One issue I noticed with your Entity classes is that you are not specifying length for string fields. You can use Data Annotation Attributes to do it.
Thursday, April 22, 2021 7:00 AM -
User-1044770503 posted
Hello,
You need to define a Data Transfer object (DTO) to get only the details you want from the client side. You can create a DTO class similar to the following with the required fields only.
using System.ComponentModel.DataAnnotations.Schema; [NotMapped] public class AccountDTO { public int AccountId { get; set; } public int AccountNumber { get; set; } public DateTime CreatedDate { get; set; } public DateTime ModifiedDate { get; set; } public decimal Balance { get; set; } public int AccountTypeId { get; set; } }
Then you can use that class for data transfer
public async Task<ActionResult<Account>> CreateAccount(AccountDTO account) { return await _accountService.AddAccountAsync(account); }
You can use automapper to map the AccountDTO object with the Account object on the server-side.
One issue I noticed with your Entity classes is that you are not specifying length for string fields. You can use Data Annotation Attributes to do it.
Thursday, April 22, 2021 7:15 AM