Answer the question
In order to leave comments, you need to log in
Entity Framework communication of elements of one table, chyadnt?
There is some table:
[Table("tags")]
public class Tag
{
[ScaffoldColumn(true)]
[StringLength(60)]
[Column("id")]
public string Id { get; set; }
/* другие поля */
public virtual ICollection<Tag> ParentTags { get; set; }
public virtual ICollection<Tag> ChildTags { get; set; }
}
parent_tag_id character varying(60) NOT NULL,
child_tag_id character varying(60) NOT NULL,
modelBuilder.Entity<Tag>()
.HasMany<Tag>(a => a.ParentTags)
.WithMany(a => a.ChildTags)
.Map(tt =>
{
tt.MapLeftKey("parent_tag_id");
tt.MapRightKey("child_tag_id");
tt.ToTable("tags_relations");
});
public ActionResult Edit([Bind(Include ="Id,Title,Visible,Description")] Tag tag,string[] ParentTags)
{
if (ModelState.IsValid)
{
var parentTags = db.Tags.Where(a => ParentTags.Contains(a.Id)).ToList<Tag>();
tag.ParentTags = parentTags;
db.Entry(tag).State = EntityState.Modified;
db.SaveChanges();
}
var tags = db.Tags.Where(a => a.Visible == false).Select(c => new {
Id = c.Id,
Title = c.Title
}).ToList();
ViewBag.Tags = new MultiSelectList(tags, "Id", "Title");
return View(tag);
}
Answer the question
In order to leave comments, you need to log in
Attention answer! =)
Logging requests to the database helped to localize the cause.
Since I'm using NLog, it's done like this:
// Добавляем объект логгера в контроллер
private static Logger logger = LogManager.GetCurrentClassLogger();
// в самом методе, чтобы отслеживать запросы из него, добавляем:
db.Database.Log = (a) => logger.Info(a);
if (ModelState.IsValid)
{
var parentTags = db.Tags.Where(a => ParentTags.Contains(a.Id)).ToList<Tag>();
tag.ParentTags = parentTags;
db.Entry(tag).State = EntityState.Modified;
db.SaveChanges();
}
if (ModelState.IsValid)
{
var parentTags = db.Tags.Where(a => ParentTags.Contains(a.Id)).ToList<Tag>();
db.Entry(tag).State = EntityState.Modified;
// перенесли добавление под предыдущую строку и добавляем так, а не приравниванием.
parentTags.ForEach(a => tag.ParentTags.Add(a));
db.SaveChanges();
}
To see what request the Entity forms, in the config, in the entity section, you need to add
<
<interceptors>
<interceptor type="System.Data.Entity.Infrastructure.Interception.DatabaseLogger, EntityFramework">
<parameters>
<parameter value="C:\temp\LogOutput.txt" />
<parameter value="true" type="System.Boolean" />
</parameters>
</interceptor>
</interceptors>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question