简体   繁体   中英

Entity Framework - Many to Many relationship not saving to database

I have stumbled upon a problem with Entity Framework this morning.

I have following code mapping a modified entity and saving it into database.

    public Group Save(Group x)
    {
        using (var db = new HostContext())
        {
            db.Projects.Attach(x.Project);

            if (x.ID != 0)
            {
                db.AttachableObjects.Attach(x);
                var manager = ((IObjectContextAdapter)db).ObjectContext.ObjectStateManager;
                manager.ChangeObjectState(x, EntityState.Modified);
            }
            else
            {
                db.AttachableObjects.Add(x);
            }

            db.SaveChanges();
            return x;
        }
    }

I call Save method with existing group as a parameter. Group contains one user I want to add as a member.

The method finishes successfully, however the relationship is not persisted in database. Any help is very appreciated.

EDIT: These are my classes

class User : AttachableObject
{

    ...

    private List<Group> memberof;

    [DataMember]
    [InverseProperty("Members")]
    public List<Group> MemberOf
    {
        get { return memberof; }
        set { memberof = value; }
    }

    ...
}

class Group : AttachableObject
{

    ...

    private List<User> members;

    [DataMember]
    [InverseProperty("MemberOf")]
    public List<User> Members
    {
        get { return members; }
        set { members = value; }
    }

    ...
}

EDIT2: This is where the Save method is called

    public Group AcceptInvite(int id)
    {
        var mapper = new InviteMapper();
        var userMapper = new UserMapper();
        var groupMapper = new GroupMapper();

        var invite = mapper.Find(id);

        if (invite != null)
        {
            var group = groupMapper.Find(invite.GroupID);
            var user = userMapper.Find(invite.InviteeID);

            group.Members.Add(user);

            mapper.Delete(invite.ID);
            return groupMapper.Save(group);
        }

        return null;
    }

EDIT3: My mappers

public class GroupMapper
{
    public Group Find(int id)
    {
        using (var db = new HostContext())
        {
            return db.AttachableObjects
                .Include("Project")
                .OfType<Group>().FirstOrDefault(x => x.ID == id);
        }
    }
}

The rest of the mappers is the same, only using their own tables.

You are not changing the relationship info of Project , you are only setting x to modified, relationship info must be changed explicitly.

So x.Project must have some property that points back to Group , you need to set it so the change is recorded.

I am guessing that x is resurrected via some deserialization process?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM