1

I'm new to .Net. I simply created two tables using the below queries.

CREATE TABLE Users(
Id int  PRIMARY KEY NOT NULL identity(1,1),
Name varchar(50),
Email varchar(50) //Users Table
)



CREATE TABLE Cities(
Id int primary key not null identity(1,1),
City varchar(45),
Country varchar(45),
UserId int,
FOREIGN KEY(UserId) REFERENCES Users(Id) //Cities Table
)

This is a post method I created to Add a user to the db.

public async Task<IActionResult> AddUser([FromBody]User data)
        {
            try {
                var user = new User
                {
                    Name = data.Name,
                    Email = data.Email,
                };

                await dbContext.Users.AddAsync(user);
                await dbContext.SaveChangesAsync();

                return Ok(new {message= "User has been added" });
                    }
            catch(Exception ex)
            {
                return BadRequest(ex);
            }
        }

My main question is. Why it is showing the Cities table in schema in Swagger ?

enter image description here

Class definition for Users. i have removed the imports since they arent necessary

namespace CRUDRevision.Models
{
    public partial class User
    {
        public User()
        {
            Cities = new HashSet<City>();
        }

        public int Id { get; set; }
        public string? Name { get; set; }
        public string? Email { get; set; }

        public virtual ICollection<City> Cities { get; set; }
    }
}

Class Definition for Cities

namespace CRUDRevision.Models
{
    public partial class City
    {
        public int Id { get; set; }
        public string? City1 { get; set; }
        public string? Country { get; set; }
        public int? UserId { get; set; }

        public virtual User? User { get; set; }
    }
}

Looking forward for a complete answer

Muhammad Umar
  • 1,291
  • 7
  • 13
  • Can you add the class definitions for User and City? – Jim de Vries Jul 15 '22 at 14:08
  • I believe that `[FromBody]User data`, the `User` is the DB entity. Do consider using a DTO (Data Transfer Object) instead of DB entity. – Yong Shun Jul 15 '22 at 14:10
  • @Young, you are correct; this is a DB entity, not a DTO( A DTO is like a class to declare variables that we need from the front end right?) – Muhammad Umar Jul 15 '22 at 14:13
  • 1
    I see, ya, technically DTO is the class template that is used for the data transfer between the front-end and back-end/services. Design the class with the property fields that are only required for the end and prevent the exposure of unnecessary property fields and data. Refer: [What is a Data Transfer Object (DTO)?](https://stackoverflow.com/q/1051182/8017690) – Yong Shun Jul 15 '22 at 14:27

0 Answers0