本文介绍了用类型检测自引用循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
实体:
Entities:
public class Trips
{
[Key]
public int TripID { get; set; }
public int TripNumber { get; set; }
public string TripStartDate { get; set; }
public virtual ICollection<layover> Layovers { get; set; }
}
//[SerializableAttribute]
public class layover
{
[Key]
public int ID { get; set; }
public int Sequnce { get; set; }
public string DepatureAirport { get; set; }
public string DepatureDate { get; set; }
public string DepatureTime { get; set; }
public string DepatureFlight { get; set; }
public string ArrivalAirport { get; set; }
public string ArrivalDate { get; set; }
public string ArrivalTime { get; set; }
public string ArrivalFlight { get; set; }
public string HotelCode { get; set; }
public string CrewMeal1 { get; set; }
public string CrewMeal2 { get; set; }
public string CrewMeal3 { get; set; }
public int TripRefID { get; set; }
[ForeignKey("TripRefID")]
public virtual Trips Trips { get; set; }
// public Trips Trips { get; set; }
}
DBContext::
public class HomeDBContext : DbContext
{
public HomeDBContext()
: base()
{
Database.SetInitializer<homedbcontext>(new HomeDBInitializer<homedbcontext>());
}
public DbSet<trips> Trips { get; set; }
public DbSet<layover> layover { get; set; }
}
API Controller method:
public object Get(int id)
{
HomeDBContext dbHomeDBContext = new HomeDBContext();
List<trips> lstTrips = dbHomeDBContext.Trips.ToList();
List<layover> lstlayover = dbHomeDBContext.layover.ToList();
List<trips> lstFinalTrips = (from t in lstTrips
select new Trips
{
TripID = t.TripID,
TripNumber = t.TripNumber,
TripStartDate = t.TripStartDate
,Layovers = (from l in lstlayover where l.TripRefID == t.TripID select l).ToList()
}).ToList<trips>();
dbHomeDBContext.Configuration.ProxyCreationEnabled = false;
// return lstFinalTrips;
string json = JsonConvert.SerializeObject(lstFinalTrips, Newtonsoft.Json.Formatting.Indented,
new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore });
//return json;
return lstFinalTrips;
}
Angular JS:
$scope.ServerGet = function () {
alert('Fired');
// Calling Controller Method
$http({
method: 'GET',
url: serviceBaseLocal + 'Demo/Get?id=5'
}).success(function (data) {
$scope.Trips = data;
alert(JSON.stringify($scope.Trips))
}).error(function (data) {
alert(JSON.stringify(data));
});
};
错误:
自我引用循环检测类型等。
Error:
self referencing loop detected with type etc.
推荐答案
这篇关于用类型检测自引用循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!