Entity Framework Circular Reference

后端 未结 1 1632
粉色の甜心
粉色の甜心 2021-01-11 12:24

Trying this question again because my first attempt was barely coherent :p

So I am super confused and using Entity Framework Code First

I have a Forest class

相关标签:
1条回答
  • 2021-01-11 13:24

    Best approach would be you should use DTOs to transfer only the data that you want to the client. The DTOs should have just simple properties so it won't create a circular reference error. At the moment the Forest has List<Trees> Trees and each Tree within Trees has Forest and that Forest again has List<Trees>

    Or

    You can decorate your attributes with ScriptIgnore for properties that you don't want the Json.Encode to serialize and then that wouldn't be sent back to the client.

    http://msdn.microsoft.com/en-us/library/system.web.script.serialization.scriptignoreattribute.aspx

    Eg:

    public class Forest
    {    
        public Guid ID { get; set; }  
        public virtual List<Tree> Trees { get; set; }
    }
    public class Tree
    {
        public Guid ID { get; set; }
        public Guid? ForestId {get;set;}
    
        [ForeignKey("ForestId")]
        [ScriptIgnore]
        public virtual Forest Forest {get;set;}
     }
    

    Edit:

    Along with ScriptIgnore you should also remove virtual from Forest and Trees and that would work. I've tested it. However, I wouldn't advise that because virtual keyword is what does the Lazy loading. Hence as I said you need to create DTOs based on these Models and only send in DTO to client.

    0 讨论(0)
提交回复
热议问题