EF 4.1 - Code First - JSON Circular Reference Serialization Error - c#

I am getting an a Circular Reference Serialization Error although, to my knowledge I do not have any circular references. I am retrieving a set of Orders from the database and sending them to the client as JSON. All the code is shown below.
This is the error:
Error
A circular reference was detected
while serializing an object of type
'System.Data.Entity.DynamicProxies.Order_83CECF2AA4DE38232F9077D4B26941AB96BC61230419EA8AC42C9100E6072812'.
Description: An unhandled exception
occurred during the execution of the
current web request. Please review the
stack trace for more information about
the error and where it originated in
the code.
Exception Details:
System.InvalidOperationException: A
circular reference was detected while
serializing an object of type
'System.Data.Entity.DynamicProxies.Order_83CECF2AA4DE38232F9077D4B26941AB96BC61230419EA8AC42C9100E6072812'.
Source Error:
An unhandled exception was generated
during the execution of the current
web request. Information regarding the
origin and location of the exception
can be identified using the exception
stack trace below.
My classes are as follows:
Order
public class Order
{
[Key]
public int OrderId { get; set; }
public int PatientId { get; set; }
public virtual Patient Patient { get; set; }
public int CertificationPeriodId { get; set; }
public virtual CertificationPeriod CertificationPeriod { get; set; }
public int AgencyId { get; set; }
public virtual Agency Agency { get; set; }
public int PrimaryDiagnosisId { get; set; }
public virtual Diagnosis PrimaryDiagnosis { get; set; }
public int ApprovalStatusId { get; set; }
public virtual OrderApprovalStatus ApprovalStatus { get; set; }
public int ApproverId { get; set; }
public virtual User Approver { get; set; }
public int SubmitterId { get; set; }
public virtual User Submitter { get; set; }
public DateTime ApprovalDate { get; set; }
public DateTime SubmittedDate { get; set; }
public Boolean IsDeprecated { get; set; }
}
Patient
public class Patient
{
[Key]
public int PatientId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string MiddleInitial { get; set; }
public bool IsMale;
public DateTime DateOfBirth { get; set; }
public int PatientAddressId { get; set; }
public Address PatientAddress { get; set; }
public bool IsDeprecated { get; set; }
}
Certification Period
public class CertificationPeriod
{
[Key]
public int CertificationPeriodId { get; set; }
public DateTime startDate { get; set; }
public DateTime endDate { get; set; }
public bool isDeprecated { get; set; }
}
Agency
public class Agency
{
[Key]
public int AgencyId { get; set; }
public string Name { get; set; }
public int PatientAddressId { get; set; }
public virtual Address Address { get; set; }
}
Diagnosis
public class Diagnosis
{
[Key]
public int DiagnosisId { get; set; }
public string Icd9Code { get; set; }
public string Description { get; set; }
public DateTime DateOfDiagnosis { get; set; }
public string Onset { get; set; }
public string Details { get; set; }
}
OrderApprovalStatus
public class OrderApprovalStatus
{
[Key]
public int OrderApprovalStatusId { get; set; }
public string Status { get; set; }
}
User
public class User
{
[Key]
public int UserId { get; set; }
public string Login { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string NPI { get; set; }
public string Email { get; set; }
}
NOTE: ADDRESS CLASS IS NEW ADDITION DURING EDIT
Address
public class Address
{
[Key]
public int AddressId { get; set; }
public string StreetAddress { get; set; }
public string City { get; set; }
public string State { get; set; }
public string Zip { get; set; }
public string Phone { get; set; }
public string Title { get; set; }
public string Label { get; set; }
}
The code that executes the serialization is here:
Excerpt from OrderController
public ActionResult GetAll()
{
return Json(ppEFContext.Orders, JsonRequestBehavior.AllowGet);
}
Thanks

You could try to remove the virtual keyword from all navigation properties to disable lazy loading and proxy creation and then use eager loading instead to load the required object graph explicitely:
public ActionResult GetAll()
{
return Json(ppEFContext.Orders
.Include(o => o.Patient)
.Include(o => o.Patient.PatientAddress)
.Include(o => o.CertificationPeriod)
.Include(o => o.Agency)
.Include(o => o.Agency.Address)
.Include(o => o.PrimaryDiagnosis)
.Include(o => o.ApprovalStatus)
.Include(o => o.Approver)
.Include(o => o.Submitter),
JsonRequestBehavior.AllowGet);
}
Referring to your previous post it looks like your application isn't relying on lazy loading anyway because you introduced there the virtual properties to load the object graph lazily, possibly causing now the serialization trouble.
Edit
It's not necessary to remove the virtual keyword from the navigation properties (which would make lazy loading completely impossible for the model). It's enough to disable proxy creation (which disables lazy loading as well) for the specific circumstances where proxies are disturbing, like serialization:
ppEFContext.Configuration.ProxyCreationEnabled = false;
This disables proxy creation only for the specific context instance ppEFContext.
(I've just seen, #WillC already mentioned it here. Upvote for this edit please to his answer.)

When you know that you need to serialize from a particular context, you can disable proxy creation for that particular query like below. This has worked for me and is better than revising my model classes.
using (var context = new MeContext())
{
context.Configuration.ProxyCreationEnabled = false;
return context.cars.Where(w => w.Brand == "Ferrari")
}
This approach takes away the proxy object type for this particular instance of the context so the returned objects are the actual class and therefore serialization is not a problem.
ie:
{Models.car}
instead of
{System.Data.Entity.DynamicProxies.car_231710A36F27E54BC6CE99BB50E0FE3B6BD4462EC‌​A19695CD1BABB79605296EB}

The problem is that your are actually serializing an entity framework generated proxy object. Unfortunatly this has some issues when used with the JSON serializer. You might consider to map your entities to special simple POCO classes for the sake of JSON compatibility.

There is an attribute to add to Entity Framework objects
[ScriptIgnore]
This makes the code not perform Circular references.

I think they have fixed this in the latest version.
Check out the help docs under the section "Serializing and Deserializing JSON -> Serialization and Preserving Object References".
Set this setting when initializing the JSON.Net Serializer:
PreserveReferencesHandling = PreserveReferencesHandling.Objects;
So an example would be this:
var serializerSettings = new JsonSerializerSettings { PreserveReferencesHandling = PreserveReferencesHandling.Objects };
string json = JsonConvert.SerializeObject(people, Formatting.Indented, serializerSettings);
I verified that this works with my code first solution, and a circular reference in the navigation properties. If you look at the resulting JSON it should have "$id" and "$ref" properties everywhere.

An alternative solution would be to use anonymous types as the result of a LINQ query.
In my project, I am using lazy loading extensively, and disabling it was not the right thing to do.

An alternative solution, if only some values from objects are necessary, is build an anonymous class and return it, like the example below:
public JsonResult AjaxFindByName(string term)
{
var customers = context.Customers
.Where(c => c.Name.ToUpper().Contains(term.ToUpper())).Take(10)
.AsEnumerable()
.Select(c => new {
value = c.Name,
SSN = String.Format(#"{0:000\-00\-0000}", c.SSN),
CustomerID = c.CustomerID });
return Json(customers, JsonRequestBehavior.AllowGet);
}

The circular reference happens because you use eager loading on the object.
You have a couple of methods:
Turn off eager loading when your loading your Query (linq or lambda)
DbContext.Configuration.ProxyCreationEnabled = false;
Remove the virtual keyword from the Domainmodel
Detach the objects (= no eager loading functionality & no proxy)
Repository.Detach(entityObject)
DbContext.Entry(entityObject).EntityState = EntityState.Detached
Clone the properties
You could use something like AutoMapper to clone the object, don't use the ICloneable interface, because it also clones the ProxyProperties in the object, so that won't work.
In case you are building an API, try using a separte project with a different configuration (that doesn't return proxies)
PS. Proxies is the object that's created by EF when you load it from the Entity Framework. In short: It means that it holds the original values and updated values so they can be updated later. It handles other things to ;-)

For those using the proxy EF/Linq2SQL classes my solution was to simply remove the parent reference on my child entities.
So in my model, I selected the relationship and changed the Parent reference to be Internal rather than Public.
May not be an ideal solution for all, but worked for me.

You can remove the virtual keyword:
public virtual Patient Patient { get; set; } -> public Patient Patient { get; set; }
Keep in mind that when you remove the virtual keyword, lazy loading will be turned off.

I was able to solve this problem by using the method described here:
http://mytechworld.officeacuity.com/index.php/2010/02/serializing-entity-framework-objects-into-json-using-asp-net-mvc/

Related

C# EF6 mapping not applied on select

I am facing an issue with the Fluent API mapping in EF6. It is all set up, but for some reason, anytime I select an object, it is missing the child objects.
Lets start with the DbContext:
public partial class PMSContext : DbContext
{
public PMSContext() : base(nameOrConnectionString: "PmsDb")
{
this.Configuration.ProxyCreationEnabled = false;
this.Configuration.LazyLoadingEnabled = false;
}
public DbSet<Employee> employees { get; set; }
public DbSet<Project> projects { get; set; }
public DbSet<ProjectStep> projectSteps { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
modelBuilder.Entity<Employee>().HasMany(e => e.Projects).WithMany(t => t.EmployeesWorkingOnProject).Map(m =>
{
m.MapLeftKey("EmployeeId");
m.MapRightKey("ProjectId");
m.ToTable("employee_project");
});
modelBuilder.Entity<ProjectStep>().HasRequired(p => p.Project).WithMany(s => s.ProjectSteps).Map(m => m.MapKey("Project")).WillCascadeOnDelete(true);
//modelBuilder.Entity<Project>().HasMany(p => p.ProjectSteps).WithRequired(ps => ps.Project).WillCascadeOnDelete(true);
modelBuilder.Entity<Employee>().HasOptional<Project>(e => e.LeaderOfProject).WithOptionalPrincipal(p => p.ProjectLeader).Map(m => m.MapKey("ProjectLeader"));
}
public Project FindProjectById(int id)
{
return this.projects.Find(id);
}
}
This is pretty much everything needed which plays into my issue.
I have set up a total of 3 model classes:
[DataContract(Namespace = "Shared")]
public class Employee
{
public Employee()
{
this.Projects = new List<Project>();
}
[DataMember]
public int ID { get; set; }
[DataMember]
public String Name { get; set; }
[DataMember]
public String JobDescription { get; set; }
[DataMember]
public String Department { get; set; }
[DataMember]
public String DirectDialing { get; set; }
[DataMember]
public bool Status { get; set; }
public virtual Project LeaderOfProject { get; set; }
[DataMember]
public virtual List<Project> Projects { get; set; }
}
[DataContract(Namespace = "Shared")]
public class Project
{
public Project()
{
this.EmployeesWorkingOnProject = new List<Employee>();
this.ProjectSteps = new List<ProjectStep>();
}
[DataMember]
public int ID { get; set; }
[DataMember]
public String Titel { get; set; }
[DataMember]
public DateTime StartDate { get; set; }
[DataMember]
public DateTime EndDate { get; set; }
[DataMember]
public String Description { get; set; }
[DataMember]
public Employee ProjectLeader { get; set; }
[DataMember]
public bool Status { get; set; }
[DataMember]
public virtual List<Employee> EmployeesWorkingOnProject { get; set; }
[DataMember]
public virtual List<ProjectStep> ProjectSteps { get; set; }
}
[DataContract(Namespace = "Shared")]
[Table("project_step")]
public class ProjectStep
{
[DataMember]
public int ID { get; set; }
[DataMember]
public String Description { get; set; }
[DataMember]
public DateTime StartDate { get; set; }
[DataMember]
public DateTime EndDate { get; set; }
[DataMember]
public Project Project { get; set; }
}
And the corresponding database setup:
Now to my problem. Whenever I execute the FindProjectById method it does return the proper object, but it is missing any reference to the childs. this means that
ProjectSteps
EmployeesWorkingOnProject
ProjectLeader
are not set. This also causes issues on my delete methods. I assume that this is an error in my OnModelCreating method, but I am not 100% sure.
Can anyone tell me what I am missing to fetch the child objects as well?
You have disabled lazy loading and proxies, but if you do not .Include() child entities then EF doesn't know to load them. To use .Include() you will need to use .SingleOrDefault() rather than Find. Otherwise you will need to go to the context to Load child collections/references.
Your FindProjectById() would look something like:
var project = this.Projects.Include(x=>x.ProjectLeader)
.Include(x=>x.EmployeesWorkingOnProject)
.Include(x=>x.ProjectSteps)
.SingleOrDefault(x=>x.ID == id);
return project;
A caveat around using SingleOrDefault vs. Find is that where Find will search local store then go to DB, Single/First/ etc. will go to DB. This means that a query is executed each time where Find may find an entity in the local memory store. if you're inserting records into the DB Context (and prior to save changes) searching for that entity will not return an entity that is in the local store, but hasn't been committed to the DB yet. (prior to SaveChanges())
Typically you do not want to return entities outside of the scope of the DbContext as lazy load proxies won't work so anything you don't pre-load will be #null. I generally rely on deferred execution, returning IQueryable then .Select() the various bits I care about into DTO/ViewModel POCO classes.
You can try change the lazy loading into true if you want to automatically load the child objects.
this.Configuration.LazyLoadingEnabled = true;
if the lazy loading is false, then you need to load the reference before accessing it.

EF Core 2.0 - Navigation properties can only participate in a single relationship

Let's say I have these models:
public class Component
{
public int Id { get; set; }
public string Name { get; set; }
public string Type { get; set; }
public List<ComponentUpdate> Updates { get; set; }
public ComponentUpdate LastUpdate { get; set; }
}
public class ComponentUpdate
{
public int Id { get; set; }
public DateTime Timestamp { get; set; }
public Component Component { get; set; }
public string Message { get; set; }
}
The reason I'm saving the LastUpdate field instead of manually pulling it according to the highest 'TimeStamp' is because of speed. It would be faster to store a reference instead of checking the entire list every request.
When I'm trying to migrate the DB it throws an error saying I cannot have my properties participate in more than a single relationship.
I'm mapping the relationships in my context class and I don't think I'm doing it right since I have ComponentUpdate.Component mapped twice.
I've looked on several solutions but some were outdated and some just did not fit this scenario.
Thanks for helping.
Edit
Mapping accordingly:
modelBuilder.Entity<Component>().HasMany(c => c.Updates).WithOne(u => u.Component);
modelBuilder.Entity<ComponentUpdate>().HasOne(u => u.Component).WithOne(c => c.LastUpdate);

Missing type map configuration or unsupported mapping error in Automapper

I am using an Automapper to map one model to second model which has some rows with the same name as the first model. I am getting this inner exception
Missing type map configuration or unsupported mapping.
Mapping types:
Bed -> Bed
Model1.Bed -> Model2.Bed
This is the condensed version of the code.
Model 1
public class Model1
{
public Guid Id { get; set; }
public string Name { get; set; }
public IEnumerable<Bed> Beds { get; set; }
public IEnumerable<Bed> Beds1 { get; set; }
public string Status {get; set;}
public string Notes {get; set;}
}
public class Model2
{
public Guid Id { get; set; }
public string Name { get; set; }
public IEnumerable<Bed> Beds { get; set; }
public IEnumerable<Bed> Beds1 { get; set; }
}
public class Bed
{
public Guid Id { get; set; }
public Guid TypeId { get; set; }
public string Type { get; set; }
public string Notes { get; set; }
public DateTime? CreatedOn { get; set; }
public DateTime? LastUpdatedOn { get; set; }
}
Mapping :
MapperConfiguration config = null;
config = new MapperConfiguration(cfg => { cfg.CreateMap<Model1, Model2>(); });
IMapper mapper = config.CreateMapper();
var dest = mapper.Map<Model1, Model2>(update);
update is an instance of Model1. Am I missing something? Should I do something with regards to the Bed class mapping even though both reference the same method?
Also is there a way to copy data from one model to another in this scenario other than manually doing it or using Automapper?
EDIT 1:
I am sure the error is from that mapping. I removed it and all the other fields were fine and there was no exception. But I am not sure how I should do the Bed -> Bed binding.
EDIT 2:This link seems to have the same issue and they say it's a possible bug and This one seems to have the answer although I am not sure how I can adapt it to my situation.
It seems like auto mapper is having trouble determining how to map Bed. I would try the following:
config = new MapperConfiguration(cfg => {
cfg.CreateMap<Model1, Model2>();
cfg.CreateMap<Bed, Bed>();
});
Although it seems odd that would be necessary...

C# Linq can't format output list in the order I need for JSON result

I encountered problem when formating my object to serialise for Json.
my model:
[Table("Stores")]
public class DeliveryPoint
{
[Key]
public Guid StoreId { get; set; }
[Required]
[StringLength(20)]
[Display(Name = "Store name")]
public string StoreName { get; set; }
public ICollection<Location> Location { get; set; }
public virtual Company Company { get; set; }
public List<DeliveryCost> DeliveryPrice{ get; set; }
}
[Table("Drop_Prices")]
public class DeliveryCost
{
[Key]
public Int16 id { get; set; }
public virtual DeliveryPoint Store { get; set; }
public virtual Location DC { get; set; }
public double Cost { get; set; }
}
my controller
public JsonResult GetAllStores()
{
var stores = _db.Stores.Select(d => new MyClass
{
name = d.StoreName,
cost = d.DeliveryPrice.ToDictionary( item => item.DC.LocationID,item=> item.Cost)
}).ToList();
return Json(stores, JsonRequestBehavior.AllowGet);
}
public class MyClass
{
public string name { get; set; }
public Dictionary<Guid,double> cost { get; set; }
}
I desired Output would be
[{"name":"foo","cost":{"82ec8689-d9e3-49e6-ba12-af091ac42760":"400"}}]
So the proble is I get error sometimes is circular error or LINQ
LINQ to Entities does not recognize the method 'System.Collections.Generic.Dictionary`2[System.Guid,System.Double] ToDictionary[DeliveryCost,Guid,Double]
The problem is that LINQ to Entities can't convert your expression to SQL. Not every C# expression tree can be converted to SQL, only a limited set of operations are supported.
You can always get around this kind of issue by materializing the entities in memory and doing the transform in memory. So query the database for the entities you need, and pull them into memory with a .ToList() and then do the work on them to shape them into the desired JSON.
Whether this is efficient depends very much on the specifics of the situation. It may be less efficient, or it could in fact be more efficient than doing the equivalent work in SQL. All you can do is try it out and see.

Entity Framework: Get children

I have the following Entity classes:
public class History : BaseEntity
{
public string Version { get; set; }
public DateTime? ReleaseDate { get; set; }
public string Name { get; set; }
public string Additional { get; set; }
public List<HistoryEntry> Entries { get; set; }
}
public class HistoryEntry : BaseEntity
{
public string Version { get; set; }
public string BugNr { get; set; }
public AllowedTypes EntryType { get; set; }
public string Description { get; set; }
public History Parent { get; set; }
}
public enum AllowedTypes
{
Bug, Enhancement
}
which are mapped that way:
public HistoryMap()
{
ToTable("History");
HasKey(c => c.Version);
Property(c => c.Version).HasMaxLength(10);
Property(c => c.Name).HasMaxLength(200);
}
this results in two tables that act exactly like I wanted ("History" has "Version" as primary key and "HistoryEntry" has a foreign Key "Version" that is linked to "History"."Version"
After Adding some stuff into these tables I try to read the contents:
IQueryable<History> query = _historyRepository.Table.OrderByDescending(c => c.ReleaseDate);
var historyList = new List<Core.Domain.Tengo.History>(query);
While all History-Entries are read successfully, the "Entries"-Property if the "History"-Object is always NULL.
How do I achieve that the linked Items are also read and stored into Entries?
Navigation properties such as ICollection<T> / IList<T> should be marked as virtual. This allows EntityFramework to override them and provide code for lazy-loading the properties.
So the line (in the History class)
public List<HistoryEntry> Entries { get; set; }
Should become
public virtual List<HistoryEntry> Entries { get; set; }
Yves answer is correct if you want to use Lazy Loading.
The other way to do this is to use Eager Loading. Eager loading loads the properties immediately rather than when it is accessed. The snippet below uses Eager Loading.
_historyRepository.Table.Include(x=>x.Entries).OrderByDescending(c => c.ReleaseDate);
The difference between Lazy and eager loading are explained in the link below:
http://www.dotnet-tricks.com/Tutorial/entityframework/RIWW210913-Difference-between-Lazy-Loading-and-Eager-Loading.html

Categories

Resources