[英]Entity Framework Core, custom sql doesn't select expected value
I want to select a value depending on another table with a custom sql. 我想根据另一个带有自定义sql的表选择一个值。 The value is 1 if an entry in the other table exists and NULL if not. 如果另一个表中的条目存在,则值为1,否则为NULL。 The sql works in SQL Server Management Studio perfectly fine. sql在SQL Server Management Studio中可以很好地工作。 But when I run it in my application I get always NULL. 但是,当我在应用程序中运行它时,我总是得到NULL。
Here is the code of my model: 这是我模型的代码:
[Table("Projects")]
public class ProjectsModel
{
[Required(ErrorMessage = "Angabe der ID ist erforderlich.")]
[Display(Name = "Lfd")]
[Key]
public int Id { get; set; }
[Required(ErrorMessage = "Name ist erforderlich.")]
[Display(Name = "Name")]
public string Name { get; set; }
[NotMapped]
public bool? userAtProject { get; set; }
}
And here is my custom sql: 这是我的自定义sql:
var Projects = _context.Projects.FromSql("
SELECT p.Id, p.Name, (
SELECT 1 AS Expr1 FROM TableAB WHERE (aId = a.Id)
AND (UserId = '" + Id + "')) AS userAtProject
FROM Projects AS p INNER JOIN TableA AS a ON p.Name = a.Name")
.ToList();
The value of userAtProject is always NULL. userAtProject的值始终为NULL。 I hope someone can help me what I'm doing wrong here. 我希望有人可以帮助我在这里做错了什么。
If I'm right the [NotMapped] attribute says EF to not try to map/fetch that properties from database 如果我是正确的,则[NotMapped]属性表示EF不会尝试从数据库映射/获取该属性
Edit : Maybe try 编辑:也许尝试
[ReadOnly(true)]
instead? 代替?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.