我的实体Person由EF生成。我正在尝试在搜索特定字符时在自动填充文本框中显示名字,就像Facebook搜索一样。当我尝试运行时,我收到错误消息:
The entity or complex type 'Dejtingsajt3.Repositories.Person' cannot be constructed in a LINQ to Entities query.
这是我的.html-page:
中的脚本代码<script>
$("#searchInput").autocomplete({
source: function (request, response) {
$.ajax({
url: '@Url.Action("GetSearchValue", "Home")',
dataType: "json",
data: { search: $("#searchInput").val() },
success: function (data) {
response($.map(data, function (item) {
return { label: item.FirstName, value: item.FirstName };
}));
},
error: function (xhr, status, error) {
alert("Error");
}
});
}
});
</script>
这是来自控制器的方法:
public class HomeController : Controller
{
private DataContext dataContext = new DataContext();
public ActionResult Index()
{
return View();
}
public JsonResult GetSearchValue(string search)
{
List<Person> allSearch = dataContext.User.Where(n => n.FirstName
.Contains(search)).Select(x => new Person
{
FirstName = x.FirstName
}).ToList();
return new JsonResult {Data=allSearch, JsonRequestBehavior =
JsonRequestBehavior.AllowGet };
}
}
这是我的人事实体:
public class Person
{
public Person()
{
FriendRequestReceive = new HashSet<FriendRequest>();
FriendRequestSend = new HashSet<FriendRequest>();
MessageRecieve = new HashSet<Message>();
MessageSend = new HashSet<Message>();
}
[Key]
public int PersonID { get; set; }
[Required(ErrorMessage = "A first name is required.")]
[Display(Name = "First Name")]
public string FirstName { get; set; }
[Required(ErrorMessage = "A last name is required.")]
public string LastName { get; set; }
[Required(ErrorMessage = "A social number is required.")]
public string SocialNumber { get; set; }
[Required(ErrorMessage = "A gender is required.")]
public string Gender { get; set; }
[Required(ErrorMessage = "A city is required.")]
public string City { get; set; }
[EmailAddress(ErrorMessage = "Please enter a valid email.")]
public string Mail { get; set; }
public string ProfilePicture { get; set; }
public string PresentationText { get; set; }
public string Preference { get; set; }
[Required(ErrorMessage = "A username is required.")]
public string UserName { get; set; }
[Compare("Password", ErrorMessage = "A password is required")]
[DataType(DataType.Password)]
public string Password { get; set; }
public virtual ICollection<FriendRequest> FriendRequestReceive { get; set; }
public virtual ICollection<FriendRequest> FriendRequestSend { get; set; }
public virtual ICollection<Message> MessageRecieve { get; set; }
public virtual ICollection<Message> MessageSend { get; set; }
public string FileName { get; internal set; }
public string ContentType { get; internal set; }
public byte[] File { get; internal set; }
}
我已经搜索了一个解决方案并尝试了一些不同的方法,但没有任何运气。
答案 0 :(得分:0)
问题出现在代码的下一部分:
sympy
如果你试图将项目放到映射的实体上,你基本上做的是部分加载一个实体,这不是一个有效的状态。这就是您无法将查询投影到映射实体的原因。但是,您可以使用DTO:
List<Person> allSearch = dataContext
.User
.Where(n => n.FirstName.Contains(search))
.Select(x => new Person { FirstName = x.FirstName })
.ToList();
您还有其他2个选项。首先,您可以将查询映射为匿名类型:
// DTO class for projection
public class PersonDTO
{
public string FirstName { get; set; }
}
// correct query
List<PersonDTO> allSearch = dataContext
.User
.Where(n => n.FirstName.Contains(search))
.Select(x => new PersonDTO { FirstName = x.FirstName })
.ToList();
此外,您可以使用var allSearch = dataContext
.User
.Where(n => n.FirstName.Contains(search))
.Select(x => new { FirstName = x.FirstName })
.ToList();
课程(如果您需要所有人员的字段),但从查询中移除投影:
Person
答案 1 :(得分:0)
您无法创建Person
,您必须从数据库中检索。假设User
的对象类型为Person
,请离开Select
:
var allSearch = dataContext.User
.Where(n => n.FirstName.Contains(search))
.ToList();