c# – problems with REST output

Question:

There are 2 tables:

Meeting (id, subject, idPlace).

Location (id, address). Linked by the idPlace -> id field.

Models are created for these tables:

public class Place  
{  
    public int Id { get; set; }  
    public string Address { get; set; }  
} 
public class Meeting  
{   
    public int Id { get; set; }  
    public string Theme { get; set; }  
    public Place FPlace { get; set; }  
}  

Repository:

public Meeting GetMeeting(int id)  
    {  
        using (var connection = new SqlConnection(_connectionString))  
        {  
            connection.Open();  
            using (var command = connection.CreateCommand())  
            {  
                command.CommandText = "SELECT m.id, m.theme,     
                                       p.address " +  
                                      "FROM [dbo].meeting AS m " +  
                                      "INNER JOIN [dbo].place AS p " +  
                                      "ON m.place_id = p.id " +  
                                      "WHERE m.id = @id";  

                command.Parameters.AddWithValue("@id", id);
                using (var reader = command.ExecuteReader())
                {
                    if (reader.Read())
                    {
                        return new Meeting
                        {
                            Id = (int)reader["id"],
                            Theme = (string)reader["theme"],
                            Time = (DateTime)reader["time"],
                            Duration = (TimeSpan)reader["duration"],
                            FPlace = new Place
                            {
                                Address = (string)reader["address"]
                            }
                        };
                    }
                    return null;
                }
            }
        }
    }

Well, Rest itself:

[HttpGet]
[Route("api/meeting/get/{id}")]
public Meeting GetMeeting(int id)
{
    return _repository.GetMeeting(id);
}

In my opinion, the problem lies here

FPlace = new Place  
{  
     Address = (string)reader["address"]  
}     

With a GET request ( api/meeting/get/{id} ), it returns not only the data from the Meeting table, the address from the Place table, but also Id = 0 (from the Place table).

How to get rid of it (Id)?

Answer:

Option A : as already mentioned in the comments

public class Place  
{  
    [IgnoreDataMember]
    public int Id { get; set; }  
    public string Address { get; set; }  
} 

Option B :

var meet = _repository.GetMeeting(id);
return new { id = meet.Id, theme = a.Theme, place = meet.FPlace.Address};

it turns out a flat list with no frills of course it would be nice to make sure that meet is not null

Scroll to Top