none
inserting object with fk references - NotSupportedException RRS feed

  • Question

  • I have three entities/classes:

    1) Search
    2) SearchCriterion
    3) SearchTerm

    Search contains a list / association of SearchCriterion, and SearchCriterion has a reference/fk to SearchTerm.  (Think: Customer has Orders, and each Order has an OrderType)

    I am creating a new Search + List of SearchCriterion. SearchTerm already exists.

    I can save to the database successfully when I do

    SearchCriterion.Search = myNewSearchObject.
    // SearchCriterion.SearchTerm = myExistingSearchTermObject; // this fails!
    SearchCriterion.SearchTermId = myExistingSearchTermObject.SearchTermId; // this works

     When I say "fail", this is the error message I get:

    Failure: System.NotSupportedException : An attempt has been made to Attach or Add an entity that is not new, perhaps having been loaded from another DataContext.  This is not supported.

    Thoughts? myExistingSearchTermObject is retrieved from the SAME data context.

    Here's what the decorations on my classes look like:

    [Table(Name = "Search")]
    public class Search 
    {
      // ...
      [Association(OtherKey = "SearchId")] readonly EntitySet<SearchCriterion> _searchCriteria = new EntitySet<SearchCriterion>(); 
      public IList<SearchCriterion> SearchCriteria 
      { 
        get { return _searchCriteria.ToList().AsReadOnly(); } 
      }
    
    }
    
    [Table(Name = "SearchCriterion")]
    public class SearchCriterion   
    {
      // ...
      [Column(CanBeNull = false)] internal int SearchId { get; set; }
      internal EntityRef<Search> _search = new EntityRef<Search>(); 
    
      [Association(ThisKey = "SearchId", Storage = "_search", OtherKey = "SearchId", IsForeignKey = true)]
      public Search Search {
        get { return _search.Entity; }
        internal set { 
          _search.Entity = value; 
          SearchId = (value == null ? 0 : value.SearchId);
      }
    }
    
        [Column(CanBeNull = false)]
        internal int SearchTermId { get; set; }
    
    		internal EntityRef<SearchTerm> _searchTerm = new EntityRef<SearchTerm>();
        [Association(ThisKey = "SearchTermId", Storage = "_searchTerm", OtherKey = "SearchTermId", IsForeignKey = true)]
    		public SearchTerm SearchTerm {
          get { return _searchTerm.Entity; }
    			internal set {
            _searchTerm.Entity = value;
    				SearchTermId = (value == null ? 0 : value.SearchTermId);
    			}
        }
    }
    

     

    Thoughts?

    Friday, June 11, 2010 8:53 PM

Answers

  • Never mind... I found the problem. As the error message state, the problem was that my SearchTerm object was in fact from a different DataContext. I am using UnityContainer for dependency injection and the Repository pattern... so it turns out that a bug in my code meant I was I was actually creating a new Repository object, as opposed to the same Repository object where the DataContext is located.
    Friday, June 11, 2010 9:36 PM