Print Page | Close Window

EntityManagerSaveException with a CF model where a one to many navigation property end is mising

Printed From: IdeaBlade
Category: DevForce
Forum Name: DevForce 2010
Forum Discription: For .NET 4.0
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=3365
Printed Date: 13-May-2026 at 8:27am


Topic: EntityManagerSaveException with a CF model where a one to many navigation property end is mising
Posted By: Walid
Subject: EntityManagerSaveException with a CF model where a one to many navigation property end is mising
Date Posted: 30-Mar-2012 at 5:11am
Hi,

I have 2 basics classes, with a one to many relationship.
in my model I don't need/want to have the EntityRelatedList on the primary end of the relationship. I have my own dbcontext (pretty small in this case).


My modele looks like this :

    public class User : BaseEntity
    {
        [Key]
        public int Id { get; set; }
        public string Name { get; set; }
    }

    public class Document : BaseEntity
    {
        [Key]
        public int Id { get; set; }
        [Required]
        public string Title { get; set; }
        public User User { get; set; }
    }



When I want to save a new Document I get an EntityManagerSaveException with the message "La séquence ne contient aucun élément correspondant" which I beleive can be translated to "Sequence contains no matching element". Below is the StackTrace:

   à System.Linq.Enumerable.First[TSource](IEnumerable`1 source, Func`2 predicate)
   à IdeaBlade.EntityModel.Edm.EdmSaveExecutor.GetRelatedEnd(IEntity entity, String propertyName)
   à IdeaBlade.EntityModel.Edm.EdmSaveExecutor.ProcessManyToMany(List`1 entities)
   à IdeaBlade.EntityModel.Edm.EdmSaveExecutor.ProcessSaves(IEnumerable`1 groupsByType)
   à IdeaBlade.EntityModel.Edm.EdmSaveExecutor.SaveWithinContext()
   à IdeaBlade.EntityModel.Edm.EdmSaveExecutor.Save(DataSourceResolver dataSourceResolver, IDataSourceKey dsKey, SaveWorkState workState)



If i do the exact same thing without using the entityManager but the dbcontext it works fine.
to make it works with Devforce, I have to add this RelatedEntityList property and do the correct mapping in the dbcontext


below  a project where both case are implemented. Just uncomment the navigation property in the User class and the mapping in the dbcontext to make it work with devforce.

http://www.ideablade.com/forum/uploads/998/ConsoleApplication3.rar - uploads/998/ConsoleApplication3.rar



Replies:
Posted By: DenisK
Date Posted: 02-Apr-2012 at 12:46pm
Hi Walid,

Thank you for the sample solution. It does sound like a bug since it works with standard DbContext. I'll file a bug report.


Posted By: DenisK
Date Posted: 02-Apr-2012 at 7:03pm
Hi Walid,

Just want to let you know that one workaround that I've found for the time being is to add a ForeignKey that corresponds to the User nav prop. Something like this.

public class Document : BaseEntity {
    [Key]
    public int Id { get; set; }

    [Required]
    public string Title { get; set; }

    [ForeignKey("User")]
    public int User_Id { get; set; }

    [ForeignKey("User_Id")]
    public User User { get; set; }
  }

With the above, the uni-directional relationship should work. I hope this can help you proceed until we fix this bug.


Posted By: Walid
Date Posted: 03-Apr-2012 at 12:30am
Thank you for the workaround.

will it be fix in the next realease ? I would like to avoid to have to add  those foreignkey on my models which has actually 100+ entities


-------------


Posted By: DenisK
Date Posted: 03-Apr-2012 at 2:50pm
I've put this as high priority so yes we will try to include it in the next release.


Posted By: DenisK
Date Posted: 05-Apr-2012 at 1:25pm
Hi Walid,

After discussing this with our lead architect, it turns out that this is a known limitation (not a bug) with DevForce, CodeFirst or non CodeFirst.

See  http://drc.ideablade.com/xwiki/bin/view/Documentation/model-foreign-key-association - http://drc.ideablade.com/xwiki/bin/view/Documentation/model-foreign-key-association

Unlike EntityFramework, we require ForeignKey association to support n-tier serialization.  EntityFramework doesn't support n-tier and this is one of the trade-offs that we had to make in order to support what EntityFramework can't support.

We will also update the documentations to better explain this.

Hope this explains it.


Posted By: Walid
Date Posted: 06-Apr-2012 at 9:15am
Hi Denis,
 
Thank you for your answer, I will update my models to include the ForeignKey.
 
 


-------------



Print Page | Close Window