Reputation: 49
I am learning MVC and for do this I am developing a "smart forum". I have do a database but I have some problem with entities. I have do this command
"Scaffold-DbContext "Server=(localdb)\mssqllocaldb;Database=SmartForum;Trusted_Connection=True; Microsoft.EntityFrameworkCore.SqlServer -OutputDir ModelsFromDb" ,
A code snippet:
modelBuilder.Entity<ArgomentiPerArea>(entity =>
{
entity.HasKey(e => e.ArgomentoId);
entity.Property(e => e.ArgomentoId).HasColumnName("argomentoId");
entity.Property(e => e.Archiviato).HasColumnName("archiviato");
entity.Property(e => e.AreaId).HasColumnName("areaId");
entity.Property(e => e.ModeratoreId).HasColumnName("moderatoreId");
entity.Property(e => e.NomeArgomento).HasColumnName("nome_argomento");
entity.Property(e => e.NumeroRigaPerArea).HasColumnName("numero_riga_per_area");
entity.Property(e => e.TestoPerArgomento).HasColumnName("testo_per_argomento");
entity.HasOne(d => d.Area)
.WithMany(p => p.ArgomentiPerArea)
.HasForeignKey(d => d.AreaId)
.HasConstraintName("FK_ArgomentiPerArea_Aree");
entity.HasOne(d => d.Moderatore)
.WithMany(p => p.ArgomentiPerArea)
.HasForeignKey(d => d.ModeratoreId)
.HasConstraintName("FK_ArgomentiPerArea_Moderatori");
});
a second snippet :
public partial class ArgomentiPerArea
{
public ArgomentiPerArea()
{
Thread = new HashSet<Thread>();
}
[Key]
public int ArgomentoId { get; set; }
public string NomeArgomento { get; set; }
public int? AreaId { get; set; }
public bool? Archiviato { get; set; }
public int? NumeroRigaPerArea { get; set; }
public string TestoPerArgomento { get; set; }
public int? ModeratoreId { get; set; }
public virtual Aree Area { get; set; }
public virtual Moderatori Moderatore { get; set; }
public virtual ICollection<Thread> Thread { get; set; }
}
public partial class Aree
{
public Aree()
{
ArgomentiPerArea = new HashSet<ArgomentiPerArea>();
}
[Key]
public int AreaId { get; set; }
public string NomeArea { get; set; }
public int? NumeroRiga { get; set; }
public int? NumeroColonna { get; set; }
public virtual ICollection<ArgomentiPerArea> ArgomentiPerArea { get; set; }
}
public partial class Moderatori
{
public Moderatori()
{
ArgomentiPerArea = new HashSet<ArgomentiPerArea>();
SegnalazioniPerModeratori = new HashSet<SegnalazioniPerModeratori>();
}
[Key]
public int ModeratoreId { get; set; }
public string UsernameModeratore { get; set; }
public string PasswordHash { get; set; }
public string NomeCognome { get; set; }
public bool? Archiviato { get; set; }
public virtual ICollection<ArgomentiPerArea> ArgomentiPerArea { get; set; }
public virtual ICollection<SegnalazioniPerModeratori> SegnalazioniPerModeratori { get; set; }
}
when this code run
public class ArgomentiPerAreasController : Controller
{
private ModelsFromDb.SmartForumContext db = new ModelsFromDb.SmartForumContext();
// GET: ArgomentiPerAreas
public ActionResult Index()
{
var argomentiPerAreas = db.ArgomentiPerArea.Include(a => a.Area).Include(a => a.Moderatore);
string msg = "m";
return View(argomentiPerAreas.ToList());
}
.............
.............}
I check in view and "moderatore" and "area" have null value. I don't understand but I know database first and MVC superficially. I hope in some suggestions.
Upvotes: 0
Views: 53
Reputation: 34783
This is likely due to circular references between your Argomenti* & the Moderator/Area. An Area holds a collection back to the Argomenti* so when MVC goes to serialize the root entity (Argomenti) it comes across the Area, then iterating through the area, a collection of Argomenti*, and co the cycle goes. It bails out and doesn't attempt to serialize the cyclical dependencies.
Generally the best thing to do with EF and views is not to attempt to send entities to the view. Instead, create a POCO (Plain old C# object) View Model to send to the view. This view model contains only the fields needed by the view, and your EF query uses .Select()
to populate that view model. This avoids the whole cyclic reference issue, and negates the need for deliberate eager loading (.Include()
) or the performance risk of lazy loading.
For example: If I want a list of Argumenti, and I want to display each Area and Moderator as part of that:
[Serializable]
public class ArgumentiViewModel
{
public string NomeArgomento { get; set; }
public bool? Archiviato { get; set; }
public int? NumeroRigaPerArea { get; set; }
public string TestoPerArgomento { get; set; }
public string NomeArea { get; set; } // From Area
public string NomeCognome { get; set; } // From Moderator
}
Then when I want to return this to the view:
var argomentiViewModels = db.ArgomentiPerArea
.Select(x => new ArgomentiViewModel
{
NomeArgomento = x.NomeArgomento,
Archiviato - x.Archiviato,
NumeroRigaPerArea = x.NumeroRigaPerArea,
TestoPerArgomento = x.TestoPerArgomento,
NomeArea = x.Area.NomeArea, // From Area
NomeCognome = x.Moderatori.NomeCognome // From Moderator
}).ToList();
string msg = "m";
return View(argomentiViewModels);
I sumarized a couple good reasons why code should not return Entities to the view here.
Upvotes: 1