我有两个班级:
public partial class Registry
{
[Key]
public int Id { get; set; }
public string Title{ get; set; }
[NotMapped]
public virtual Category Category { get; set; }
}
public partial class Category
{
[Key]
public int Id { get; set; }
public string TitleSearch { get; set; }
public string Name{ get; set; }
}
我想使用Registry.Category
运算符填充Registry.Title
Category.SearchTitle
包含Like
的第一个类别。
当我调用Registry.Category
时,可以自动建立此链接,并查看某个类别是否为空?
我想这样做:
if(registry.Category != null){
categoryName = registry.Category.Name;
}
更新 我尝试了这个,它起作用了:
[NotMapped]
public virtual Category Category {
get
{
Context db = new Context();
var category = db.Categories.Where(e=> Title.Contains(e.TitleSearch)).FirstOrDefault();
return category;
}
}