本文整理汇总了C#中DataServiceContext.SetLink方法的典型用法代码示例。如果您正苦于以下问题:C# DataServiceContext.SetLink方法的具体用法?C# DataServiceContext.SetLink怎么用?C# DataServiceContext.SetLink使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类DataServiceContext
的用法示例。
在下文中一共展示了DataServiceContext.SetLink方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ProcessForeignKeys
private void ProcessForeignKeys(DataServiceContext dataServiceContext, object dataObject, IDictionary values)
{
foreach (string key in values.Keys) {
// Check if it looks like a FK, e.g. Category.CategoryID
string[] parts = key.Split('.');
if (parts.Length != 2)
continue;
// Get the name of the entity ref property, e.g. Category
string entityRefPropertyName = parts[0];
// Get the PropertyInfo for the entity ref property
PropertyInfo propInfo = dataObject.GetType().GetProperty(entityRefPropertyName);
object entityRefObject = null;
if (values[key] != null) {
// Create an 'empty' related entity, e.g a Category
entityRefObject = Activator.CreateInstance(propInfo.PropertyType);
// Set the PK in the related entity, e.g. set the CategoryID in the Category
PropertyInfo subPropInfo = propInfo.PropertyType.GetProperty(parts[1]);
subPropInfo.SetValue(
entityRefObject,
Convert.ChangeType(values[key], subPropInfo.PropertyType),
null);
}
// Find the entity set property for the association table e.g. Categories
PropertyInfo entitySetProp = DataServiceUtilities.FindEntitySetProperty(
dataServiceContext.GetType(), propInfo.PropertyType);
// Attach the related entity and set it as the link on the main entity
if (entitySetProp != null) {
if (entityRefObject != null) {
dataServiceContext.AttachTo(entitySetProp.Name, entityRefObject);
}
dataServiceContext.SetLink(dataObject, entityRefPropertyName, entityRefObject);
}
}
}
示例2: EnsureInsert
public static void EnsureInsert(DataServiceContext context, object entity, string entitySetName, Workspace workspace, string skipEntitySet)
{
#if !ClientSKUFramework
IEdmEntityType entityType = null;
if (DataServiceMetadata.ServiceMetadata == null)
{
DataServiceMetadata.LoadServiceMetadata(workspace.ServiceUri);
}
if (DataServiceMetadata.EntityTypes.Any(eType => eType.Name == entitySetName))
{
entityType = DataServiceMetadata.EntityTypes.First(eType => eType.Name == entitySetName);
}
if (entityType == null && DataServiceMetadata.EntityTypes.Any(eType => eType.Name == entity.GetType().Name))
{
entityType = DataServiceMetadata.EntityTypes.First(eType => eType.Name == entity.GetType().Name);
entitySetName = entity.GetType().Name;
}
if (entityType == null) return;
foreach (IEdmNavigationProperty navProperty in entityType.NavigationProperties())
{
if (context.Links.All(ld => (ld.SourceProperty != navProperty.Name)))
{
if (navProperty.TargetMultiplicity() == EdmMultiplicity.One && navProperty.Name != skipEntitySet)
{
IEdmEntityType navProperyEntityType = DataServiceMetadata.GetEntityType(navProperty);
ResourceType resourceType = workspace.ServiceContainer.ResourceTypes.First(rt => rt.Name == navProperyEntityType.Name);
object instance = resourceType.CreateInstance(false);
context.AddObject(navProperyEntityType.Name == "College" ? "Colleges" : navProperyEntityType.Name, instance);
context.SetLink(entity, navProperty.Name, instance);
}
}
}
#endif
}
示例3: EFFK_1To1_BasicInsertAndBind_Batch_ChangedUriCompositionRulesOnServer
public void EFFK_1To1_BasicInsertAndBind_Batch_ChangedUriCompositionRulesOnServer()
{
// Fix URI composition in Astoria for V3 payloads
ctx = new DataServiceContext(web.ServiceRoot, Microsoft.OData.Client.ODataProtocolVersion.V4);
ctx.EnableAtom = true;
ctx.Format.UseAtom();
// Create new office type
EFFKClient.Office o = new EFFKClient.Office() { ID = 1, BuildingName = "Building 35", FloorNumber = 2, OfficeNumber = 2173 };
ctx.AddObject("CustomObjectContext.Offices", o);
// create new employee type
EFFKClient.Worker e = new EFFKClient.Worker() { ID = 1, FirstName = "Pratik", LastName = "Patel" };
ctx.AddObject("CustomObjectContext.Workers", e);
// Establish relationship between employee and office
ctx.SetLink(o, "Worker", e);
ctx.SetLink(e, "Office", o);
ctx.SaveChanges(SaveChangesOptions.BatchWithSingleChangeset);
// clean the context
ctx.DeleteObject(e);
ctx.DeleteObject(o);
ctx.SaveChanges(SaveChangesOptions.BatchWithSingleChangeset);
}
示例4: Collection_IExpandProvider
public void Collection_IExpandProvider()
{
var metadata = CreateMetadataForXFeatureEntity();
DSPServiceDefinition service = new DSPServiceDefinition()
{
Metadata = metadata,
CreateDataSource = (m) => new DSPContext(),
Writable = true,
SupportIExpandProvider = true
};
string[] responseFormats = new string[] { UnitTestsUtil.AtomFormat };
TestUtil.RunCombinations(responseFormats, (format) =>
{
using (TestWebRequest request = service.CreateForInProcessWcf())
{
request.StartService();
DataServiceContext ctx = new DataServiceContext(request.ServiceRoot, ODataProtocolVersion.V4);
ctx.EnableAtom = true;
ctx.Format.UseAtom();
PopulateClientContextWithTestEntities(ctx);
// set navigation
((XFeatureTestsEntity)ctx.Entities.Last().Entity).NextTestEntity = (XFeatureTestsEntity)ctx.Entities.First().Entity;
ctx.SetLink(ctx.Entities.Last().Entity, "NextTestEntity", ctx.Entities.First().Entity);
ctx.SaveChanges();
request.RequestUriString = "/Entities?$expand=NextTestEntity";
request.Accept = format;
request.SendRequest();
XDocument resp = UnitTestsUtil.GetResponseAsAtomXLinq(request);
Assert.IsNotNull(resp.Descendants(UnitTestsUtil.MetadataNamespace + "inline").Elements(UnitTestsUtil.AtomNamespace + "entry").FirstOrDefault(), "No expanded entities in the payload.");
foreach (XFeatureTestsEntity entity in ctx.Entities.Select(e => e.Entity))
{
var entityXElement = (from e in resp.Element(UnitTestsUtil.AtomNamespace + "feed").Elements(UnitTestsUtil.AtomNamespace + "entry")
where (int)e.Element(UnitTestsUtil.AtomNamespace + "content")
.Element(UnitTestsUtil.MetadataNamespace + "properties")
.Element(UnitTestsUtil.DataNamespace + "ID") == entity.ID
select e).Single();
ValidateResult(entity, entityXElement);
}
}
});
}