Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8278405
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T08:59:46+00:00 2026-06-08T08:59:46+00:00

I have been using Entity Framework in a .NET 4.0 solution for a few

  • 0

I have been using Entity Framework in a .NET 4.0 solution for a few weeks. It is EF 4.3.1. I have created the database schema first, and generated my Entity Objects using “EF4.x DbContext Generator” template.

I had three tables in the schema and it was all working fine with simple CRUD methods.

I have now added a fourth table, “Subjects”, which has a foreign Key reference to an existing table “SourceUri”, such that a SourceUri can have 0-many subjects, and a Subject has exactly one SourceUri.

I have updated my edmx model and it looks correct. However, no matter what I try, I cannot seem to do the following:

  • Add a new SourceUri record
  • Add one or more Subjects for the new SourceUri

This is the code I am currently trying. You can see I am saving the context regularly, but originally I was only saving the changes once at the end of the method.

    /// <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;
        }
    }

The code adds the new SourceUri and saves the changes. However, it does not manage to add the new Subject to the data context. It does not get as far as trying to save that change.

The exception is:

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)

I have gone round and round on this, and have seen several slightly different exceptions. They all seem to point to the fact that the Subjects navigation property of the SourceUri object is in some way read-only or fixed length (array?).

The generated entity classes look as follows:

//------------------------------------------------------------------------------
// <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; }
    }
}

Why doesn’t this work?

Quick list of things I’ve checked/tried:

  • The database schema looks correct – I can insert records as expected with SQL and primary keys, foreign keys and identities seem to be behaving correctly
  • The model seems to reflect the database schema correctly, including PK identities (EntityKey = true, StoreGeneratedPattern=Identity)
  • I have managed to get EF to persist data into my schema, but after the data is inserted, an exception is thrown stating that the context might be out of sync – again pertaining to not being able to update the Subjects navigation property of the SourceUri object
  • I have tried adding the Subjects to the dbContext.Subjects collection, and also to the SourceUri.Subjects collection. I have also tried setting the Subject’s SourceUri property instead of the Subject’s SourceUriId property.
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T08:59:49+00:00Added an answer on June 8, 2026 at 8:59 am

    I have got to the bottom of this issue. The problem behaviour was caused by the SourceUri entity being passed into the method via a WCF web service. It meant that the ICollection properties of the object were being deserialized as an Array, which is of fixed-length, and cannot be added to.

    I have resolved this issue by altering the template that generates my entities so that it produces classes where the collections are explicitly Lists, as follows:

    //------------------------------------------------------------------------------
    // <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
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been experimenting recently with Silverlight, RIA Services, and Entity Framework using .NET
I have been using the Entity Framework with the POCO First approach. I have
I have an ASP.NET MVC 3 application using an Entity Framework (4.3.1) Code First
Can anyone help? I have been using the entity framework and its going well
I'm using Entity Framework to access my MySQL database. The model was generated using
I'm writing an ASP.net MVC3 application using Entity Framework Code First and SqlCe4. I
I have an assembly generated using POCO template using Entity Framework (e.g. Company.Models.dll).Besides generated
I have a ASP.NET MVC 3 application, using Entity Framework 4 to handle Data
I'm using Entity Framework 4 Code First in my .net MVC 2.0 project and
I have an ASP.NET 4 web app that is using the Entity Framework to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.