目前我正在使用[JsonIgnore]
屬性來避免模型中的循環引用。但我認爲,它將應用於所有實現。如何避免在Linq to Sql查詢中循環循環?
現在我使用像這個 -
我有兩個型號:
public class Project
{
public int ProjectID { get; set; }
[Required]
public string ProjectName { get; set; }
public DateTime? EstimatedEndDate { get; set; }
public DateTime? ProjectStartDate { get; set; }
public string OrderNumber { get; set; }
public string ProjectDescription { get; set; }
public virtual List<ServiceObject> ServiceObjects { get; set; }
[Required]
public string RegardingUser { get; set; }
public string CreatedBy { get; set; }
public DateTime CreatedDate { get; set; }
public string ModifiedBy { get; set; }
public DateTime ModifiedDate { get; set; }
public bool IsProjectDeleted { get; set; }
[NotMapped]
public int? ServiceObjectTemplateID { get; set; }
}
彼此型號是 -
public class ServiceObject
{
public int ServiceObjectID { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public string RegardingUser { get; set; }
public int? ParentServiceObjectID { get; set; }
[ForeignKey("ParentServiceObjectID")]
public virtual List<ServiceObject> ServiceObjects { get; set; }
public int ProjectID { get; set; }
[JsonIgnore]
public virtual Project Project { get; set; }<---------------------------
public virtual List<ServicePicture> ServicePictures { get; set; }
public bool IsServiceObjectDeleted { get; set; }
public string CreatedBy { get; set; }
public DateTime CreatedDate { get; set; }
public string ModifiedBy { get; set; }
public DateTime ModifiedDate { get; set; }
}
但我想申請JsonIgnore
財產或任何其他屬性在Linq to Sql中查詢自己。我的意思是我想動態應用它。
查詢是喜歡 -
var projectList = (from pro in context.Projects.All
select pro).ToList();
我希望有條件地適用。
在此先感謝。
屬性必須在編譯時應用,因此不能動態添加它們。 – TGH 2014-10-10 04:13:46
這意味着它將只適用於模型? – Abhinav 2014-10-10 04:15:34
是的,你必須在編譯時將它們應用到你的屬性。 – TGH 2014-10-10 04:16:29