none
RIA and Relational table inserts

    Question

  • Hello all,

    I'm trying to insert new data into a SQL table using RIA services and the EF.  Three tables are involved in this particular insert.  I have a Person table, a Phone table, and relational table between those two called PersonPhone.  PersonPhone contains PersonId from Person, PhoneTypeId, and PhoneId from Phone table.  The presentation involves a Person UI page which contains a grid with phone numbers in it.  The add phone button displays a popup window that accepts a phone number and phone number type.  When the save button is clicked from the popup window, the phone number is added to the phone collection and displayed on the phone grid of the person page.  The problem is that when the context is submitted, I can see in the domain service that PersonId and PhoneId are missing from the PersonPhone entity being added.  I was hoping that a Phone record would be created first and then that PhoneId would be added to PersonPhone but that does not seem to be the case.  Am I overlooking something related to the FK's?

     Thanks in advance

    Tuesday, May 05, 2009 3:29 PM

Answers

  • I finally got it working.  Here's what I ended up with.  Not sure how much or if all of this is needed yet. 

     

            public void AddPersonPhone(string PhoneNumber, string ExtensionNumber, AdminPhoneType PhoneType) {            Phone p = new Phone();            //PhoneData NewPhone = new PhoneData(); ;            PersonPhone NewPhone = new PersonPhone();             p.CreatedByUser = Globals.CurrentUser;            p.CreatedDate = DateTime.UtcNow;            p.ModifiedByUser = Globals.CurrentUser;            p.ModifiedDate = DateTime.UtcNow;            p.Number = PhoneNumber;            p.IsActive = true;             if (ExtensionNumber != null && ExtensionNumber.Length > 0) {                p.NumberExtension = ExtensionNumber;            }            //p.PersonPhone = NewPhone;             NewPhone.PhoneId = p.PhoneId;            //p.PhoneId = NewPhone.PhoneId;             NewPhone.Phone = p;            NewPhone.PhoneType = PhoneType;             this.SelectedPerson.Person.Phones.Add(NewPhone);            p.PersonPhone.Add(NewPhone);             context.Phones.Add(p);            context.PersonPhones.Add(NewPhone);            //p.PersonPhone.PhoneType = PhoneType;                       // NewPhone.Phone = p;          //  NewPhone.PhoneId = p.PhoneId;            //this.PhoneList.Add(NewPhone);            //NewPhone.PersonPhone.PhoneId = 10;             //this.SelectedPerson.Person.Phones.Add(NewPhone.PersonPhone);            //this.SelectedPerson.PhoneList.Add(NewPhone);            //this.SelectedPerson.PhoneList.Add(NewPhone.PersonPhone);            

            }

     

    Friday, May 08, 2009 6:07 PM

All replies

  • It sounds from your description that you realize .NET RIA Services doesn't currently support many to many mappings with an implicit "middle table". It sounds like your PersonPhone table is part of your data model and is being generated on the client. To get this to work, when you associate a new number with a person you'll need to:

    1. Create the new Phone entity
    2. Create the new PersonPhone link entity
    3. Add that link to both Person and Phone by doing a person.PhoneNumbers.Add(pf) and phone.Persons.Add(pf). Each will cause the corresponding FK value to be set. Alternatively, you could set the FK members manually yourself when you create the PersonPhone entity.

    Hope that helps. If not, you might post what your generated client association members look like so we can see if anything is awry there.

    Thursday, May 07, 2009 9:22 PM
  • Still can't get this working.  It seems like the issue is that the phoneId is not avaialable when the PersonPhone record is added (although it's hard to tell from the exception - inner exception is null).  The entity framework and RIA should be handling that though?  I shouldn't have to create a phone, get the phoneId back, add that to the person phone, and then create the person phone?   AddPersonPhone method below is called from the popup window and then displayed on the person info page in a phone grid.  Context.SubmitChanges is then called from the person info page and this is the stack trace that i'm getting from SubmittedChangesEventArgs.

    I've included the generated client members at the bottom.   I'd be happy to provide more info if need be.

    Thanks for the help.

    at System.Data.Mapping.Update.Internal.UpdateTranslator.Update(IEntityStateManager stateManager, IEntityAdapter adapter)
       at System.Data.EntityClient.EntityAdapter.Update(IEntityStateManager entityCache)
       at System.Data.Objects.ObjectContext.SaveChanges(Boolean acceptChangesDuringSave)
       at System.Data.Objects.ObjectContext.SaveChanges()
       at System.Web.DomainServices.LinqToEntities.LinqToEntitiesDomainService.PersistChangeSet(ChangeSet changeSet)
       at System.Web.DomainServices.DomainService.Submit(ChangeSet changeSet)
       at System.Web.Ria.DataServiceSubmitRequest.Invoke(DomainService domainService)
       at System.Web.Ria.DataService.System.Web.IHttpHandler.ProcessRequest(HttpContext context)

     

    public void AddPersonPhone(string PhoneNumber, string ExtensionNumber, PhoneType PhoneType) {

    Phone p = new Phone();

    PersonPhone NewPhone = new PersonPhone();

    p.CreatedByUser = Globals.CurrentUser;

    p.CreatedDate = DateTime.UtcNow;

    p.ModifiedByUser = Globals.CurrentUser;

    p.ModifiedDate = DateTime.UtcNow;

    p.Number = PhoneNumber;

    p.IsActive =
    true;if (ExtensionNumber != null && ExtensionNumber.Length > 0) {

    p.NumberExtension = ExtensionNumber;

    }

    //p.PersonPhone = NewPhone;

    NewPhone.PhoneId = p.PhoneId;

    p.PhoneId = NewPhone.PhoneId;

    NewPhone.PhoneType = PhoneType;

    this.SelectedPerson.Person.Phones.Add(NewPhone);

    p.PersonPhone.Add(NewPhone);

     

    //p.PersonPhone.PhoneType = PhoneType;

     

    // NewPhone.Phone = p;

    // NewPhone.PhoneId = p.PhoneId;

    //context.Phones.Add(p);

    //context.PersonPhones.Add(NewPhone);

     

     

    //this.PhoneList.Add(NewPhone);

    //NewPhone.PersonPhone.PhoneId = 10;

    //this.SelectedPerson.Person.Phones.Add(NewPhone.PersonPhone);

    //this.SelectedPerson.PhoneList.Add(NewPhone);

    //this.SelectedPerson.PhoneList.Add(NewPhone.PersonPhone);

     

    }

     

        [DataContract(Namespace = "http://schemas.datacontract.org/2004/07/Impact.Web")]    public sealed partial class Phone : Entity {         private int _createdBy;         private EntityRef<User> _createdByUser;         private DateTime _createdDate;         private bool _isActive;         private int _modifiedBy;         private EntityRef<User> _modifiedByUser;         private DateTime _modifiedDate;         private string _number;         private string _numberExtension;         private EntityCollection<PersonPhone> _personPhone;         private int _phoneId;         [DataMember()]        public int CreatedBy {            get {                return this._createdBy;            }            set {                if ((this._createdBy != value)) {                    this.ValidateProperty("CreatedBy", value);                    this.RaiseDataMemberChanging("CreatedBy");                    this._createdBy = value;                    this.RaiseDataMemberChanged("CreatedBy");                }            }        }         [Association("User_Phone", "CreatedBy", "AdminApplicationUserId", IsForeignKey = true)]        public User CreatedByUser {            get {                if ((this._createdByUser == null)) {                    this._createdByUser = new EntityRef<User>(this, "CreatedByUser", this.FilterCreatedByUser);                }                return this._createdByUser.Entity;            }            set {                User previous = this.CreatedByUser;                if ((previous != value)) {                    if ((previous != null)) {                        previous.PhoneCreatedByUsers.Remove(this);                    }                    this._createdByUser.Entity = value;                    if ((value != null)) {                        value.PhoneCreatedByUsers.Add(this);                        this.CreatedBy = value.AdminApplicationUserId;                    } else {                        this.CreatedBy = default(int);                    }                }            }        }         [DataMember()]        public DateTime CreatedDate {            get {                return this._createdDate;            }            set {                if ((this._createdDate != value)) {                    this.ValidateProperty("CreatedDate", value);                    this.RaiseDataMemberChanging("CreatedDate");                    this._createdDate = value;                    this.RaiseDataMemberChanged("CreatedDate");                }            }        }         [DataMember()]        public bool IsActive {            get {                return this._isActive;            }            set {                if ((this._isActive != value)) {                    this.ValidateProperty("IsActive", value);                    this.RaiseDataMemberChanging("IsActive");                    this._isActive = value;                    this.RaiseDataMemberChanged("IsActive");                }            }        }         [DataMember()]        public int ModifiedBy {            get {                return this._modifiedBy;            }            set {                if ((this._modifiedBy != value)) {                    this.ValidateProperty("ModifiedBy", value);                    this.RaiseDataMemberChanging("ModifiedBy");                    this._modifiedBy = value;                    this.RaiseDataMemberChanged("ModifiedBy");                }            }        }         [Association("User_Phone1", "ModifiedBy", "AdminApplicationUserId", IsForeignKey = true)]        public User ModifiedByUser {            get {                if ((this._modifiedByUser == null)) {                    this._modifiedByUser = new EntityRef<User>(this, "ModifiedByUser", this.FilterModifiedByUser);                }                return this._modifiedByUser.Entity;            }            set {                User previous = this.ModifiedByUser;                if ((previous != value)) {                    if ((previous != null)) {                        previous.PhoneModifiedByUsers.Remove(this);                    }                    this._modifiedByUser.Entity = value;                    if ((value != null)) {                        value.PhoneModifiedByUsers.Add(this);                        this.ModifiedBy = value.AdminApplicationUserId;                    } else {                        this.ModifiedBy = default(int);                    }                }            }        }         [DataMember()]        public DateTime ModifiedDate {            get {                return this._modifiedDate;            }            set {                if ((this._modifiedDate != value)) {                    this.ValidateProperty("ModifiedDate", value);                    this.RaiseDataMemberChanging("ModifiedDate");                    this._modifiedDate = value;                    this.RaiseDataMemberChanged("ModifiedDate");                }            }        }         [DataMember()]        public string Number {            get {                return this._number;            }            set {                if ((this._number != value)) {                    this.ValidateProperty("Number", value);                    this.RaiseDataMemberChanging("Number");                    this._number = value;                    this.RaiseDataMemberChanged("Number");                }            }        }         [DataMember()]        public string NumberExtension {            get {                return this._numberExtension;            }            set {                if ((this._numberExtension != value)) {                    this.ValidateProperty("NumberExtension", value);                    this.RaiseDataMemberChanging("NumberExtension");                    this._numberExtension = value;                    this.RaiseDataMemberChanged("NumberExtension");                }            }        }         [Association("Phone_PersonPhone", "PhoneId", "PhoneId")]        public EntityCollection<PersonPhone> PersonPhone {            get {                if ((this._personPhone == null)) {                    this._personPhone = new EntityCollection<PersonPhone>(this, "PersonPhone", this.FilterPersonPhone, this.AttachPersonPhone, this.DetachPersonPhone);                }                return this._personPhone;            }        }         [DataMember()]        [Key()]        public int PhoneId {            get {                return this._phoneId;            }            set {                if ((this._phoneId != value)) {                    this.ValidateProperty("PhoneId", value);                    this.RaiseDataMemberChanging("PhoneId");                    this._phoneId = value;                    this.RaiseDataMemberChanged("PhoneId");                }            }        }         private bool FilterCreatedByUser(User entity) {            return (entity.AdminApplicationUserId == this.CreatedBy);        }         private bool FilterModifiedByUser(User entity) {            return (entity.AdminApplicationUserId == this.ModifiedBy);        }         private void AttachPersonPhone(PersonPhone entity) {            entity.Phone = this;        }         private void DetachPersonPhone(PersonPhone entity) {            entity.Phone = null;        }         private bool FilterPersonPhone(PersonPhone entity) {            return (entity.PhoneId == this.PhoneId);        }         public override object GetIdentity() {            return this._phoneId;        }

        }

        [DataContract(Namespace = "http://schemas.datacontract.org/2004/07/Impact.Web")]    public sealed partial class PersonPhone : Entity {         private int _adminPhoneTypeId;         private EntityRef<Person> _person;         private int _personId;         private int _personPhoneId;         private EntityRef<Phone> _phone;         private int _phoneId;         private EntityRef<AdminPhoneType> _phoneType;         [DataMember()]        public int AdminPhoneTypeId {            get {                return this._adminPhoneTypeId;            }            set {                if ((this._adminPhoneTypeId != value)) {                    this.ValidateProperty("AdminPhoneTypeId", value);                    this.RaiseDataMemberChanging("AdminPhoneTypeId");                    this._adminPhoneTypeId = value;                    this.RaiseDataMemberChanged("AdminPhoneTypeId");                }            }        }         [Association("Person_PersonPhone", "PersonId", "PersonId", IsForeignKey = true)]        public Person Person {            get {                if ((this._person == null)) {                    this._person = new EntityRef<Person>(this, "Person", this.FilterPerson);                }                return this._person.Entity;            }            set {                Person previous = this.Person;                if ((previous != value)) {                    if ((previous != null)) {                        previous.Phones.Remove(this);                    }                    this._person.Entity = value;                    if ((value != null)) {                        value.Phones.Add(this);                        this.PersonId = value.PersonId;                    } else {                        this.PersonId = default(int);                    }                }            }        }         [DataMember()]        public int PersonId {            get {                return this._personId;            }            set {                if ((this._personId != value)) {                    this.ValidateProperty("PersonId", value);                    this.RaiseDataMemberChanging("PersonId");                    this._personId = value;                    this.RaiseDataMemberChanged("PersonId");                }            }        }         [DataMember()]        [Key()]        public int PersonPhoneId {            get {                return this._personPhoneId;            }            set {                if ((this._personPhoneId != value)) {                    this.ValidateProperty("PersonPhoneId", value);                    this.RaiseDataMemberChanging("PersonPhoneId");                    this._personPhoneId = value;                    this.RaiseDataMemberChanged("PersonPhoneId");                }            }        }         [Association("Phone_PersonPhone", "PhoneId", "PhoneId", IsForeignKey = true)]        public Phone Phone {            get {                if ((this._phone == null)) {                    this._phone = new EntityRef<Phone>(this, "Phone", this.FilterPhone);                }                return this._phone.Entity;            }            set {                Phone previous = this.Phone;                if ((previous != value)) {                    if ((previous != null)) {                        previous.PersonPhone.Remove(this);                    }                    this._phone.Entity = value;                    if ((value != null)) {                        value.PersonPhone.Add(this);                        this.PhoneId = value.PhoneId;                    } else {                        this.PhoneId = default(int);                    }                }            }        }         [DataMember()]        public int PhoneId {            get {                return this._phoneId;            }            set {                if ((this._phoneId != value)) {                    this.ValidateProperty("PhoneId", value);                    this.RaiseDataMemberChanging("PhoneId");                    this._phoneId = value;                    this.RaiseDataMemberChanged("PhoneId");                }            }        }         [Association("AdminPhoneType_PersonPhone", "AdminPhoneTypeId", "AdminPhoneTypeId", IsForeignKey = true)]        public AdminPhoneType PhoneType {            get {                if ((this._phoneType == null)) {                    this._phoneType = new EntityRef<AdminPhoneType>(this, "PhoneType", this.FilterPhoneType);                }                return this._phoneType.Entity;            }            set {                AdminPhoneType previous = this.PhoneType;                if ((previous != value)) {                    if ((previous != null)) {                        previous.PersonPhone.Remove(this);                    }                    this._phoneType.Entity = value;                    if ((value != null)) {                        value.PersonPhone.Add(this);                        this.AdminPhoneTypeId = value.AdminPhoneTypeId;                    } else {                        this.AdminPhoneTypeId = default(int);                    }                }            }        }         private bool FilterPerson(Person entity) {            return (entity.PersonId == this.PersonId);        }         private bool FilterPhone(Phone entity) {            return (entity.PhoneId == this.PhoneId);        }         private bool FilterPhoneType(AdminPhoneType entity) {            return (entity.AdminPhoneTypeId == this.AdminPhoneTypeId);                public override object GetIdentity() {            return this._personPhoneId;        }    }     [DataContract(Namespace = "http://schemas.datacontract.org/2004/07/Impact.Web")]    public sealed partial class Person : Entity {         private EntityCollection<PersonAddress> _addresses;         private Nullable<int> _adminEthnicityId;         private Nullable<int> _adminGenderId;         private Nullable<int> _adminMaritalStatusId;         private Nullable<DateTime> _birthdate;         private int _createdBy;         private EntityRef<User> _createdByUser;         private DateTime _createdDate;         private EntityRef<AdminEthnicity> _ethnicity;         private string _firstName;         private EntityRef<AdminGender> _gender;         private string _initial;         private bool _isActive;         private string _lastName;         private EntityRef<AdminMaritalStatus> _maritalStatus;         private int _modifiedBy;         private EntityRef<User> _modifiedByUser;         private DateTime _modifiedDate;         private string _nickname;         private EntityCollection<PersonCenter> _personCenters;         private int _personId;         private EntityCollection<PersonRaceSelection> _personRaceSelection;         private EntityCollection<PersonPhone> _phones;         private string _prefix;         private string _socialSecurityNumber;         private string _suffix;         [Association("Person_PersonAddress", "PersonId", "PersonId")]        public EntityCollection<PersonAddress> Addresses {            get {                if ((this._addresses == null)) {                    this._addresses = new EntityCollection<PersonAddress>(this, "Addresses", this.FilterAddresses, this.AttachAddresses, this.DetachAddresses);                }                return this._addresses;            }        }         [DataMember()]        public Nullable<int> AdminEthnicityId {            get {                return this._adminEthnicityId;            }            set {                if ((this._adminEthnicityId != value)) {                    this.ValidateProperty("AdminEthnicityId", value);                    this.RaiseDataMemberChanging("AdminEthnicityId");                    this._adminEthnicityId = value;                    this.RaiseDataMemberChanged("AdminEthnicityId");                }            }        }         [DataMember()]        public Nullable<int> AdminGenderId {            get {                return this._adminGenderId;            }            set {                if ((this._adminGenderId != value)) {                    this.ValidateProperty("AdminGenderId", value);                    this.RaiseDataMemberChanging("AdminGenderId");                    this._adminGenderId = value;                    this.RaiseDataMemberChanged("AdminGenderId");                }            }        }         [DataMember()]        public Nullable<int> AdminMaritalStatusId {            get {                return this._adminMaritalStatusId;            }            set {                if ((this._adminMaritalStatusId != value)) {                    this.ValidateProperty("AdminMaritalStatusId", value);                    this.RaiseDataMemberChanging("AdminMaritalStatusId");                    this._adminMaritalStatusId = value;                    this.RaiseDataMemberChanged("AdminMaritalStatusId");                }            }        }         [DataMember()]        public Nullable<DateTime> Birthdate {            get {                return this._birthdate;            }            set {                if ((this._birthdate != value)) {                    this.ValidateProperty("Birthdate", value);                    this.RaiseDataMemberChanging("Birthdate");                    this._birthdate = value;                    this.RaiseDataMemberChanged("Birthdate");                }            }        }         [DataMember()]        public int CreatedBy {            get {                return this._createdBy;            }            set {                if ((this._createdBy != value)) {                    this.ValidateProperty("CreatedBy", value);                    this.RaiseDataMemberChanging("CreatedBy");                    this._createdBy = value;                    this.RaiseDataMemberChanged("CreatedBy");                }            }        }         [Association("User_Person", "CreatedBy", "AdminApplicationUserId", IsForeignKey = true)]        public User CreatedByUser {            get {                if ((this._createdByUser == null)) {                    this._createdByUser = new EntityRef<User>(this, "CreatedByUser", this.FilterCreatedByUser);                }                return this._createdByUser.Entity;            }            set {                User previous = this.CreatedByUser;                if ((previous != value)) {                    if ((previous != null)) {                        previous.PersonCreatedByUsers.Remove(this);                    }                    this._createdByUser.Entity = value;                    if ((value != null)) {                        value.PersonCreatedByUsers.Add(this);                        this.CreatedBy = value.AdminApplicationUserId;                    } else {                        this.CreatedBy = default(int);                    }                }            }        }         [DataMember()]        public DateTime CreatedDate {            get {                return this._createdDate;            }            set {                if ((this._createdDate != value)) {                    this.ValidateProperty("CreatedDate", value);                    this.RaiseDataMemberChanging("CreatedDate");                    this._createdDate = value;                    this.RaiseDataMemberChanged("CreatedDate");                }            }        }         [Association("AdminEthnicity_Person", "AdminEthnicityId", "AdminEthnicityId", IsForeignKey = true)]        public AdminEthnicity Ethnicity {            get {                if ((this._ethnicity == null)) {                    this._ethnicity = new EntityRef<AdminEthnicity>(this, "Ethnicity", this.FilterEthnicity);                }                return this._ethnicity.Entity;            }            set {                AdminEthnicity previous = this.Ethnicity;                if ((previous != value)) {                    if ((previous != null)) {                        previous.Person.Remove(this);                    }                    this._ethnicity.Entity = value;                    if ((value != null)) {                        value.Person.Add(this);                        this.AdminEthnicityId = value.AdminEthnicityId;                    } else {                        this.AdminEthnicityId = default(Nullable<int>);                    }                }            }        }         [DataMember()]        [Required()]        public string FirstName {            get {                return this._firstName;            }            set {                if ((this._firstName != value)) {                    this.ValidateProperty("FirstName", value);                    this.RaiseDataMemberChanging("FirstName");                    this._firstName = value;                    this.RaiseDataMemberChanged("FirstName");                }            }        }         [Association("AdminGender_Person", "AdminGenderId", "AdminGenderId", IsForeignKey = true)]        public AdminGender Gender {            get {                if ((this._gender == null)) {                    this._gender = new EntityRef<AdminGender>(this, "Gender", this.FilterGender);                }                return this._gender.Entity;            }            set {                AdminGender previous = this.Gender;                if ((previous != value)) {                    if ((previous != null)) {                        previous.Person.Remove(this);                    }                    this._gender.Entity = value;                    if ((value != null)) {                        value.Person.Add(this);                        this.AdminGenderId = value.AdminGenderId;                    } else {                        this.AdminGenderId = default(Nullable<int>);                    }                }            }        }         [DataMember()]        public string Initial {            get {                return this._initial;            }            set {                if ((this._initial != value)) {                    this.ValidateProperty("Initial", value);                    this.RaiseDataMemberChanging("Initial");                    this._initial = value;                    this.RaiseDataMemberChanged("Initial");                }            }        }         [DataMember()]        public bool IsActive {            get {                return this._isActive;            }            set {                if ((this._isActive != value)) {                    this.ValidateProperty("IsActive", value);                    this.RaiseDataMemberChanging("IsActive");                    this._isActive = value;                    this.RaiseDataMemberChanged("IsActive");                }            }        }         [DataMember()]        [Required()]        public string LastName {            get {                return this._lastName;            }            set {                if ((this._lastName != value)) {                    this.ValidateProperty("LastName", value);                    this.RaiseDataMemberChanging("LastName");                    this._lastName = value;                    this.RaiseDataMemberChanged("LastName");                }            }        }         [Association("AdminMaritalStatus_Person", "AdminMaritalStatusId", "AdminMaritalStatusId", IsForeignKey = true)]        public AdminMaritalStatus MaritalStatus {            get {                if ((this._maritalStatus == null)) {                    this._maritalStatus = new EntityRef<AdminMaritalStatus>(this, "MaritalStatus", this.FilterMaritalStatus);                }                return this._maritalStatus.Entity;            }            set {                AdminMaritalStatus previous = this.MaritalStatus;                if ((previous != value)) {                    if ((previous != null)) {                        previous.Person.Remove(this);                    }                    this._maritalStatus.Entity = value;                    if ((value != null)) {                        value.Person.Add(this);                        this.AdminMaritalStatusId = value.AdminMaritalStatusId;                    } else {                        this.AdminMaritalStatusId = default(Nullable<int>);                    }                }            }        }         [DataMember()]        public int ModifiedBy {            get {                return this._modifiedBy;            }            set {                if ((this._modifiedBy != value)) {                    this.ValidateProperty("ModifiedBy", value);                    this.RaiseDataMemberChanging("ModifiedBy");                    this._modifiedBy = value;                    this.RaiseDataMemberChanged("ModifiedBy");                }            }        }         [Association("User_Person1", "ModifiedBy", "AdminApplicationUserId", IsForeignKey = true)]        public User ModifiedByUser {            get {                if ((this._modifiedByUser == null)) {                    this._modifiedByUser = new EntityRef<User>(this, "ModifiedByUser", this.FilterModifiedByUser);                }                return this._modifiedByUser.Entity;            }            set {                User previous = this.ModifiedByUser;                if ((previous != value)) {                    if ((previous != null)) {                        previous.PersonModifedByUsers.Remove(this);                    }                    this._modifiedByUser.Entity = value;                    if ((value != null)) {                        value.PersonModifedByUsers.Add(this);                        this.ModifiedBy = value.AdminApplicationUserId;                    } else {                        this.ModifiedBy = default(int);                    }                }            }        }         [DataMember()]        public DateTime ModifiedDate {            get {                return this._modifiedDate;            }            set {                if ((this._modifiedDate != value)) {                    this.ValidateProperty("ModifiedDate", value);                    this.RaiseDataMemberChanging("ModifiedDate");                    this._modifiedDate = value;                    this.RaiseDataMemberChanged("ModifiedDate");                }            }        }         [DataMember()]        public string Nickname {            get {                return this._nickname;            }            set {                if ((this._nickname != value)) {                    this.ValidateProperty("Nickname", value);                    this.RaiseDataMemberChanging("Nickname");                    this._nickname = value;                    this.RaiseDataMemberChanged("Nickname");                }            }        }         [Association("Person_PersonCenter", "PersonId", "PersonId")]        public EntityCollection<PersonCenter> PersonCenters {            get {                if ((this._personCenters == null)) {                    this._personCenters = new EntityCollection<PersonCenter>(this, "PersonCenters", this.FilterPersonCenters, this.AttachPersonCenters, this.DetachPersonCenters);                }                return this._personCenters;            }        }         [DataMember()]        [Key()]        public int PersonId {            get {                return this._personId;            }            set {                if ((this._personId != value)) {                    this.ValidateProperty("PersonId", value);                    this.RaiseDataMemberChanging("PersonId");                    this._personId = value;                    this.RaiseDataMemberChanged("PersonId");                }            }        }         [Association("Person_PersonRaceSelection", "PersonId", "PersonId")]        public EntityCollection<PersonRaceSelection> PersonRaceSelection {            get {                if ((this._personRaceSelection == null)) {                    this._personRaceSelection = new EntityCollection<PersonRaceSelection>(this, "PersonRaceSelection", this.FilterPersonRaceSelection, this.AttachPersonRaceSelection, this.DetachPersonRaceSelection);                }                return this._personRaceSelection;            }        }         [Association("Person_PersonPhone", "PersonId", "PersonId")]        public EntityCollection<PersonPhone> Phones {            get {                if ((this._phones == null)) {                    this._phones = new EntityCollection<PersonPhone>(this, "Phones", this.FilterPhones, this.AttachPhones, this.DetachPhones);                }                return this._phones;            }        }         [DataMember()]        public string Prefix {            get {                return this._prefix;            }            set {                if ((this._prefix != value)) {                    this.ValidateProperty("Prefix", value);                    this.RaiseDataMemberChanging("Prefix");                    this._prefix = value;                    this.RaiseDataMemberChanged("Prefix");                }            }        }         [DataMember()]        public string SocialSecurityNumber {            get {                return this._socialSecurityNumber;            }            set {                if ((this._socialSecurityNumber != value)) {                    this.ValidateProperty("SocialSecurityNumber", value);                    this.RaiseDataMemberChanging("SocialSecurityNumber");                    this._socialSecurityNumber = value;                    this.RaiseDataMemberChanged("SocialSecurityNumber");                }            }        }         [DataMember()]        public string Suffix {            get {                return this._suffix;            }            set {                if ((this._suffix != value)) {                    this.ValidateProperty("Suffix", value);                    this.RaiseDataMemberChanging("Suffix");                    this._suffix = value;                    this.RaiseDataMemberChanged("Suffix");                }            }        }         private void AttachAddresses(PersonAddress entity) {            entity.Person = this;        }         private void DetachAddresses(PersonAddress entity) {            entity.Person = null;        }         private bool FilterAddresses(PersonAddress entity) {            return (entity.PersonId == this.PersonId);        }         private bool FilterCreatedByUser(User entity) {            return (entity.AdminApplicationUserId == this.CreatedBy);        }         private bool FilterEthnicity(AdminEthnicity entity) {            return (entity.AdminEthnicityId == this.AdminEthnicityId);        }         private bool FilterGender(AdminGender entity) {            return (entity.AdminGenderId == this.AdminGenderId);        }         private bool FilterMaritalStatus(AdminMaritalStatus entity) {            return (entity.AdminMaritalStatusId == this.AdminMaritalStatusId);        }         private bool FilterModifiedByUser(User entity) {            return (entity.AdminApplicationUserId == this.ModifiedBy);        }         private void AttachPersonCenters(PersonCenter entity) {            entity.Person = this;        }         private void DetachPersonCenters(PersonCenter entity) {            entity.Person = null;        }         private bool FilterPersonCenters(PersonCenter entity) {            return (entity.PersonId == this.PersonId);        }         private void AttachPersonRaceSelection(PersonRaceSelection entity) {            entity.Person = this;        }         private void DetachPersonRaceSelection(PersonRaceSelection entity) {            entity.Person = null;        }         private bool FilterPersonRaceSelection(PersonRaceSelection entity) {            return (entity.PersonId == this.PersonId);        }         private void AttachPhones(PersonPhone entity) {            entity.Person = this;        }         private void DetachPhones(PersonPhone entity) {            entity.Person = null;        }         private bool FilterPhones(PersonPhone entity) {            return (entity.PersonId == this.PersonId);        }         public override object GetIdentity() {            return this._personId;        }

        }

     

    Friday, May 08, 2009 1:42 PM
  • I finally got it working.  Here's what I ended up with.  Not sure how much or if all of this is needed yet. 

     

            public void AddPersonPhone(string PhoneNumber, string ExtensionNumber, AdminPhoneType PhoneType) {            Phone p = new Phone();            //PhoneData NewPhone = new PhoneData(); ;            PersonPhone NewPhone = new PersonPhone();             p.CreatedByUser = Globals.CurrentUser;            p.CreatedDate = DateTime.UtcNow;            p.ModifiedByUser = Globals.CurrentUser;            p.ModifiedDate = DateTime.UtcNow;            p.Number = PhoneNumber;            p.IsActive = true;             if (ExtensionNumber != null && ExtensionNumber.Length > 0) {                p.NumberExtension = ExtensionNumber;            }            //p.PersonPhone = NewPhone;             NewPhone.PhoneId = p.PhoneId;            //p.PhoneId = NewPhone.PhoneId;             NewPhone.Phone = p;            NewPhone.PhoneType = PhoneType;             this.SelectedPerson.Person.Phones.Add(NewPhone);            p.PersonPhone.Add(NewPhone);             context.Phones.Add(p);            context.PersonPhones.Add(NewPhone);            //p.PersonPhone.PhoneType = PhoneType;                       // NewPhone.Phone = p;          //  NewPhone.PhoneId = p.PhoneId;            //this.PhoneList.Add(NewPhone);            //NewPhone.PersonPhone.PhoneId = 10;             //this.SelectedPerson.Person.Phones.Add(NewPhone.PersonPhone);            //this.SelectedPerson.PhoneList.Add(NewPhone);            //this.SelectedPerson.PhoneList.Add(NewPhone.PersonPhone);            

            }

     

    Friday, May 08, 2009 6:07 PM
  • As you can see, the forum garbled all the code you posted to the point where it's unparsable :( However, you shouldn't have to write all the complicated code it appears you're writing. Since PersonPhone has two simple EntityRefs to two entitities, you should be able to do:

    NewPhone.Phone = phone;
    NewPhone.Person = person;  
    NewPhone.PhoneType = PhoneType;

    If you're having to repeat yourself or do complicated things, something is wrong. Doesn't what I suggest work? If you step into the generated code for PersonPhone.Phone and PersonPhone.Person, you should see that we generate the code to sync the FK values for you. Therefore simply assigning the references as I suggest should work.

    Friday, May 08, 2009 6:31 PM
  • Phone p = new Phone(); 

    ...set properties of p...

    NewPhone.Phone = p;

    NewPhone.Person = this.SelectedPerson.Person;

    NewPhone.PhoneType = PhoneType;

    context.Phones.Add(p);

    context.PersonPhones.Add(NewPhone);

     

    The above code works.  It sounds like you are suggesting that I don't need the last two lines of code.  Without them, the phone grid gets the added phone entity from the phone add popup window, but when the save is clicked from the main window and I inspect the SubmittedChangesEventArgs after the context is submitted, the ChangeSet property contains 0 changes.  If I don't need those two lines, do you have any suggestions as to where I should look for something that may be wrong?

    Sorry about the garbled code.  I'll try pasting it into textpad next time and see if that helps.

     

    Thanks

    Monday, May 11, 2009 9:57 AM
  • You don't need both of the last two lines. You just need to add the NewPhone entity to the context, and the Phone will be inferred at submit time as an add. You've constructed a disconected tree of objects, and to connect them you can Add any single node to the context - Add will recursively add the rest.

    Monday, May 11, 2009 6:13 PM
  • I'm guessing I must have an issue with my EF.  If I use this line

    context.PersonPhones.Add(NewPhone);

    then I'm back to getting this exception

    at System.Data.Mapping.Update.Internal.UpdateTranslator.Update(IEntityStateManager stateManager, IEntityAdapter adapter)
       at System.Data.EntityClient.EntityAdapter.Update(IEntityStateManager entityCache)
       at System.Data.Objects.ObjectContext.SaveChanges(Boolean acceptChangesDuringSave)
       at System.Data.Objects.ObjectContext.SaveChanges()
       at System.Web.DomainServices.LinqToEntities.LinqToEntitiesDomainService.PersistChangeSet(ChangeSet changeSet)
       at System.Web.DomainServices.DomainService.Submit(ChangeSet changeSet)
       at System.Web.Ria.DataServiceSubmitRequest.Invoke(DomainService domainService)
       at System.Web.Ria.DataService.System.Web.IHttpHandler.ProcessRequest(HttpContext context)

    If I use the other line

    context.Phones.Add(p);

    the Phone is added but not the link entity.  It works OK if both are added.  I'm not sure where or what to look for to figure this out?

     

    Thanks

    Monday, May 11, 2009 6:28 PM
  • To figure out what's going on, you start with the client and the changeset that will be submitted (DataContext.GetChanges()) and verify that everything is as it should be. If not, then debug into the series of FK property sets in the generated code resulting from your context.PersonPhones.Add call. As I mentioned Adds are recursive - from the entity being added we navigate all EntityRef/EntitiyCollections and eagerly add any unattached entities we find.

    If you have a small self contained repro you could send privately I could debug further.

    Tuesday, May 12, 2009 12:47 PM
  • We migrated from the March CTP to the May CTP and the changes brought about some new issues.  In a private email, mathewc mentioned that you shouldn't use 2 DataContexts (unless you know what you're doing)....

    "The way the eager/inferred adds work is any time you add an entity to a context, any related unattached entities are inferred as adds. So in your case above, where did the AdminEthnicity entity come from? We're apparently inferring it as an add because it is unattached. Is this entity from another context? You don't want to associate entities from different contexts (unless you're using [External] associations - if you don't know what that is, don't worry about it). I agree that you shouldn't have to add the CUD methods to get stuff to work." 

    THE ANSWER IS...(this was my email back to mathew on 5/13) 

    We verified that all of the issues that we're having are related to using two datacontexts.  Our app uses the first context when the user logins.  The user entity is stored in the first context.  When a module is opened, for example scheduling, we create a new context.  We assume we need to do it that way so we don't eventually end up with the entire database being loaded into the context. 

    When an item is added to an entity, we're setting the create user to the user that was created on the first context during login.  We didn't run into this issue in the March release (guessing you guys tightened something up), but I was still having the issue with adding phones.  I am able to set the userId int value of the phone being added and now adding phones seems to work ok.

    I have a couple of new questions now.  This is the first we're hearing about using multiple contexts, is this documented somewhere?  How would you recomend we handle the user object in our case?  We were just discussing it, and our initial thought is to use the AddReference method of the context.  We're wondering how the garbage collection will be handled?

    -----

    We have since tried using the AddReference but not have been able to make that work.  I guess I'll start a new post looking for a new answer to a new question.

    Thursday, May 14, 2009 4:29 PM