Loop referring to objects in entity framework

0

I have a relationship that goes like this: A sale has a business (the business where the sale was made)

Then I have the business entity by itself, it is not related to sales.

Now, I made a change in the project, and I have to list the Businesses with their sales, so what I did was to the business entity to include a sales collection.

What happens is that when you run it, the business looks for your sales, and in turn that sale, looks for a business, and so I have a loop and I get an error (Self referencing loop detected with type)

Now, there is some way of telling the Business entity that when you go to look for your sales, do not bring the business (since you already have it because it is the same) I hope it has been understood, I am using Entity Framework 6 with Anotations and Code First. Thanks

    
asked by Lucho 01.02.2018 в 14:01
source

1 answer

0

Use this example that I created so you can see how you can do what you need

public class Negocio
{
    public int Id { get; set; }
    [Required(ErrorMessage = "El campo Nombre es obligatorio")]
    public string Nombre { get; set; }

    public virtual ICollection<Venta> Ventas { get; set; }
}

public class Venta
{
    public int Id { get; set; }
    public string Producto { get; set; }
    public int Cantidad { get; set; }
    public double Saldo { get; set; }
    public int NegocioId { get; set; }

    public virtual Negocio Negocio { get; set; }
}

now in your controller Business in the Index action you must change the query so that EF takes the sales data of each business.

 // GET: Negocios
    public async Task<IActionResult> Index()
    {
        var negocios = await _context.Negocio.Include(n => n.Ventas).ToListAsync();
        return View(negocios);
    }

and in your view you must go through the sales of each business to show them example:

 @model IEnumerable<WebApplication3.Models.Negocio>
@{
    ViewData["Title"] = "Index";
}
<h2>Index</h2>
<p>
    <a asp-action="Create">Create New</a>
</p>
<table class="table">
    <thead>
        <tr>
            <th>
                @Html.DisplayNameFor(model => model.Nombre)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Ventas)
            </th>
            <th></th>
        </tr>
    </thead>
    <tbody>
        @foreach (var item in Model)
        {
            <tr>
                <td>
                    @Html.DisplayFor(modelItem => item.Nombre)
                </td>
                <td>
                    //Aqui recorres las ventas de cada Negocio para mostrarlas
                    @foreach(var venta in item.Ventas)
                    {
                        <p>@venta.Producto</p>
                    }
                </td>
                <td>
                    <a asp-action="Edit" asp-route-id="@item.Id">Edit</a> |
                    <a asp-action="Details" asp-route-id="@item.Id">Details</a> |
                    <a asp-action="Delete" asp-route-id="@item.Id">Delete</a>
                </td>
            </tr>
        }
    </tbody>
</table>

This example has been done in Asp.Net core but the idea is the same, you can use it in another project. I hope you find it useful

    
answered by 01.02.2018 в 14:55