none
In-memory only and population of many side? RRS feed

  • Question

  • With code first I can have a one to many relationship like so, and the ClassAs collection is automatically updated as the ReferencedClassB is assigned.

    ClassA { public ReferencedClassB }

    ClassB { public ICollection<ClassA> ReferencedClassAs }

    Without this nice feature of EF, you'd have to do this to keep the collection in sync:

    someClassA.ReferencedClassB = someClassB;
    someClassB.RferencedClassAs.Add(someClassA);

    In essence, the second line is not needed since EF will update the FK and both sides of the relationship are populated based on that information.

    Can I get this nice functionality(syncing the collection side of each relationship) without persisting to a database?  I am going the code first approach but I haven't actually set things up to save or retrieve from a database.  I essentially am programmatically creating the data, similar to what you would do in a seed function.  After seeding the data structure, I need the collections on the many side of each relationship to be sync'd up, so that the rest of the program can traverse the data structure from either side of each relationship.

    Thursday, January 19, 2012 10:11 PM

Answers

  • Hi AaronLST,

    Welcome to MSDN Forum.

    The sync feature of Entity Framework is based on association built. When you call SaveChanges method, Entity Framework can detect the relationships between the two entities and do some special to sync them. If the association is not built at all, I don't think this feature can work. But there's something can achieve the aim. You can write a method to imitate the sync feature, but it may difficulty. For example, a method need two generic parameters, when you assign a entity to another entity, call the method by the two entities type, and then use reflection to detect the two entities relationship, after confirm the relationship between the two entities, you can realize the sync feature in this method. I hope the thinking can help you.

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us
    Tuesday, January 24, 2012 6:26 AM
    Moderator

All replies

  • What's the meaning, do you mean the database has not established, you want to use the sync feature in memory?
    Monday, January 23, 2012 9:04 AM
  • When assignments are made in code to the ReferencedClassB property, I want the ReferencedClassAs collection on the other side of the relationship to be sync'd up as well automatically.  Just as if you had performed as save to the database, but in this case there is no database.

    Monday, January 23, 2012 7:10 PM
  • Hi AaronLST,

    Welcome to MSDN Forum.

    The sync feature of Entity Framework is based on association built. When you call SaveChanges method, Entity Framework can detect the relationships between the two entities and do some special to sync them. If the association is not built at all, I don't think this feature can work. But there's something can achieve the aim. You can write a method to imitate the sync feature, but it may difficulty. For example, a method need two generic parameters, when you assign a entity to another entity, call the method by the two entities type, and then use reflection to detect the two entities relationship, after confirm the relationship between the two entities, you can realize the sync feature in this method. I hope the thinking can help you.

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us
    Tuesday, January 24, 2012 6:26 AM
    Moderator