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 ?
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