public void SingletonClientTest() { Random rand = new Random(); ODataFormat[] formats = { ODataFormat.Json }; foreach (var format in formats) { //Query Singleton TestClientContext.MergeOption = Microsoft.OData.Client.MergeOption.OverwriteChanges; var company = TestClientContext.Company.GetValue(); Assert.IsTrue(company != null); //Update Singleton Property and Verify company.CompanyCategory = CompanyCategory.Communication; company.Name = "UpdatedName"; company.Address.City = "UpdatedCity"; TestClientContext.UpdateObject(company); TestClientContext.SaveChanges(Microsoft.OData.Client.SaveChangesOptions.ReplaceOnUpdate); //Query Singleton Property - Select var companyCategory = TestClientContext.Company.Select(c => c.CompanyCategory).GetValue(); Assert.IsTrue(companyCategory == CompanyCategory.Communication); var city = TestClientContext.CreateSingletonQuery <string>("Company/Address/City").Execute().Single(); Assert.IsTrue(city == "UpdatedCity"); var name = TestClientContext.Execute <string>(new Uri("Company/Name", UriKind.Relative)).Single(); Assert.IsTrue(name == "UpdatedName"); //Projection with properties - Select company = TestClientContext.Company.Select(c => new Company { CompanyID = c.CompanyID, Address = c.Address, Name = c.Name }).GetValue(); Assert.IsTrue(company != null); Assert.IsTrue(company.Name == "UpdatedName"); //Load Navigation Property //Singleton TestClientContext.LoadProperty(company, "VipCustomer"); Assert.IsTrue(company.VipCustomer != null); //Collection TestClientContext.LoadProperty(company, "Departments"); Assert.IsTrue(company.Departments != null); Assert.IsTrue(company.Departments.Count > 0); //Single Entity TestClientContext.LoadProperty(company, "CoreDepartment"); Assert.IsTrue(company.CoreDepartment != null); //Add Navigation Property - Collection company = TestClientContext.Company.GetValue(); int tmpDepartmentID = rand.Next(); int tmpCoreDepartmentID = rand.Next(); Department department = new Department() { DepartmentID = tmpDepartmentID, Name = "ID" + tmpDepartmentID, }; Department coreDepartment = new Department() { DepartmentID = tmpCoreDepartmentID, Name = "ID" + tmpCoreDepartmentID, }; TestClientContext.AddToDepartments(department); TestClientContext.AddLink(company, "Departments", department); TestClientContext.SaveChanges(); TestClientContext.AddToDepartments(coreDepartment); TestClientContext.AddLink(company, "Departments", coreDepartment); TestClientContext.SaveChanges(); TestClientContext.Departments.ToList(); //Projection with Navigation properties - Select company = TestClientContext.Company.Select(c => new Company { CompanyID = c.CompanyID, Departments = c.Departments }).GetValue(); Assert.IsTrue(company != null); Assert.IsTrue(company.Departments.Any(c => c.DepartmentID == tmpDepartmentID)); Assert.IsTrue(company.Departments.Any(c => c.DepartmentID == tmpCoreDepartmentID)); //Update Navigation Property - Single Entity TestClientContext.SetLink(company, "CoreDepartment", coreDepartment); TestClientContext.SaveChanges(); //Projection with Navigation properties - Select company = TestClientContext.Company.Select(c => new Company { CompanyID = c.CompanyID, CoreDepartment = c.CoreDepartment }).GetValue(); Assert.IsTrue(company != null); Assert.IsTrue(company.CoreDepartment.DepartmentID == tmpCoreDepartmentID); //Update EntitySet's Navigation Property - Singleton TestClientContext.SetLink(department, "Company", company); TestClientContext.SaveChanges(Microsoft.OData.Client.SaveChangesOptions.ReplaceOnUpdate); //Query(Expand) EntitySet's Navigation Property - Singleton department = TestClientContext.Departments.Expand(d => d.Company).Where(d => d.DepartmentID == tmpDepartmentID).Single(); Assert.IsTrue(department != null); Assert.IsTrue(department.Company.CompanyID == company.CompanyID); //Delete Navigation Property - EntitySet TestClientContext.DeleteLink(company, "Departments", department); TestClientContext.SaveChanges(); //Projection with Navigation Property - EntitySet company.Departments = null; company = TestClientContext.Company.Select(c => new Company { CompanyID = c.CompanyID, Departments = c.Departments }).GetValue(); Assert.IsTrue(company != null); Assert.IsTrue(!company.Departments.Any(c => c.DepartmentID == tmpDepartmentID)); //Query Singleton's Navigation Property - Singleton company = TestClientContext.Company.Select(c => new Company { CompanyID = c.CompanyID, VipCustomer = c.VipCustomer }).GetValue(); Assert.IsTrue(company != null); Assert.IsTrue(company.VipCustomer != null); //Query Singleton again with Execute var vipCustomer = TestClientContext.Execute <Customer>(new Uri("VipCustomer", UriKind.Relative)).Single(); //Update Singleton's Navigation property - Singleton vipCustomer.LastName = "UpdatedLastName"; TestClientContext.UpdateRelatedObject(company, "VipCustomer", vipCustomer); TestClientContext.SaveChanges(); company.VipCustomer = null; company = TestClientContext.Company.Expand(c => c.VipCustomer).GetValue(); Assert.IsTrue(company != null); Assert.IsTrue(company.VipCustomer != null); Assert.IsTrue(company.VipCustomer.LastName == "UpdatedLastName"); //Update Navigation Property - Delete the Singleton navigation TestClientContext.SetLink(company, "VipCustomer", null); TestClientContext.SaveChanges(); //Expand Navigation Property - Singleton company.VipCustomer = null; company = TestClientContext.Company.Expand(c => c.VipCustomer).GetValue(); Assert.IsTrue(company != null); Assert.IsTrue(company.VipCustomer == null); //TODO: AttachTo doesn't support singleton. //TestClientContext = new InMemoryEntities(ServiceBaseUri); //TestClientContext.AttachTo("Company", company); //TestClientContext.AttachTo("VipCustomer", vipCustomer); //TestClientContext.SetLink(company, "VipCustomer", vipCustomer); //TestClientContext.SaveChanges(); //Update Navigation Property - Singleton vipCustomer = TestClientContext.VipCustomer.GetValue(); TestClientContext.SetLink(company, "VipCustomer", vipCustomer); TestClientContext.SaveChanges(); company = TestClientContext.Company.Select(c => new Company { CompanyID = c.CompanyID, VipCustomer = c.VipCustomer }).GetValue(); Assert.IsTrue(company != null); Assert.IsTrue(company.VipCustomer != null); } }