New Posts New Posts RSS Feed: EntityManagerSaveException with a CF model where a one to many navigation property end is mising
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

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

 Post Reply Post Reply
Author
Walid View Drop Down
Senior Member
Senior Member
Avatar

Joined: 14-Nov-2010
Posts: 161
Post Options Post Options   Quote Walid Quote  Post ReplyReply Direct Link To This Post Topic: EntityManagerSaveException with a CF model where a one to many navigation property end is mising
    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.



Edited by Walid - 30-Mar-2012 at 7:28am
Back to Top
DenisK View Drop Down
IdeaBlade
IdeaBlade


Joined: 25-Aug-2010
Posts: 715
Post Options Post Options   Quote DenisK Quote  Post ReplyReply Direct Link To This Post 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.
Back to Top
DenisK View Drop Down
IdeaBlade
IdeaBlade


Joined: 25-Aug-2010
Posts: 715
Post Options Post Options   Quote DenisK Quote  Post ReplyReply Direct Link To This Post 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.
Back to Top
Walid View Drop Down
Senior Member
Senior Member
Avatar

Joined: 14-Nov-2010
Posts: 161
Post Options Post Options   Quote Walid Quote  Post ReplyReply Direct Link To This Post 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
Back to Top
DenisK View Drop Down
IdeaBlade
IdeaBlade


Joined: 25-Aug-2010
Posts: 715
Post Options Post Options   Quote DenisK Quote  Post ReplyReply Direct Link To This Post 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.
Back to Top
DenisK View Drop Down
IdeaBlade
IdeaBlade


Joined: 25-Aug-2010
Posts: 715
Post Options Post Options   Quote DenisK Quote  Post ReplyReply Direct Link To This Post 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.


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.


Edited by DenisK - 05-Apr-2012 at 1:26pm
Back to Top
Walid View Drop Down
Senior Member
Senior Member
Avatar

Joined: 14-Nov-2010
Posts: 161
Post Options Post Options   Quote Walid Quote  Post ReplyReply Direct Link To This Post Posted: 06-Apr-2012 at 9:15am
Hi Denis,
 
Thank you for your answer, I will update my models to include the ForeignKey.
 
 
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down