【发布时间】:2012-12-13 18:02:28
【问题描述】:
是否可以将连接结果存储在模型中的两个/多个表上? 例如 说我有模型
public class model1{
public string studentID {get; set;}
public string Address [get; set;}
}
public class model2{
public int ID {get; set;}
public string StudentName [get; set;}
}
public class model3{
public string StudentName [get; set;}
public string Address [get; set;}
}
如果 model1 和 model2 从数据库中提取数据,我可以通过连接 model1 和 model2 的结果来创建 model3 吗?
编辑
所以,我做了类似的事情
model3object = dbcontext.Model3.AsQueryable();
model3object = from m1 in dbcontext.model1
let id = dbcontext.Model1.Select(x => m1.studentId).Cast<int>().FirstOrDefault()
join m2 in dbcontext.model2
on id equals m2.Id
select new Result
{
studentName = m2.studentName,
Address = m1.Adress,
};
但我得到了错误
System.NotSupportedException: The entity or complex type 'ProjectName.Models.Model3' cannot be constructed in a LINQ to Entities query.
请问我该如何解决这个问题?
【问题讨论】:
-
模型不会从数据库中提取数据,基本上是 Controller 做的。您可以在控制器中运行连接查询并使用结果数据填充 Model3 的对象并将其传递给视图以供使用。
标签: asp.net-mvc-3 join model