当前位置: 首页>>代码示例>>C#>>正文


C# DataServiceContext.SetLink方法代码示例

本文整理汇总了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);
                }
            }
        }
开发者ID:davidebbo-test,项目名称:DynamicDataWCFDataService,代码行数:41,代码来源:DataServiceLinqDataSource.cs

示例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
        }
开发者ID:larsenjo,项目名称:odata.net,代码行数:37,代码来源:UpdateModel.cs

示例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);
        }
开发者ID:larsenjo,项目名称:odata.net,代码行数:24,代码来源:Dev10EF4FKTests.cs

示例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);
                        }
                    }
                });
            }
开发者ID:AlineGuan,项目名称:odata.net,代码行数:50,代码来源:CollectionCrossFeature.cs


注:本文中的DataServiceContext.SetLink方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。