Entity framework update ICollection for self referencing - asp.net-mvc

When I try update database the property Childrens not saving but SortOrder
and display name are success.
This is my model:
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Int64 Id { get; set; }
public Int64? LanguageId { get; set; }
[ForeignKey("LanguageId")]
public Language Language { get; set; }
public int SortOrder { get; set; }
public string DisplayName { get; set; }
[ForeignKey("Childrens")]
public Int64? ParentId { get; set; }
public ICollection<Menu> Childrens { get; set; }
I called this function with new parameters:
public void SaveMenu(List<Menu> newMenu)
{
foreach(Menu _addMenu in newMenu)
{
this.Update(_addMenu);
}
this.Commit();
}
public void Update(T entity)
{
try
{
DbEntityEntry _dbEntityEntry = this.DbContext.Entry(entity);
DbContext.Entry(entity).State = EntityState.Modified;
if (_dbEntityEntry.State == EntityState.Detached)
this.dbSet.Attach(entity);
_dbEntityEntry.State = EntityState.Modified;
}
}
public bool Commit()
{
try
{
this.DbContext.SaveChanges();
}
catch (Exception ex)
{
return false;
}
return true;
}
And the childrens are not change but sortOrder success to change.
Why is it happening?
EDIT:
I found solution the problem because the children objects are dummy and need to get the "real" object from the DB
public void SaveMenu(List<Menu> newMenu)
{
foreach(Menu _addMenu in newMenu)
{
List<Menu> listMenu = new List<Menu>();
foreach (Menu child in _addMenu.Childrens)
{
Menu _menuFromDB = this.Query().FirstOrDefault(s => s.Id == child.Id);
_menuFromDB.SortOrder = child.SortOrder;
listMenu.Add(_menuFromDB);
}
_addMenu.Childrens = listMenu;
this.Update(_addMenu,null);
}
this.Commit();
}

Related

Adding new entries over entity navigation property collection

I need to create a generic way to add missing languages entries to all entities in which implements an specific interface. I found out how to get my collection property, but I still don't know how to add new values on it before proceed to save.
Following a piece of my public override int SaveChanges() handling.
foreach (var translationEntity in ChangeTracker.Entries(<ITranslation>))
{
if (translationEntity.State == EntityState.Added)
{
var translationEntries = translationEntity.Entity.GetType()
.GetProperties(BindingFlags.Public | BindingFlags.Instance)
.Where(x => x.CanWrite &&
x.GetGetMethod().IsVirtual &&
x.PropertyType.IsGenericType == true &&
typeof(IEnumerable<ILanguage>).IsAssignableFrom(x.PropertyType) == true);
foreach (var translationEntry in translationEntries)
{
//Add missing items.
}
}
}
Classes code samples
public partial class FileType : ITranslation
{
public long FileTypeId { get; set; }
public string AcceptType { get; set; }
public virtual ICollection<FileTypeTranslation> FileTypeTranslations { get; set; }
public FileType()
{
this.FileTypeTranslations = new HashSet<FileTypeTranslation>();
}
}
public class FileTypeTranslation : EntityTranslation<long, FileType>, ILanguage
{
[Required]
public string TypeName { get; set; }
}
public partial class ElementType : ITranslation
{
public long ElementTypeId { get; set; }
public string Code { get; set; }
public virtual ICollection<ElementTypeTranslation> ElementTypeTranslations { get; set; }
public ElementType()
{
this.ElementTypeTranslations = new HashSet<FileTypeTranslation>();
}
}
public class ElementTypeTranslation : EntityTranslation<long, ElementType>, ILanguage
{
[Required]
public string Description { get; set; }
}
Entries from ChangeTracker have property called Entity which holds original entity
foreach (var fileType in ChangeTracker.Entries(<FileType>))
{
fileType.Entity.FileTypeTranslations.Add();
}
and for ElementType:
foreach (var elementType in ChangeTracker.Entries(<ElementType>))
{
elementType.Entity.ElementTypeTranslations.Add();
}
I didn't test, but it was too long to paste in comment.

Exception: Multiplicity constraint violated

After changing relationship of Question and PossibleAnswer from Many-to-many to One-to-many I got an exception: "The changes to the database were committed successfully, but an error occurred while updating the object context. The ObjectContext might be in an inconsistent state. Inner exception message: Multiplicity constraint violated. The role 'PossibleAnswer_Question_Source' of the relationship 'WebUI.Models.PossibleAnswer_Question' has multiplicity 1 or 0..1." What does it mean?
Here is my model:
public class Question
{
public int ID { get; set; }
public string Text { get; set; }
public bool IsAssociatedWithProfessor { get; set; }
public bool IsAssociatedWithAssistant { get; set; }
public virtual ICollection<PossibleAnswer> PossibleAnswers { get; set; }
public virtual ICollection<Results> Results { get; set; }
}
public class PossibleAnswer
{
public int ID { get; set; }
public string Text { get; set; }
public virtual Question Question { get; set; }
}
And view model for Question (I know that ToQuestion should be in controller, I will rearrange it later):
public class QuestionVM
{
public QuestionVM() {
}
public QuestionVM(Question question) : this()
{
ID = question.ID;
Text = question.Text;
IsAssociatedWithProfessor = question.IsAssociatedWithProfessor;
IsAssociatedWithAssistant = question.IsAssociatedWithAssistant;
}
public int? ID { get; set; }
public string Text { get; set; }
public bool IsAssociatedWithProfessor { get; set; }
public bool IsAssociatedWithAssistant { get; set; }
private IEnumerable<string> _possibleAnswers;
public IEnumerable<string> PossibleAnswers
{
get
{
return _possibleAnswers ?? new List<string>(){"", "", "", "", ""};
}
set
{
_possibleAnswers = value;
}
}
public Question ToQuestion()
{
Question question = new Question
{
Text = this.Text,
IsAssociatedWithProfessor = this.IsAssociatedWithProfessor,
IsAssociatedWithAssistant = this.IsAssociatedWithAssistant,
PossibleAnswers = new List<PossibleAnswer>()
};
//ID will be null if creating new question
if(ID != null)
{
question.ID = (int) ID;
}
foreach (string possibleAnswer in this.PossibleAnswers)
{
if (!String.IsNullOrEmpty(possibleAnswer))
{
question.PossibleAnswers.Add(new PossibleAnswer { Text = possibleAnswer });
}
}
return question;
}
}
Here is my post method for creating new question:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult AddQuestion(QuestionVM questionVM)
{
try
{
if (ModelState.IsValid)
{
Question question = questionVM.ToQuestion();
context.Questions.Add(question);
context.SaveChanges();
return RedirectToAction("Questions");
}
}
catch (DataException /* dex */)
{
//Log the error (uncomment dex variable name and add a line here to write a log.
ModelState.AddModelError("", "Trenutno nije moguće snimiti promjene, pokušajte ponovo.");
}
return View(questionVM);
}
Line:question.PossibleAnswers.Add(new PossibleAnswer { Text = possibleAnswer });
causes the exception since I didn't save possible answers in database before I am adding them to question... But how can I add them to database without use of DbContext (since it is not a good practice to use DbContext in method which converts view model to model)?
Error is coming because PossibleAnswers are not being marked as added entity.
So update your QuestionVM like below:
public class QuestionVM
{
public QuestionVM() {
}
public QuestionVM(Question question) : this()
{
ID = question.ID;
Text = question.Text;
IsAssociatedWithProfessor = question.IsAssociatedWithProfessor;
IsAssociatedWithAssistant = question.IsAssociatedWithAssistant;
}
public int? ID { get; set; }
public string Text { get; set; }
public bool IsAssociatedWithProfessor { get; set; }
public bool IsAssociatedWithAssistant { get; set; }
private IEnumerable<string> _possibleAnswers;
public IEnumerable<string> PossibleAnswers
{
get
{
return _possibleAnswers ?? new List<string>(){"", "", "", "", ""};
}
set
{
_possibleAnswers = value;
}
}
public Question ToQuestion()
{
Question question = new Question
{
Text = this.Text,
IsAssociatedWithProfessor = this.IsAssociatedWithProfessor,
IsAssociatedWithAssistant = this.IsAssociatedWithAssistant,
PossibleAnswers = new List<PossibleAnswer>()
};
//ID will be null if creating new question
if(ID != null)
{
question.ID = (int) ID;
}
return question;
}
public List<PossibleAnswer> GetPosibleAnswers()
{
var listOfPossibleAnswers = new List<PossibleAnswer>();
foreach (string possibleAnswer in this.PossibleAnswers)
{
if (!String.IsNullOrEmpty(possibleAnswer))
{
listOfPossibleAnswers.Add(new PossibleAnswer { Text = possibleAnswer });
}
}
return listOfPossibleAnswers;
}
}
and then update your code like below.
if (ModelState.IsValid)
{
Question question = questionVM.ToQuestion();
context.Questions.Add(question);
context.SaveChanges();
question.PossibleAnswers.AddRange(questionVM.GetPosibleAnswers());
context.SaveChanges();
return RedirectToAction("Questions");
}

MVC Updates over values with NULL

This is probably very simple for most MVC programmers, so maybe you can help me out. I have a table called Images in the database with nine columns. On my UPDATE, I just have three I want to mess with (ImageName, ImagePath, CourseID). After I post back with UPDATE, it sets the other six columns to NULL. I'm not sure how to handle this in MVC.:
My ViewModel:
public class GalleryViewModel
{
public Image _image { get; set; }
}
My Model:
public partial class Image
{
public int ImageID { get; set; }
public string ImageName { get; set; }
public string ImagePath { get; set; }
public Nullable<int> CourseID { get; set; }
public string UserId { get; set; }
public Nullable<System.DateTime> CreateDate { get; set; }
public string ImageType { get; set; }
public string ImageSize { get; set; }
public string FriendlyName { get; set; }
public virtual Cours Cours { get; set; }
}
My Controller:
[HttpGet]
public ActionResult UploadImageEdit(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
var model = new GalleryViewModel
{
_image = db.Images.Find(id),
};
return View(model);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult UploadImageEdit(GalleryViewModel galleryViewModel)
{
if (ModelState.IsValid)
{
db.Entry(galleryViewModel._image).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("UploadImageIndex");
}
return View(galleryViewModel);
}
After reading other examples and becoming more familiar with ViewModel to controller, I got the UPDATE to work by doing the following:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult UploadImageEdit(int id, GalleryViewModel galleryViewModel)
{
if (ModelState.IsValid)
{
var imageContext = db.Images.Find(id);
imageContext.FriendlyName = galleryViewModel._image.FriendlyName;
db.Entry(imageContext).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("UploadImageIndex");
}
return View(galleryViewModel);
}
I hope this helps other folks out!

ASP.NET MVC 5: Query fails to return data using IdentityDbContext in UnitOfWork Pattern

I am trying to extend the UnitOfWork pattern that I used for a number of MVC 4 applications to MVC 5 whilst also using the new IdentityDbContext and things are not working out. The problem is its very hard to debug as no errors are being generated.
First, some code.
I have a context definition that looks as follows. I have added my own DbSets to the out of the box IdentityDbContext as it makes sense to keep everything in one place.
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public DbSet<PALSOfficer> PALSOfficers { get; set; }
public DbSet<Client> Clients { get; set; }
public DbSet<GP> GPs { get; set; }
public DbSet<Surgery> Surgeries { get; set; }
public DbSet<Disability> Disabilities { get; set; }
public DbSet<Area> Areas { get; set; }
public DbSet<PALSReferral> PALSReferrals { get; set; }
public DbSet<Appointment> Appointments { get; set; }
public ApplicationDbContext()
: base("DefaultConnection")
{
}
}
I then have a UnitOfWork class that looks as follows:
public class UnitOfWork : IDisposable
{
private bool _disposed = false;
private ApplicationDbContext _context = new ApplicationDbContext();
public UserManager<ApplicationUser> _userManager { get; set; }
private PalsOfficerRepository _palsOfficerRepository;
private UserRepository _userRepository;
private GenericRepository<Area> _areaRepository;
public UserRepository UserRepository
{
get
{
if (this._userRepository == null)
{
this._userRepository = new UserRepository(_context);
}
return _userRepository;
}
}
public PalsOfficerRepository PalsOfficerRepository
{
get
{
if (this._palsOfficerRepository == null)
{
this._palsOfficerRepository = new PalsOfficerRepository(_context);
}
return _palsOfficerRepository;
}
}
public GenericRepository<Area> AreaRepository
{
get
{
if (this._areaRepository == null)
{
this._areaRepository = new GenericRepository<Area>(_context);
}
return _areaRepository;
}
}
public UserManager<ApplicationUser> UserManager
{
get
{
if (this._userManager == null)
{
this._userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(_context));
}
return _userManager;
}
}
public void Save()
{
try
{
_context.SaveChanges();
}
catch (DbEntityValidationException dbEx)
{
foreach (var validationErrors in dbEx.EntityValidationErrors)
{
foreach (var validationError in validationErrors.ValidationErrors)
{
Trace.TraceInformation("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage);
}
}
throw;
}
}
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
if (this._userManager != null)
{ this._userManager.Dispose(); }
_userManager.Dispose();
}
this._disposed = true;
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
}
So far so good. I am using code first to generate my database with automatic migrations.
My problem is that if I try to query the database, odd things happen. Take this method for instance.
var results = new SearchResults<PALSOfficer>();
var officers = from o in Context.PALSOfficers
select o;
if (!string.IsNullOrEmpty(keyword))
{
officers = (from o in officers
where o.FirstName.Contains(keyword) || o.LastName.Contains(keyword)
select o);
}
officers = officers.OrderBy(p => p.LastName);
results.Total = officers.Count();
int offset = page * display;
results.ResultList = results.Total > offset ? officers.Skip(offset).Take(display) : officers;
//results.ResultList = Context.PALSOfficers;
return results;
This returns nothing even though the database contains data. Weirdly, the count will work. If I put a breakpoint in and mouseover the results I get the message that 'children could not be evaluated'
If I simply return Context.PALSOfficers I get the rows back. Any type of manipulation of that data though (sorting etc) seems to break the query entirely.
Here is the definition of PALSOfficer
public class ApplicationUser : IdentityUser
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public DateTime Added { get; set; }
public DateTime Updated { get; set; }
}
public class PALSOfficer : ApplicationUser
{
public string InternalReference { get; set; }
public virtual ICollection<Area> Areas { get; set; }
}

EF5 ASP.NET MVC 4 Linq query not returning anything & model property null -> Code First

Im having trouble linking my loaned items to my Library for each customer. It does it fine when it goes through the "AddToLibrary" method but when it comes to retreiving it, the medialibrary is empty and the query in the IEnumerable<Item> ItemsOnLoan method is returning null. This is a very basic ASP.NET MVC 4 application and im very new to this so its probably something silly ive missed out.
I just want to be able to add an item to the loaned items table, have the list of loaned items for each customer appear in their personal Library (defined in model) and then retreive the list of their items. Below is all the code and I am using a code first approach. Thank you :)
Model
public class Customer
{
public int Id { get; set; }
public string ForeName { get; set; }
public string SurName { get; set; }
public Address address { get; set; }
public string Email { get; set; }
public string Telephone { get; set; }
public string Mobile { get; set; }
public List<LoanedItem> Library { get; set; }
public Customer()
{
if (Library == null || Library.Count == 0)
{
Library = new List<LoanedItem>();
}
}
public IEnumerable<Item> ItemsOnLoan
{
get
{
var items = (from i in Library
where i.Customer.Id == this.Id
select i).OfType<item>();
return items;
}
}
}
Loaned Item model
public class LoanedItem
{
public int? Id { get; set; }
public Customer Customer { get; set; }
public MediaItem Item { get; set; }
}
ItemController --> adding to library method
public ActionResult AddToLibrary(int id)
{
Item libraryItem = db.Items.Find(id);
Customer c = db.Customers.Find(1);
LoanedItem newLoanGame = new LoanedItem()
{
Customer = c,
Item = libraryItem
};
db.LoanedItems.Add(newLoanGame);
db.SaveChanges();
return RedirectToAction("Index");
}
Customer Controller
public ActionResult ViewProfile(int id = 1)
{
Customer c = db.Customers.Find(id);
if (c == null)
{
return HttpNotFound();
}
return View(c);
}
public ActionResult GetLibraryItems(int id = 1)
{
var items = db.Customers.Find(id).ItemsOnLoan;
return View(items);
}
Context
public class LibraryContext : DbContext
{
public DbSet<Address> Addresses { get; set; }
public DbSet<Customer> Customers { get; set; }
public DbSet<LoanedItem> LoanedItems { get; set; }
public DbSet<Item> Items { get; set; }
public LibraryContext()
: base("LbContext")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Configurations.Add(new CustomerConfiguration());
modelBuilder.Configurations.Add(new LoanedItemConfiguration());
modelBuilder.Entity<Item>();
modelBuilder.Entity<Address>();
base.OnModelCreating(modelBuilder);
}
}
Assuming that Proxy generation is enabled try this:
public class Customer
{
public int Id { get; set; }
public string ForeName { get; set; }
public string SurName { get; set; }
public Address address { get; set; }
public string Email { get; set; }
public string Telephone { get; set; }
public string Mobile { get; set; }
public virtual ICollection<LoanedItem> ItemsOnLoan { get; set; }
public Customer()
{
}
}
using this to acccess:
public ActionResult GetLibraryItems(int id = 1)
{
var customer = db.Customers.Find(id);
if (customer != null)
{
var items = customer.ItemsOnLoan;
return View(items);
}
//handle not found or throw an exception
throw new Exception();
}
follow this link for more information on Proxies and Lazy Loading.

Resources