Search code examples
entity-frameworkc#-4.0wcf-data-servicesforeign-key-relationship

Trouble saving related object through Wcf DataService


To start off ive created a WCF Dataservice with Entity Framework.

On the client i try do the following

var user = (from u in ctx.FysPosUsers
                       where u.Username == "test"
                        select u).SingleOrDefault();

            ForumUser forumUser = new ForumUser();
            forumUser.DisplayName = "tester";
            forumUser.TagLine = "A random tagLine";
            forumUser.User = user;
            ctx.AddToForumUsers(forumUser);
            ctx.SaveChanges();

But when i try saving the context, i get the following error:

Entities in 'EntityContext.ForumUsers' participate in the 'ForumUser_User' relationship. 0 related 'ForumUser_User_Target' were found. 1 'ForumUser_User_Target' is expected.

Anyone thought on how this problem would could be solved will be greatly appriciated :)


Solution

  • Have you tried adding the following line before SaveChanges:

    ctx.SetLink(forumUser, "User", user);

    Hope this helps. Thanks Pratik