2012-07-16 9 views
7

के साथ इकाई प्रकार पर फ़ील्ड/प्रॉपर्टी सेट करने में असमर्थ मैं कुछ हफ्तों के लिए .NET 4.0 समाधान में एंटीटी फ्रेमवर्क का उपयोग कर रहा हूं। यह ईएफ 4.3.1 है। मैंने पहले डेटाबेस स्कीमा बनाया है, और "EF4.x DbContext जनरेटर" टेम्पलेट का उपयोग करके मेरी इकाई ऑब्जेक्ट जेनरेट की है।एंटीटी फ्रेमवर्क 4.3.1

मेरे पास स्कीमा में तीन टेबल थे और यह सब सरल सीआरयूडी विधियों के साथ ठीक काम कर रहा था।

मैंने अब एक चौथी तालिका, "विषय" जोड़ा है, जिसमें मौजूदा तालिका "स्रोतयूरी" का एक विदेशी कुंजी संदर्भ है, जैसे कि स्रोतयूरी में 0-कई विषय हो सकते हैं, और विषय में एक स्रोतउरी है।

मैंने अपना एडीएमएक्स मॉडल अपडेट किया है और यह सही दिखता है। हालांकि, कोई फर्क नहीं पड़ता कि मैं क्या करने की कोशिश, मैं निम्न कार्य नहीं कर पा रहे:

  • एक नया SourceUri रिकॉर्ड
  • नई SourceUri के लिए एक या अधिक विषयों जोड़ें

इस कोड को मैं है वर्तमान में कोशिश कर रहा हूँ। आप देख सकते हैं कि मैं नियमित रूप से संदर्भ को सहेज रहा हूं, लेकिन मूल रूप से मैं केवल विधि के अंत में परिवर्तनों को सहेज रहा था।

/// <summary> 
    /// Adds a new Source URI to the system 
    /// </summary> 
    /// <param name="sourceUri">The source URI to add</param> 
    /// <param name="subjectNames">List of subjects for this source URI, in order</param> 
    /// <returns>The added source URI</returns> 
    public SourceUri AddSourceUri(SourceUri sourceUri, IList<string> subjectNames) 
    { 
     try 
     { 
      _logger.Debug("Adding new source URI '{0}', with '{1}' subjects.", sourceUri.Uri, 
       subjectNames != null ? subjectNames.Count : 0); 
      LogSourceUriDetails(sourceUri, "Adding"); 

      using (var dbContext = GetDbContext()) 
      { 
       dbContext.SourceUris.Add(sourceUri); 
       dbContext.SaveChanges(); // this save succeeds 

       // add the subjects if there are any 
       if (subjectNames != null) 
       { 
        for (int i = 0; i < subjectNames.Count; i++) 
        { 
         Subject newSubject = new Subject() 
               { 
                DisplayOrder = i, 
                SourceUriId = sourceUri.SourceUriId, 
                SubjectText = subjectNames.ElementAt(i).Trim() 
               }; 
         _logger.Debug("Adding new subject '{0}' to source URI '{1}'.", newSubject.SubjectText, 
             sourceUri.Uri); 
         dbContext.Subjects.Add(newSubject); // this line fails 
         dbContext.SaveChanges(); 
        } 
       } 

       _logger.Debug("Successfully added new source URI '{0}' with '{1}' subjects. Source URI ID is '{2}'.", 
        sourceUri.Uri, subjectNames != null ? subjectNames.Count : 0, sourceUri.SourceUriId); 
       return sourceUri; 
      } 
     } 
     catch (Exception exception) 
     { 
      _logger.ErrorException(string.Format("An error occurred adding new source URI '{0}' with '{1}' subjects.", 
       sourceUri.Uri, subjectNames != null ? subjectNames.Count : 0), exception); 
      throw; 
     } 
    } 

कोड नया SourceUri जोड़ता है और परिवर्तनों को बचाता है। हालांकि, यह डेटा संदर्भ में नया विषय जोड़ने का प्रबंधन नहीं करता है। यह उस परिवर्तन को बचाने की कोशिश करने तक नहीं मिलता है।

अपवाद है:

Unable to set field/property Subjects on entity type CommentService.DomainObjects.SourceUri. See InnerException for details. 
System.Data.Objects.Internal.PocoPropertyAccessorStrategy.CollectionRemove(RelatedEnd relatedEnd, Object value) 
System.Data.Objects.Internal.EntityWrapper`1.CollectionRemove(RelatedEnd relatedEnd, Object value) 
System.Data.Objects.DataClasses.EntityCollection`1.RemoveFromObjectCache(IEntityWrapper wrappedEntity) 
System.Data.Objects.DataClasses.RelatedEnd.Remove(IEntityWrapper wrappedEntity, Boolean doFixup, Boolean deleteEntity, Boolean deleteOwner, Boolean applyReferentialConstraints, Boolean preserveForeignKey) 
System.Data.Objects.DataClasses.RelatedEnd.FixupOtherEndOfRelationshipForRemove(IEntityWrapper wrappedEntity, Boolean preserveForeignKey) 
System.Data.Objects.DataClasses.RelatedEnd.Remove(IEntityWrapper wrappedEntity, Boolean doFixup, Boolean deleteEntity, Boolean deleteOwner, Boolean applyReferentialConstraints, Boolean preserveForeignKey) 
System.Data.Objects.DataClasses.EntityReference`1.Exclude() 
System.Data.Objects.DataClasses.RelationshipManager.RemoveRelatedEntitiesFromObjectStateManager(IEntityWrapper wrappedEntity) 
System.Data.Objects.ObjectContext.AddObject(String entitySetName, Object entity) 
System.Data.Entity.Internal.Linq.InternalSet`1.&lt;&gt;c__DisplayClass5.&lt;Add&gt;b__4() 
System.Data.Entity.Internal.Linq.InternalSet`1.ActOnSet(Action action, EntityState newState, Object entity, String methodName) 
System.Data.Entity.Internal.Linq.InternalSet`1.Add(Object entity) 
System.Data.Entity.DbSet`1.Add(TEntity entity) 
CommentService.Business.Managers.SourceUriManager.AddSourceUri(SourceUri sourceUri, IList`1 subjectNames) in C:\Projects\SVN Misc Projects\CommentService\trunk\CommentService.Business\Managers\SourceUriManager.cs:line 152 
CommentService.Web.Comment.AddSourceUri(SourceUri sourceUri, IList`1 subjectNames) in C:\Projects\SVN Misc Projects\CommentService\trunk\CommentService.Web\Comment.svc.cs:line 173 
SyncInvokeAddSourceUri(Object , Object[] , Object[]) 
System.ServiceModel.Dispatcher.SyncMethodInvoker.Invoke(Object instance, Object[] inputs, Object[]&amp; outputs) 
System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc&amp; rpc) 
System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc&amp; rpc) 
System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage41(MessageRpc&amp; rpc) 
System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage4(MessageRpc&amp; rpc) 
System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage31(MessageRpc&amp; rpc) 
System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage3(MessageRpc&amp; rpc) 
System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage2(MessageRpc&amp; rpc) 
System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage11(MessageRpc&amp; rpc) 
System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage1(MessageRpc&amp; rpc) 
System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet) 

मैं दौर है और इस पर दौर चले गए हैं, और कई थोड़ा अलग अपवाद देखा है। वे सभी इस तथ्य को इंगित करते हैं कि SourceUri ऑब्जेक्ट की विषय नेविगेशन प्रॉपर्टी किसी भी तरह से केवल पढ़ने या निश्चित लंबाई (सरणी?) है।

//------------------------------------------------------------------------------ 
// <auto-generated> 
// This code was generated from a template. 
// 
// Manual changes to this file may cause unexpected behavior in your application. 
// Manual changes to this file will be overwritten if the code is regenerated. 
// </auto-generated> 
//------------------------------------------------------------------------------ 

namespace CommentService.DomainObjects 
{ 
    using System; 
    using System.Collections.Generic; 

    public partial class SourceUri 
    { 
     public SourceUri() 
     { 
      this.Comments = new HashSet<Comment>(); 
      this.Subjects = new HashSet<Subject>(); 
     } 

     public long SourceUriId { get; set; } 
     public string Uri { get; set; } 
     public string Description { get; set; } 
     public System.DateTime DateCreated { get; set; } 
     public string AdminUser { get; set; } 

     public virtual ICollection<Comment> Comments { get; set; } 
     public virtual ICollection<Subject> Subjects { get; set; } 
    } 
} 


//------------------------------------------------------------------------------ 
// <auto-generated> 
// This code was generated from a template. 
// 
// Manual changes to this file may cause unexpected behavior in your application. 
// Manual changes to this file will be overwritten if the code is regenerated. 
// </auto-generated> 
//------------------------------------------------------------------------------ 

namespace CommentService.DomainObjects 
{ 
    using System; 
    using System.Collections.Generic; 

    public partial class Subject 
    { 
     public Subject() 
     { 
      this.Comments = new HashSet<Comment>(); 
     } 

     public long SubjectId { get; set; } 
     public long SourceUriId { get; set; } 
     public string SubjectText { get; set; } 
     public int DisplayOrder { get; set; } 

     public virtual ICollection<Comment> Comments { get; set; } 
     public virtual SourceUri SourceUri { get; set; } 
    } 
} 

यह काम क्यों नहीं करता है:

उत्पन्न इकाई वर्गों ऐसा नज़र?

चीजें मैं देख लिया है के त्वरित सूची/करने की कोशिश की:

  • डेटाबेस स्कीमा सही लगती है - एसक्यूएल और प्राथमिक कुंजी, विदेशी कुंजी और पहचान के साथ की उम्मीद के रूप में मैं रिकॉर्ड सम्मिलित कर सकते हैं सही ढंग से
  • व्यवहार कर जा करने के लिए लग
  • यह मॉडल डेटाबेस स्कीमा को सही ढंग से प्रतिबिंबित करता है, जिसमें पीके पहचान (EntityKey = true, StoreGeneratedPattern = Identity)
  • मैं अपने स्कीमा में डेटा को जारी रखने के लिए ईएफ प्राप्त करने में कामयाब रहा हूं, लेकिन डेटा डालने के बाद, एक अपवाद फेंक दिया गया है यह बताते हुए कि संदर्भ सिंक से बाहर हो सकता है - फिर से सक्षम नहीं होने से संबंधित SourceUri ऑब्जेक्ट
  • की विषय नेविगेशन प्रॉपर्टी को अपडेट करने के लिए मैंने dbContext.Subjects संग्रह, और SourceUri.Subjects संग्रह में विषय जोड़ने का प्रयास किया है। मैंने विषय की SourceUriId संपत्ति के बजाय विषय की स्रोत यूआरआई संपत्ति को सेट करने का भी प्रयास किया है।
+0

"* जानकारी के लिए InnerException देखें।" * कहते हैं पहले स्टैक ट्रेस में लाइन। क्या आप ऐसा कर सकते हैं और अपने प्रश्न में आंतरिक अपवाद संदेश जोड़ सकते हैं? – Slauma

+0

आंतरिक अपवाद शून्य है। –

उत्तर

14

मुझे इस मुद्दे के निचले हिस्से में जाना है। समस्या व्यवहार डब्लूसीएफ वेब सेवा के माध्यम से SourceUri इकाई को विधि में पारित करने के कारण हुआ था।इसका मतलब था कि वस्तु के आईसीओलेक्शन गुणों को एक ऐरे के रूप में deserialized किया जा रहा था, जो निश्चित लंबाई की है, और इसमें जोड़ा नहीं जा सकता है।

मैं इस प्रकार है, कि मेरे संस्थाओं उत्पन्न करता है इतना है कि यह कक्षाएं जहां संग्रह स्पष्ट सूचियाँ हैं पैदा करता है टेम्पलेट बदलकर इस समस्या का समाधान कर दिया है:

//------------------------------------------------------------------------------ 
// <auto-generated> 
// This code was generated from a template. 
// 
// Manual changes to this file may cause unexpected behavior in your application. 
// Manual changes to this file will be overwritten if the code is regenerated. 
// </auto-generated> 
//------------------------------------------------------------------------------ 

namespace CommentService.DomainObjects 
{ 
    using System; 
    using System.Collections.Generic; 
    using System.Linq; // added this 

    public partial class SourceUri 
    { 
     public SourceUri() 
     { 
      this.Comments = new HashSet<Comment>().ToList(); // added this 
      this.Subjects = new HashSet<Subject>().ToList(); // added this 
     } 

     public long SourceUriId { get; set; } 
     public string Uri { get; set; } 
     public string Description { get; set; } 
     public System.DateTime DateCreated { get; set; } 
     public string AdminUser { get; set; } 

     public virtual List<Comment> Comments { get; set; } // altered these to List 
     public virtual List<Subject> Subjects { get; set; } // altered these to List 
    } 
} 

 संबंधित मुद्दे

  • कोई संबंधित समस्या नहीं^_^