.net mvc dropdownlist selected value not being updated - entity-framework

I have two models defined as follows:
public class Division
{
public int DivisionID { get; set; }
[Required]
public string DivisionName { get; set; }
public virtual Employee Contact{ set;get; }
public virtual ICollection<Employee> Employees { get; set; }
}
public class Employee
{
public int EmployeeID{ get; set; }
public string Name{ get; set; }
public virtual Division Division{set;get;}
}
Entity framework sets a field in division table called employee_employeeid, how can I create dropdown for employees for the contact attribute in the division table.
Here is what I have tried but nothing is being sent to the database.
ViewBag.contact = new SelectList(db.Employees,"EmployeeID","Name");
In the view I have:
#Html.DropDownList("contact",String.Empty)
Is there a naming convention I have to use?
Edit
POST action:
[HttpPost]
public ActionResult Edit(Division division)
{
if (ModelState.IsValid)
{
db.Entry(division).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.employeeid = new SelectList(
db.Employees, "EmployeeID", "EmployeeFirstName", division.employee);
return View(division);
}

I would recommend actually using ViewData instead of the ViewBag. In your controller have the following:
var employees = db.Employees.Select(e => new DropDownItem{ Text = e.Name, Value = e.EmployeeID });
ViewData["Employees"] = employees;
Then, in your view, have the following to display it:
#Html.DropDownList("Contact", ((IEnumerable<DropDownItem>)(ViewData["Employees"])))

I would suggest that you expose a foreign key property in your model. It makes the binding to the dropdown list and the later update much easier. Your Division model would look like this:
public class Division
{
public int DivisionID { get; set; }
[Required]
public string DivisionName { get; set; }
[ForeignKey("Contact")]
public int ContactId { set;get; }
public virtual Employee Contact { set;get; }
public virtual ICollection<Employee> Employees { get; set; }
}
Then in the Edit GET action you fill the ViewBag as you did:
ViewBag.Contacts = new SelectList(
db.Employees, "EmployeeID", "Name", division.ContactId);
You have a strongly typed view with the Division as model:
#model MyNamespace.Division
In this view you can bind the dropdown list to the ContactId property:
#Html.DropDownListFor(model => model.ContactId, ViewBag.Contacts)
Your POST action can be similar to your current version:
[HttpPost]
public ActionResult Edit(Division division)
{
if (ModelState.IsValid)
{
db.Entry(division).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.Contacts = new SelectList(
db.Employees, "EmployeeID", "Name", division.ContactId);
return View(division);
}
Note, that it is in many cases the better practice (especially for security reasons) to use a special ViewModel instead of a database entity for your views. You could then incorporate the Contacts collection into your ViewModel (instead of using the ViewBag). To update the entity in the database you would load it and write the changed properties from the ViewModel to the entity and then save it.

Related

Entity Framework Core: How do I update a record with nested fields?

I've got a simple "ContactsList" ASP.Net Core Web (REST) application, .Net Core 3.0, an MSSQL LocalDB, using MSVS 2019.
My "Contact" entity contains a list of "Notes".
When I create a new contact that already contains one or more notes, everything works fine. EF automagically inserts the notes into the notes table.
But when I try to UPDATE a contact, EF seems to disregard "notes".
Q: For "Updates", do I need write code in my controller to explicitly update the notes myself?
Or am I doing something "wrong", such that EF can't "automagically" do the updates it's supposed to?
Models/Contact.cs:
public class Contact
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int ContactId { get; set; }
public string Name { get; set; }
public string EMail { get; set; }
public string Phone1 { get; set; }
public string Phone2 { get; set; }
public string Address1 { get; set; }
public string Address2 { get; set; }
public string City { get; set; }
public string State { get; set; }
public string Zip { get; set; }
public List<Note> Notes { get; set; }
}
Models/Note.cs:
public class Note
{
public Note()
{
this.Date = DateTime.Now; // Default value: local "now"
}
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int NoteId { get; set; }
public string Text { get; set; }
public DateTime Date { get; set; }
[ForeignKey("Contact")]
public int ContactId { get; set; }
}
Controllers/ContactsController.cs (POST works: if there are notes in the contacts list, it adds them):
[HttpPost]
public async Task<ActionResult<Contact>> PostContact(Contact contact)
{
_context.Contacts.Add(contact);
await _context.SaveChangesAsync();
//return CreatedAtAction("GetContact", new { id = contact.ContactId }, contact);
return CreatedAtAction(nameof(GetContact), new { id = contact.ContactId }, contact);
}
Controllers/ContactsController.cs (PUT seems to completely disregard any assocated notes):
[HttpPut("{id}")]
public async Task<IActionResult> PutContact(int id, Contact contact)
{
if (id != contact.ContactId)
{
return BadRequest();
}
_context.Entry(contact).State = EntityState.Modified;
try
{
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!ContactExists(id))
{
return NotFound();
}
else
{
throw;
}
}
return NoContent();
}
The SQL for POST shows four separate INSERTs: one for the contact, and one for each note.
The SQL for PUT only shows one UPDATE: just the contact; nothing else.
The debugger shows "notes" are clearly part of the "Contact" record that the controller received by PutContact().
Q: Should EF deal with "updating" notes automagically, or do I need to hand-code my updates in the controller?
Entity Framework Core ignores relationships unless you explicitly include them in queries.
_context.Entry(contact).State = EntityState.Modified;
The problem with the line above is that you did not specify that the related data has been modified, so it will be ignored in the query.
So you can either
attach all the related data
set the state of the related data to EntityState.Modified
or you can
query the object in the database and include the related data
and then assign the contact object to that queried object
var dbContactObj = _context.Contacts.Include(x => x.Notes).First(x => x.Id == contact.Id);
dbContactObj = contact;
_context.SaveChangesAsync();

BreezeController Controller Issue

I have a simple BreezeController that returns a unit of work repository object. The object is a DbSet entity object of the class below:
public int OrderId { get; set; }
public string Customer { get; set; }
public virtual ICollection<OrderLine> OrderLines { get; set; }
The Unit of Work class is as follows:
private readonly EFContextProvider<ESpaDBEntities> _contextProvider;
public UoW()
{
_contextProvider = new EFContextProvider<ESpaDBEntities>();
Orders = new Repository<Order>(_contextProvider.Context);
OrderLine = new Repository<OrderLine>(_contextProvider.Context);
Products = new Repository<Product>(_contextProvider.Context);
}
public IRepository<Order> Orders { get; set; }
public IRepository<OrderLine> OrderLine { get; set; }
public IRepository<Product> Products { get; set; }
public SaveResult Commit(JObject changeSet)
{
return _contextProvider.SaveChanges(changeSet);
}
The BreezeController action is as follows:
[HttpGet]
public IQueryable<Order> Orders()
{
return uow.Orders.All();
}
When I access this method from my browser the following Json object is returned:
$id: "1",$type: "KoDurandalBreeze.DomainModel.Order, KoDurandalBreeze",OrderId: 1,Customer: "Bob",OrderLines: [ ]
For whatever reason, orderlines are not populated even though virtual is specified. Does anyone have any ideas of why the JSON object would not contain any OrderLine objects?
You will need to either perform the equivalent of an EF 'Include' on the server or if this is an EF Queryable you can call 'extend' on your client side EntityQuery, i.e.
var query = EntityQuery.from("Orders").expand("OrderDetails");
var myEntityManager.executeQuery(query).then(...)

Manually Updating a Many-to-Many Relationship in Entity Framework Code First

Although the link tables which facilitate a many-to-many relationship are usually hidden by EF, I have an instance where I think I need to create (and manage) one myself:
I have the following entities:
public class TemplateField
{
public int Id
{
get;
set;
}
[Required]
public string Name
{
get;
set;
}
}
public class TemplateFieldInstance
{
public int Id
{
get;
set;
}
public bool IsRequired
{
get;
set;
}
[Required]
public virtual TemplateField Field
{
get;
set;
}
[Required]
public virtual Template Template
{
get;
set;
}
}
public class Template
{
public int Id
{
get;
set;
}
public string Name
{
get;
set;
}
public virtual ICollection<TemplateFieldInstance> Instances
{
get;
set;
}
}
So essentially; a Template can have many TemplateField and a TemplateField can have many Template.
I believe I could just add a navigation property in the form of a collection of Template items on the TemplateField entity and have EF manage the link entity, but I need to store some additional information around the relationship, hence the IsRequired property on TemplateFieldInstance.
The actual issue I'm having is when updating a Template. I'm using code similar to the following:
var template = ... // The updated template.
using (var context = new ExampleContext())
{
// LoadedTemplates is just Templates with an Include for the child Instances.
var currentTemplate = context.LoadedTemplates.Single(t => t.Id == template.Id);
currentTemplate.Instances = template.Instances;
context.Entry(currentTemplate).CurrentValues.SetValues(template);
context.SaveChanges();
}
However; if I try and update a Template to - for example - remove one of the TemplateFieldInstance entities, it this throws an exception (with an inner exception) which states:
A relationship from the 'TemplateFieldInstance_Template'
AssociationSet is in the 'Deleted' state. Given multiplicity
constraints, a corresponding 'TemplateFieldInstance_Template_Source'
must also in the 'Deleted' state.
After doing some research, it sounds like this is because EF has essentially marked the TemplateFieldInstance foreign key to the Template as being null and then tried to save it, which would violate the Required constraint.
I'm very new to Entity Framework, so this is all a bit of a journey of discovery for me, so I'm fully anticipating there being errors in my approach or how I'm doing the update!
Thanks in advance.
You must map the relationships in your model as two one-to-many relationships. The additional field in the link table makes it impossible to create a many-to-many relationship. I would also recommend to use a composite key in your "link entity" TemplateFieldInstance where both components are foreign keys to the other entities. This ensures in the database that you can only have one row for a unique combination of a template field and a template and comes closest to the idea of a "many-to-many link table with additional data":
public class TemplateField
{
public int Id { get; set; }
[Required]
public string Name { get; set; }
public virtual ICollection<TemplateFieldInstance> Instances { get; set; }
}
public class TemplateFieldInstance
{
[Key, Column(Order = 0)]
public int FieldId { get; set; }
[Key, Column(Order = 1)]
public int TemplateId { get; set; }
public bool IsRequired { get; set; }
public virtual TemplateField Field { get; set; }
public virtual Template Template { get; set; }
}
public class Template
{
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<TemplateFieldInstance> Instances { get; set; }
}
EF naming conventions will detect the FK relations in this model if you use the property names above.
More details about such a model type are here: https://stackoverflow.com/a/7053393/270591
Your approach to update the template is not correct: context.Entry(currentTemplate).CurrentValues.SetValues(template); will only update the scalar fields of the template, not the navigation properties nor will it add or remove any new or deleted child entities of the parent entity. Unfortunately updating detached object graphs doesn't work that easy and you have to write a lot more code, something like this:
var template = ... // The updated template.
using (var context = new ExampleContext())
{
// LoadedTemplates is just Templates with an Include for the child Instances.
var currentTemplate = context.LoadedTemplates
.Single(t => t.Id == template.Id);
context.Entry(currentTemplate).CurrentValues.SetValues(template);
foreach (var currentInstance in currentTemplate.Instances.ToList())
if (!template.Instances.Any(i => i.Id == currentInstance.Id))
context.TemplateFieldInstances.Remove(currentInstance); // DELETE
foreach (var instance in template.Instances)
{
var currentInstance = currentTemplate.Instances
.SingleOrDefault(i => i.Id == instance.Id);
if (currentInstance != null)
context.Entry(currentInstance).CurrentValues.SetValues(instance);
// UPDATE
else
currentTemplate.Instances.Add(instance); // INSERT
}
context.SaveChanges();
}
A similar example with more comments what is happening is here: https://stackoverflow.com/a/5540956/270591

mvc validating binded to entitiy with reference to other entities

Hi just thought about mapping and binding my entity in controller.
How should i correctly bind entity in model so i can use modelstate
I am creating new MenuItem using MenuItemModel.
public class MenuItemModel
{
public List<SelectListItem> Menus { get; set; }
public MenuItem MenuItem { get; set; }
}
where my MenuItem class is defined as follows:
public class MenuItem:Entity
{
public virtual int MenuItemId { get; set; }
public virtual Menu Menu { get; set; }
[Required]
public virtual string Name { get; set; }
public virtual int ItemOrder { get; set; }
public virtual string ExternalUrl { get; set; }
public virtual DateTime Created { get; set; }
public virtual bool Deleted { get; set; }
public virtual DateTime? DisplayUntil { get; set; }
public virtual User Author { get; set; }
}
now when i bind my entity in controller.
//
// POST: /Administrator/MenuItem/Create
[HttpPost]
public ActionResult Create(MenuItem menuItem)
{
if (ModelState.IsValid)
{
// do saving logic
menuItem.Created = DateTime.Now;
menuItem.Author = this._userProvider.GetCurrentUser();
menuItem.Menu = _menuRepository.Load(menuItem.Menu.MenuId);
}
//restore
MenuItemModel menuItemModel = new MenuItemModel();
menuItemModel.MenuItem = menuItem;
menuItemModel.Menus =
this._menuRepository.All.Select(x => new SelectListItem() { Text = x.Name, Value = x.MenuId.ToString() }).ToList();
return View(menuItemModel);
}
the only problem is i am getting validation not for only MenuItem but for Menu, User too.
How shall set this validation to accept validation only for MenuItem Entity ?
PS i know that i can go into modelstate items and find only the entities that i need and check if they are valid but i believe there will be better way of doing this...
Any idea is appreciated.
How shall set this validation to accept validation only for MenuItem Entity ?
You should use a view model which contains only the properties that are needed to be validated in your controller action (usually those are the properties contained on the form and entered by the user). View models are classes which are specifically designed for the requirements of a given view. A controller action should never pass/take a domain model to/from a view. A controller action should always pass/take a view model to/from a view. For example:
public class MenuItemViewModel
{
public int MenuItemId { get; set; }
[Required]
public string Name { get; set; }
... put any properties that are contained on the form with their
respective validation
}
then have your POST controller action take this view model as argument:
[HttpPost]
public ActionResult Create(MenuItemViewModel viewModel)
{
if (!ModelState.IsValid)
{
// there were some validation errors => redisplay the view
// so that the user can fix them
return View(viewModel);
}
// at this stage validation went fine
// TODO: map the view model back to a domain model
// (MenutItem or whatever you are willing to update)
// I would recommend you AutoMapper for this task: http://automapper.codeplex.com/
// TODO: once you get the domain model pass it to a service layer
// method in order to perform the necessary business operation with it
// (in your case creating a menu item)
return RedirectToAction("Success");
}

Entity Framework 4 CTP 5 POCO - Many-to-many configuration, insertion, and update?

I really need someone to help me to fully understand how to do many-to-many relationship with Entity Framework 4 CTP 5, POCO. I need to understand 3 concepts:
How to config my model to indicates
some tables are many-to-many.
How to properly do insert.
How to properly do update.
Here are my current models:
public class MusicSheet
{
[Key]
public int ID { get; set; }
public string Title { get; set; }
public string Key { get; set; }
public virtual ICollection<Author> Authors { get; set; }
public virtual ICollection<Tag> Tags { get; set; }
}
public class Author
{
[Key]
public int ID { get; set; }
public string Name { get; set; }
public string Bio { get; set; }
public virtual ICollection<MusicSheet> MusicSheets { get; set; }
}
public class Tag
{
[Key]
public int ID { get; set; }
public string TagName { get; set; }
public virtual ICollection<MusicSheet> MusicSheets { get; set; }
}
As you can see, the MusicSheet can have many Authors or Tags, and an Author or Tag can have multiple MusicSheets.
Again, my questions are:
What to do on the
EntityTypeConfiguration to set the
relationship between them as well as
mapping to an table/object that
associates with the many-to-many
relationship.
How to insert a new music sheets
(where it might have multiple
authors or multiple tags).
How to update a music sheet. For
example, I might set TagA,
TagB to MusicSheet1, but later I need to change the tags to TagA
and TagC. It seems like I need
to first check to see if the tags
already exists, if not, insert the
new tag and then associate it with
the music sheet (so that I doesn't
re-insert TagA?). Or this is
something already handled by the
framework?
Thank you very much. I really hope to fully understand it rather than just doing it without fully understand what's going on. Especially on #3.
In the EF4 CTP5 the relationship is done by default convention when you put public virtual ICollection in each of the classes of the many to many relationship, as you already have done, your context class should look like this:
public class YourContextName : DbContext
{
public DbSet<MusicSheet> MusicSheets { get; set; }
public DbSet<Tag> Tags { get; set; }
public DbSet<Author> Authors { get; set; }
}
Very simple you just create a instance of the MusicSheet class and then add all the instances of you authors and tags to each of the collections of Authors and Tags in your MusicSheet, and then add your instance of MusicSheet to your context collection of MusicSheets and then call SaveChanges:
MusicSheet musicSheet = new MusicSheet
{
Title = "Music Sheet 1",
Key = "Key",
Authors = new List<Author>
{
new Author
{
Name = "Author 1",
Bio = "Author 1 biographic text..."
},
new Author
{
Name = "Author 2",
Bio = "Author 2 biographic text..."
}
},
Tags = new List<Tag>
{
new Tag {TagName = "TagA"},
new Tag {TagName = "TagC"}
}
};
var context = new YourContextName();
context.MusicSheets.Add(musicSheet);
context.SaveChanges();
To update you have to load your MusicSheet and remove the tags you don't want and then add the ones you need to add, this is how:
var context = new YourContextName();
var myMusicSheet = context.MusicSheets.First();
//The Tag you wnat to remove.
var tagToRemove = myMusicSheet.Tags.First();
var tagToAdd = new Tag {TagName = "TagX"};
myMusicSheet.Tags.Remove(tagToRemove);
myMusicSheet.Tags.Add(tagToAdd);
context.Entry(myMusicSheet).State = EntityState.Modified;
context.SaveChanges();
You can also find any author and/or tag that you know that exist and added to your MusicSheet and vice versa, but this is the foundation.
Remember this is for the EF4 CTP5 Code first...
Excuse me my English is not my main language, I hope this can help you, best regards from Dominican Republic.
PS: Don't forget to add references to EntityFramework and System.Data.Entity, is your responsibility to do anything else like unit test, validation, exception handling...etc
EDIT:
First you need to add a constructor to your models:
public class Tag
{
[Key]
public int ID { get; set; }
public string TagName { get; set; }
public Tag()
{
MusicSheets = new List<MusicSheet>();
}
public virtual ICollection<MusicSheet> MusicSheets { get; set; }
}
...Then you can do something like this:
var context = new YourContextName();
var newMusicSheet = new MusicSheet();
newMusicSheet.Title = "Newly added Music Sheet";
//Your existing Tag.
var existingTag = contex.Tags.Find(3);
existingTag.MusicSheets.Add(existingTag);
context.Entry(existingTag).State = EntityState.Modified;
context.SaveChanges();
You can do the same for all your models.
I hope this can help you!
You do not really need an EntityTypeConfiguration to set the relationship between them. It should work as it is right now. With CTP5 all you have to do to establish a many-to-many relationship is to include ICollection in both entities.
Now about how to perform inserts and deletes, there are two ways I know of. The one I usually use is create an entity for the resultant table of the many-to-many relationship, then create an instance of this entity and feed it with all the data that is required, including instances of the other entities (the ones that have the many-to-many relationship). And finally I simply add it to the repository and commit the transaction (usually using a UnitOfWork class).
Quick example:
public class Item
{
public int ID { get; set; }
public string Title { get; set; }
public virtual ICollection<Bid> Bids { get; set; }
}
public class User
{
public int ID { get; set; }
public string Username{ get; set; }
public string Email { get; set; }
public string Password { get; set; }
public virtual ICollection<Bid> Bids { get; set; }
}
public class Bid
{
public int ID { get; set; }
public float Amount { get; set; }
public DateTime Date { get; set; }
public virtual Item Item { get; set; }
public virtual User User { get; set; }
}
Then I would simply create instances of the Bid entity.
public void PlaceBid(User user, Item item, int amount)
{
if (ValidateBid(amount, user, item))
{
Bid bid = new Bid
{
Amount = amount,
Date = DateTime.Now,
User = user,
Item = item
};
try
{
repository.Add(bid);
unitOfWork.Commit();
}
catch (Exception ex)
{
//TODO: Log the exception
throw;
}
}
}