The entity or complex type cannot be constructed in a LINQ to Entities query

You cannot (and should not be able to) project onto a mapped entity. You can, however, project onto an annonymous type or onto a DTO:

public class ProductDTO
    public string Name { get; set; }
    // Other field you may need from the Product entity

And your method will return a List of DTO’s.

public List<ProductDTO> GetProducts(int categoryID)
    return (from p in db.Products
            where p.CategoryID == categoryID
            select new ProductDTO { Name = p.Name }).ToList();



Another Solution



How do I make EditorFor conditionally readonly?





@Html.EditorFor(model => model.Quantity, ViewBag.Readonly ? (object)new { htmlAttributes = new { @readonly = "readonly", @class = "form-control" }} : new { htmlAttributes = new { @class = "form-control" } })




MVC Project from github


## EmployeeManagement Demo Application
This repository contains a multi-layer application which was built for demonstration purposes.
It contains a collection of patterns and services which are used in enterprise applications.
– Layered architecture
– Dependency injection using AutoFac
– Test-driven design
– EntityFramework code-first database context
– UnitOfWork and repository pattern
– Restful WCF service with JSON
– Diverse client technologies. Using DTOs to exchange information with the backend and ViewModels (MVVM) to bind model data to views.

Furthermore, the aim of this project was to show the interplay of the mentioned technologies, principles and practices.

### Missing points
– Authentication and authorization
– Cross-platform mobile clients
– Localization
– Input validation
– Filtering, grouping, sorting, paging of list items

### Helpful links

Generic repository pattern, unit of work, entity framework in MVC

Autofac for MVC

Autofac for WebApi

Using Web API 2 with Entity Framework 6

Dependency Injection in ASP.NET Web API 2

Exception Handling in ASP.NET Web API

Automated Testing of ASP.NET Web API and MVC applications

Creating WCF REST services

Using MVVMLight in a WPF application

Configuring Relationships with the Fluent API

ASP.NET Web API Exception Handling


Exception Handling in WebAPI

Consuming WCF Services in Xamarin–part-1.aspx–part-2.aspx

Custom WCF message formatters (NewtonsoftJsonBehavior)

How to add a second css class with a conditional value in razor MVC 4

Your answer (assuming this works, I haven’t tried this):

<div class="details @(@Model.Details.Count > 0 ? "show" : "hide")">

Second option:

@if (Model.Details.Count > 0) {
    <div class="details show">
else {
    <div class="details hide">

Third option:

<div class="@("details " + (Model.Details.Count>0 ? "show" : "hide"))">

Binding arrays in ASP.NET MVC without index?

You don’t need to explicitly index flat data. If in your view you have

<input type='text' name='data' value='George' />
<input type='text' name='data' value='John' />
<input type='text' name='data' value='Paul' />
<input type='text' name='data' value='Ringo' />

Then in your controller you can use

public ActionResult Create(string[] data)
    //data should now be a string array of 4 elements
    return RedirectToAction("Index");