In one application there is a database with "one to many". Models:
public class NewForm
{
public int Id { get; set; }
[Required]
public string HeadForm { get; set; }
[Required]
public string DescriptionForm { get; set; }
public virtual List<Field> Fields { get; set; }
}
public class Field
{
public int Id { get; set; }
public bool Check { get; set; }
public string HeadField { get; set; }
public int? NewFormId { get; set; }
public virtual NewForm NewForm { get; set; }
}
In other application (Web Api) I take the data using sqlconnection:
[HttpGet]
public string GetNamesByIndex(int id)
{
string connectionString = @"data source = (localdb)\MSSQLLocalDB; Initial Catalog = EditFormApplication.Models.NewFormContext; Integrated Security=True";
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
SqlCommand command = new SqlCommand();
command.CommandText = "SELECT * FROM NewForms WHERE Id = "+id+"";
command.Connection = connection;
var reader = command.ExecuteReader();
if (reader.HasRows)
{
while (reader.Read())
{
string HeadForm = reader.GetString(1);
return HeadForm;
}
}
return "false";
}
}
For example, here I return HeadForm from table "NewForm". But to call an element from the table "Field" I need to create connection for it separately. Is it possible to do this with one sqlconnection or to get the whole model or model structure? I need to send data from the database to the View in format JSON. Also I need to do it without entity framework.