I am receiving this error:
The ViewData item that has the key 'DepartmentId' is of type 'System.Int32' but must be of type 'IEnumerable'.
with the following set up. I am not sure how to resolve it. The error is happening in the Model View code. This line: public void MapTo(Person domainModel). I am using AutoMapper to map ViewModel back to DomainModel (reversing the initial mapping of DomainModel to ViewModel).
Domain model (using LINQ to SQL, so this is a partial class):
public partial class Person { }
// Validation rules
public class Person_Validation
{
[HiddenInput(DisplayValue = false)]
[ScaffoldColumn(false)]
public object PersonId { get; set; }
[HiddenInput(DisplayValue = false)]
[ScaffoldColumn(false)]
public object DepartmentId { get; set; }
[DisplayName("Employee Name")]
[Required(ErrorMessage = "Employee Name is required")]
[StringLength(50, ErrorMessage = "Employee Name cannot be more than 50 characters")]
public object Name { get; set; }
[HiddenInput(DisplayValue = false)]
public object Active { get; set; }
[HiddenInput(DisplayValue = false)]
public object DateAdded { get; set; }
[HiddenInput(DisplayValue = false)]
public object DateDeleted { get; set; }
public object Department { get; set; }
}
This is my Model View:
public class PersonViewModel
{
public object PersonId { get; set; }
public object DepartmentId { get; set; }
public object Name { get; set; }
public object Active { get; set; }
public object DateAdded { get; set; }
public object DateDeleted { get; set; }
public object DepartmentName { get; set; }
//helper method
public void MapTo(Person domainModel)
{
Mapper.Map(this, domainModel);
}
}
Controller Class Code:
[HttpPost]
public ActionResult Edit(PersonViewModel viewModel)
{
var domainModel = new Person();
try
{
viewModel.MapTo(domainModel);
UpdateModel(domainModel);
_personRepository.Save();
return RedirectToAction("Index", "Person");
}
catch
{
return View(viewModel);
}
}
And my View HTML code:
<div class="editor-field">
<%: Html.DropDownList("DepartmentId", (IEnumerable<SelectListItem>)ViewData["DepartmentList"])%>
<%: Html.ValidationMessageFor(model => model.DepartmentId) %>
</div>
you're not really following the best practices of developing a mvc application.
About the error:
the Html.DropDownList looks for Data of type IEnumerable<SelectListItem> in the model but it finds an int instead (DepartmentId)
your ViewModel should not have the MapTo method, it breaks the single responsibility principle
in your action method you don't do any server side validation like:
if(!ModelState.IsValid)
{
//rebuild the viewmodel and return the view
}
catching everything in the action is also not necessary (and bad)
you do this in Global.asax Application_Error instead
attributes like HiddenInput, ScaffoldColumn, Validation and anything else UI related should be on you ViewModel not in your domain model
for a good sample of using viewmodels & validation & mapping between entity <-> viewmodel I recommend you to look at the Samples solution from here
I did this sample and it's main purpose is to demonstrate the usage of ValueInjecter (mapping technology)
Related
I have SQLite db and these EF models and context.
Models and Context
public class CardHolder
{
public int CardHolderId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string PhoneNumber { get; set; }
public string EmailAddress { get; set; }
public string TenantName { get; set; }
public ICollection<AccessCard> AccessCards { get; set; }
}
public class AccessCard
{
public int AccessCardId { get; protected set; }
public CardHolder CardHolder { get; set; }
public DateTime ActivationDate { get; protected set; }
public bool ActivationProcessed { get; set; }
public DateTime? DeactivationDate { get; protected set; }
public string DeactivationReason { get; set; }
public bool DeactivationProcessed { get; set; }
}
public class MyContext : DbContext
{
public DbSet<CardHolder> CardHolders { get; set; }
public DbSet<AccessCard> AccessCards { get; set; }
}
And the Main program
class Program
{
static void Main(string[] args)
{
using (var db = new MyContext())
{
var cardHolders = db.CardHolders.Include("AccessCard").ToList();
}
}
}
Question1: Why do I get this exception
System.InvalidOperationException: 'A specified Include path is not
valid. The EntityType 'SQLiteDemo.Models.CardHolder' does not declare
a navigation property with the name 'AccessCard'.'
If I replace it with
var cardHolders = db.CardHolders.Include("AccessCards").ToList();
I get another error:
SQL logic error no such column: Extent2.CardHolder_CardHolderId
What is wrong with Entity Framework?
Question2: Why cant I use arrow function in Include statement, it doesnt compile at all?
var cardHolders = db.CardHolders.Include(x => x.AccessCards).ToList();
Question3: Why do I need to use Include at all if my ICollection association property AccessCards is NOT virtual - that means eager loading must work by itself!
Why the hell it is so problematic and buggy? Nothing works as it should :(
1 - You have a typo as you have already determined :)
1B - "SQL logic error no such column: Extent2.CardHolder_CardHolderId"
EF isn't finding your FK. You could add it to your AccessCard model:
public int CardHolderId { get; set; }
2 - You need to pull in the LINQ extensions. Make sure you have both of these using statements at the top:
using System.Data.Entity;
using System.Linq;
3 - You, like many others, are misunderstanding lazy loading. Eager loading still requires an Include() to fetch related data. Lazy loading only fetches the relations when you access them.
The method is really simple and I don't see what am I missing...
public int SaveEvent(Data.Models.Event evnt)
{
db.Events.Add(evnt);
db.SaveChanges();
return evnt.EventId;
}
here is the object declaration:
public class Event
{
public int EventId { get; set; }
public string Name { get; set; }
public ICollection<EventTag> EventTags { get; set; }
}
The evnt object contains a property name EventTags that contains 6 new elements.
The evnt is inserted in the database but not the EventTag... any idea ? no error nothing. just the EventTag are not added...
public class EventDbContext : DbContext
{
public DbSet<Event> Events { get; set; }
public DbSet<EventTag> EventTags { get; set; }
public DbSet<Tag> Tags { get; set; }
}
Here is a screenshot of the value:
If the EventTags are not being added to the database you may need to manually specify the EntityState for each tag.
public int SaveEvent(Data.Models.Event evnt)
{
foreach(var tag in evnt.EventTags)
{
db.Entry(tag).State = EntityState.Added;
}
db.Events.Add(evnt);
db.SaveChanges();
return evnt.EventId;
}
You might also want to update your class definition and set the EventTags property as virtual.
public class Event
{
public int EventId { get; set; }
public string Name { get; set; }
public virtual ICollection<EventTag> EventTags { get; set; }
}
In your screenshot it looks like the tags are loading, but not the Location property on the tags. If that's the case, then make sure to set the Location property to virtual as well.
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(...)
I am using Entity Framework 6 with Generic Repository and DTOs.
I want to create new entities via navigation property.
Here is my model:
public partial class Project
{
public Project()
{
this.ProjectAssets = new List<ProjectAsset>();
}
public int ProjectID { get; set; }
public string Name { get; set; }
public virtual ICollection<ProjectAsset> ProjectAssets { get; set; }
}
public partial class Asset
{
public Asset()
{
this.Revisions = new List<Revision>();
}
public int AssetID { get; set; }
public string Name { get; set; }
public short Type { get; set; }
public virtual ICollection<Revision> Revisions { get; set; }
}
public partial class ProjectAsset
{
public int MappingID { get; set; }
public int ProjectID { get; set; }
public int AssetID { get; set; }
public virtual Asset Asset { get; set; }
}
I have already created Project. And if i am creating new Asset, then create Project Asset with AssetID from just created Asset, it's OK, but i have to re-fetch Project from DB.
I want to do it in one transaction, like that:
Project.ProjectAssets.Add(new ProjectAsset(new Asset((short)type, fileName)));
ServiceLocator.Default.ResolveType<IPipeLine>().Update(Project);
public void Update<TEntity>(TEntity entity) where TEntity : class
{
var fqen = GetEntityName<TEntity>();
object originalItem;
var key = ((IObjectContextAdapter)DbContext).ObjectContext.CreateEntityKey(fqen, entity);
if (((IObjectContextAdapter)DbContext).ObjectContext.TryGetObjectByKey(key, out originalItem))
((IObjectContextAdapter)DbContext).ObjectContext.ApplyCurrentValues(key.EntitySetName, entity);
//DbContext.Entry(entity).State = EntityState.Modified;
}
But after SaveChanges there is no record in DB, and MappingID still 0.
I thought that ApplyCurrentValues must work with Navigation Properties.
Is there any good way to solve that problem?
EDIT:
I accessing DAL throughBusiness Entities with contain the same properties, but they also implement INotifyPropertyChanged and other WPF stuff. So i think i can subscribe to CollectionChanged event and manualy create/delete entities from navigation property. And in property setters i can call update, but i think it can strongly decrease perfomance.
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.