I have a new Problem, that I don't know how it came up.
public static int SaveNewProjectGraphic(string svgString, int pageId, ApplicationDbContext db, string user = "System")
{
db = new ApplicationDbContext(); //tried with new and with db-context given from controller-action
LayoutGraphic layoutGraphic = new LayoutGraphic()
{
byUser = user,
EditorSettingId = pageId,
SvgString = svgString
};
db.LayoutGraphics.Add(layoutGraphic);
db.SaveChanges();
layoutGraphic.EditorSetting.Project.LastChange = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
layoutGraphic.EditorSetting.Project.byUser = user;
db.Entry(layoutGraphic.EditorSetting.Project).State = EntityState.Modified;
string newId = layoutGraphic.Id.ToString();
string newSvgString = svgString.Replace("newGraphicId", "g" + newId);
layoutGraphic.SvgString = newSvgString;
db.Entry(layoutGraphic).State = EntityState.Modified;
db.SaveChanges();
return layoutGraphic.Id;
}
In the static function above, I create a new record.
After db.SaveChanges() I normally got the virtual record (EditorSetting), that is associated with the new record.
Now I could use this association to Change some properties (fields) of that record.
I am 100% sure that this worked before but somehow this is broken (not only on this model), so I only get a null-reference-exception on EditorSetting (And of Course Project, which is associated with Editorsetting).
I have no idea, what I have done :-(
Here the LayouGraphic-model (Detail):
public class LayoutGraphic : BaseEntity
{
[Display(Name = "SVG")]
public string SvgString { get; set; }
public int EditorSettingId { get; set; }
public virtual EditorSetting EditorSetting{ get; set; }
}
And the associated master-model:
public class EditorSetting
{
//defaults setzen
public EditorSetting()
{
ViewBox = "0 0 600 500";
Created = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
LastChange = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
}
public int Id { get; set; }
public int ProjectId { get; set; }
public virtual Project Project { get; set; }
public string ViewBox { get; set; }
public string ViewName { get; set; }
public string Pagenumber { get; set; }
public string Created { get; set; }
public string LastChange { get; set; }
public string byUser { get; set; }
//1:n Beziehung zu Unterfunktionen
public virtual ICollection<SubFunction> SubFunctions { get; set; }
}
Any ideas? Thanks Carsten