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


C# EntityManager.AttachEntity方法代码示例

本文整理汇总了C#中EntityManager.AttachEntity方法的典型用法代码示例。如果您正苦于以下问题:C# EntityManager.AttachEntity方法的具体用法?C# EntityManager.AttachEntity怎么用?C# EntityManager.AttachEntity使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在EntityManager的用法示例。


在下文中一共展示了EntityManager.AttachEntity方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: CreateFakeExistingCustomer

 // Useful utility method
 private Customer CreateFakeExistingCustomer(EntityManager entityManager, string companyName = "Existing Customer") {
     var customer = new Customer();
     customer.CompanyName = companyName;
     customer.CustomerID = Guid.NewGuid();
     entityManager.AttachEntity(customer);
     return customer;
 }
开发者ID:baotnq,项目名称:breeze.sharp.samples,代码行数:8,代码来源:NavigationTests.cs

示例2: DoesValidateOnAttachByDefault

        public async Task DoesValidateOnAttachByDefault()
        {
            var manager = new EntityManager(_serviceName); // new empty EntityManager
            await manager.FetchMetadata(); // required before creating a new entity

            var customer = new Customer();
            
            var validationErrors = customer.EntityAspect.ValidationErrors;
            Assert.IsFalse(validationErrors.Any(), "Should be no validation errors before attach.");

            // attach triggers entity validation by default
            manager.AttachEntity(customer);

            Assert.IsTrue(validationErrors.Any(ve => ve.Message.Contains("CompanyName") && ve.Message.Contains("required")), "Should be a validation error stating CompanyName is required.");
        }
开发者ID:baotnq,项目名称:breeze.sharp.samples,代码行数:15,代码来源:ValidationTests.cs

示例3: DoesNotValidateOnAttachWhenOptionIsOff

        public async Task DoesNotValidateOnAttachWhenOptionIsOff()
        {
            var manager = new EntityManager(_serviceName); // new empty EntityManager
            await manager.FetchMetadata(); // required before creating a new entity

            // change the default options, turning off "OnAttach"
            var valOpts = new ValidationOptions { ValidationApplicability = ValidationApplicability.OnPropertyChange | ValidationApplicability.OnSave };
            
            // reset manager's options
            manager.ValidationOptions = valOpts;

            var customer = new Customer();
            manager.AttachEntity(customer);

            var validationErrors = customer.EntityAspect.ValidationErrors;
            Assert.IsFalse(validationErrors.Any(), "Should be no validation errors even though CompanyName is required.");
        }
开发者ID:baotnq,项目名称:breeze.sharp.samples,代码行数:17,代码来源:ValidationTests.cs

示例4: ManualValidationAndClearingOfErrors

        public async Task ManualValidationAndClearingOfErrors()
        {
            var manager = new EntityManager(_serviceName); // new empty EntityManager
            await manager.FetchMetadata(); // required before creating a new entity

            var newCustomer = new Customer();
            // attach triggers entity validation by default
            manager.AttachEntity(newCustomer);

            // validate an individual entity at any time
            var results = newCustomer.EntityAspect.Validate();
            if (results.Any()) {/* do something about errors */}
            Assert.IsTrue(results.Any(ve => ve.Message.Contains("CompanyName") && ve.Message.Contains("required")), "Should be a validation error stating CompanyName is required.");

            // remove all current errors from the collection
            newCustomer.EntityAspect.ValidationErrors.Clear();
            Assert.IsFalse(newCustomer.EntityAspect.ValidationErrors.Any(), "Should be no validation errors after clearing them.");

            // validate a specific property at any time
            var dp = newCustomer.EntityAspect.EntityType.GetDataProperty("CompanyName");
            results = newCustomer.EntityAspect.ValidateProperty(dp);
            if (results.Any()) { /* do something about errors */}
            Assert.IsTrue(results.Any(ve => ve.Message.Contains("CompanyName") && ve.Message.Contains("required")), "Should be a validation error stating CompanyName is required.");

            // remove a specific error
            var specificError = results.First(ve => ve.Context.PropertyPath == "CompanyName");
            newCustomer.EntityAspect.ValidationErrors.Remove(specificError);
            Assert.IsFalse(newCustomer.EntityAspect.ValidationErrors.Any(), "Should be no validation errors after clearing a specific one.");

            // clear server errors
            var errors = newCustomer.EntityAspect.ValidationErrors;
            errors.ForEach(ve =>
                               {
                                   if (ve.IsServerError) errors.Remove(ve);
                               });

        }
开发者ID:baotnq,项目名称:breeze.sharp.samples,代码行数:37,代码来源:ValidationTests.cs

示例5: EmployeeMustHaveValidUSPhone

        public async Task EmployeeMustHaveValidUSPhone()
        {
            var manager = new EntityManager(_serviceName); // new empty EntityManager
            await manager.FetchMetadata(); // required before creating a new entity

            var employeeType = manager.MetadataStore.GetEntityType(typeof(Employee)); //get the Employee type

            var phoneValidator = new PhoneNumberValidator();
            // or the hard way ... :)
            // var phoneValidator = new RegexValidator(@"^((\([2-9]\d{2}\) ?)|([2-9]\d{2}[-.]))\d{3}[-.]\d{4}$", "phone number"); // email pattern
            try
            {
                // add regex validator that validates emails to the HomePhone property
                employeeType.GetProperty("HomePhone").Validators.Add(phoneValidator); 

                var employee = new Employee { FirstName = "Jane", LastName = "Doe" };
                employee.HomePhone = "N2L 3G1"; // a bad phone number

                // force validation of unattached employee
                manager.AttachEntity(employee);
                var errors = employee.EntityAspect.ValidationErrors;

                Assert.IsTrue(errors.Any(), String.Format("should have 1 error: {0}", errors.First().Message));

                employee.HomePhone = "510-555-1111";

                Assert.IsFalse(errors.Any(), String.Format("should have no errors"));
            }
            finally
            {
                Assert.IsTrue(employeeType.GetProperty("HomePhone").Validators.Remove(phoneValidator));
            }
        }
开发者ID:baotnq,项目名称:breeze.sharp.samples,代码行数:33,代码来源:ValidationTests.cs

示例6: AddRemoveValidationError

        public async Task AddRemoveValidationError()
        {
            var manager = new EntityManager(_serviceName); // new empty EntityManager
            await manager.FetchMetadata(); // required before creating a new entity

            var customer = new Customer { CompanyName = "Presumed Guilty"};
            manager.AttachEntity(customer);

            var errmsgs = customer.EntityAspect.ValidationErrors;
            Assert.IsFalse(errmsgs.Any(), "should have no errors at start");

            // create a fake error
            var fakeError = new ValidationError( 
                new AlwaysWrongValidator(),     // the marker validator
                new ValidationContext(customer),// validation context
                "You were wrong this time!"     // error message
            );

            // add the fake error
            errmsgs.Add(fakeError);

            Assert.IsTrue(errmsgs.Any(), String.Format("should have 1 error after add: {0}", errmsgs.First().Message));               

            // Now remove that error
            errmsgs.Remove(fakeError);

            customer.EntityAspect.Validate(); // re-validate

            Assert.IsFalse(errmsgs.Any(), "should have no errors after remove");
        }
开发者ID:baotnq,项目名称:breeze.sharp.samples,代码行数:30,代码来源:ValidationTests.cs

示例7: RemoveRuleAndError

        public async Task RemoveRuleAndError()
        {
            var manager = new EntityManager(_serviceName); // new empty EntityManager
            await manager.FetchMetadata(); // required before creating a new entity

            var customerType = manager.MetadataStore.GetEntityType(typeof(Customer)); //get the Customer type
            
            var alwaysWrongValidator = new AlwaysWrongValidator();
            var validators = customerType.Validators;

            try
            {
                // add alwaysWrong to the entity (not to a property)
                validators.Add(alwaysWrongValidator);

                var customer = new Customer { CompanyName = "Presumed Guilty"};

                // Attach triggers entity validation by default
                manager.AttachEntity(customer);

                var errmsgs = customer.EntityAspect.ValidationErrors;
                Assert.IsTrue(errmsgs.Any(), String.Format("should have 1 error: {0}", errmsgs.First().Message));

                // remove validation rule
                Assert.IsTrue(validators.Remove(alwaysWrongValidator));

                // Clear out the "alwaysWrong" error
                // Must do manually because that rule is now gone
                // and, therefore, can't cleanup after itself
                customer.EntityAspect.ValidationErrors.RemoveKey(ValidationError.GetKey(alwaysWrongValidator));

                customer.EntityAspect.Validate(); // re-validate

                Assert.IsFalse(errmsgs.Any(), "should have no errors");
            }
            finally
            {
                validators.Remove(alwaysWrongValidator);
            }
        }
开发者ID:baotnq,项目名称:breeze.sharp.samples,代码行数:40,代码来源:ValidationTests.cs

示例8: EmployeeMustBeFromUs

        public async Task EmployeeMustBeFromUs()
        {
            var manager = new EntityManager(_serviceName); // new empty EntityManager
            await manager.FetchMetadata(); // required before creating a new entity

            var employee = new Employee { FirstName = "Bill", LastName = "Gates" };
            manager.AttachEntity(employee);

            try
            {
                // add the US-only validator
                employee.EntityAspect.EntityType.GetProperty("Country")
                        .Validators.Add(new CountryIsUsValidator());

                // non-US customers no longer allowed 
                employee.Country = "Canada";
                var validationErrors = employee.EntityAspect.ValidationErrors;
                Assert.IsTrue(validationErrors.Any(ve => ve.Message.Contains("Country must start with 'US'")), "Should be a validation error stating Country must start with 'US'.");

                // back in the USA 
                employee.Country = "USA";
                Assert.IsFalse(validationErrors.Any(), "Should be no validation errors.");
            }
            finally
            {
                Assert.IsTrue(employee.EntityAspect.EntityType.GetProperty("Country")
                        .Validators.Remove(new CountryIsUsValidator()));                
            }
        }
开发者ID:baotnq,项目名称:breeze.sharp.samples,代码行数:29,代码来源:ValidationTests.cs

示例9: CustomerMustBeInUs

        public async Task CustomerMustBeInUs()
        {
            var manager = new EntityManager(_serviceName); // new empty EntityManager
            await manager.FetchMetadata(); // required before creating a new entity

            var customer = new Customer { CompanyName = "zzz", ContactName = "Shania Twain" };
            manager.AttachEntity(customer);

            try
            {
                // add the US-only validator
                customer.EntityAspect.EntityType.GetProperty("Country")
                        .Validators.Add(new CountryIsUsValidator());

                // non-US customers no longer allowed 
                customer.Country = "CA";
                var validationErrors = customer.EntityAspect.ValidationErrors;
                Assert.IsTrue(validationErrors.Any(ve => ve.Message.Contains("Country must start with 'US'")), "Should be a validation error stating Country must start with 'US'.");

                // back in the USA 
                customer.Country = "USA";
                Assert.IsFalse(validationErrors.Any(), "Should be no validation errors.");
            }
            finally
            {
                Assert.IsTrue(customer.EntityAspect.EntityType.GetProperty("Country")
                        .Validators.Remove(new CountryIsUsValidator()));
            }
        }
开发者ID:baotnq,项目名称:breeze.sharp.samples,代码行数:29,代码来源:ValidationTests.cs

示例10: AddRequiredValidator

        public async Task AddRequiredValidator()
        {
            var manager = new EntityManager(_serviceName); // new empty EntityManager
            await manager.FetchMetadata(); // required before creating a new entity

            // Add required validator to the Country property of a Customer entity
            var customerType = manager.MetadataStore.GetEntityType(typeof(Customer)); //get the Customer type
            var countryProp = customerType.GetProperty("Country"); //get the property definition to validate

            var validators = countryProp.Validators; // get the property's validator collection
            Assert.IsFalse(validators.Any(v => v == new RequiredValidator()), "Should be no required validators on Customer.Country.");

            try
            {
                validators.Add(new RequiredValidator()); // add a new required validator instance 
                Assert.IsTrue(validators.Any(), "Should now have a required validator on Customer.Country.");

                // create a new customer setting the required CompanyName
                var customer = new Customer { CompanyName = "zzz" };
                var validationErrors = customer.EntityAspect.ValidationErrors;
                Assert.IsFalse(validationErrors.Any(), "Should be no validation errors prior to attach");

                // attach triggers entity validation by default
                manager.AttachEntity(customer);
                // customer is no longer valid since Country was never set
                Assert.IsTrue(validationErrors.Any(ve => ve.Message.Contains("Country") && ve.Message.Contains("required")), "Should be a validation error stating Country is required.");
            }
            finally
            {
                Assert.IsTrue(validators.Remove(new RequiredValidator()));
            }
        }
开发者ID:baotnq,项目名称:breeze.sharp.samples,代码行数:32,代码来源:ValidationTests.cs

示例11: ErrorOnAttachMultiple

 public async Task ErrorOnAttachMultiple() {
   await _emTask;
 
   var order = _em1.CreateEntity<Order>(EntityState.Unchanged);
   var em2 = new EntityManager(_em1);
   try {
     em2.AttachEntity(order);
     Assert.Fail("should not get here");
   } catch (Exception e) {
     Assert.IsTrue(e.Message.Contains("EntityManager"), "message should mention 'EntityManager'");
   }
 }
开发者ID:udomsak,项目名称:Breeze,代码行数:12,代码来源:AttachTests.cs

示例12: AttachEmViaDetach

    public async Task AttachEmViaDetach() {
      await _emTask;

        var cust = new Customer();
        cust.EntityAspect.SetValue(TestFns.CustomerKeyName, Guid.NewGuid());
        Assert.IsTrue(cust.EntityAspect.IsDetached, "should be detached");
        _em1.AttachEntity(cust);
        Assert.IsTrue(cust.EntityAspect.IsAttached, "should be attached");
        _em1.Clear(); // will detach cust

        Assert.IsTrue(cust.EntityAspect.IsDetached, "should be detached - again");
        Assert.IsTrue(cust.EntityAspect.EntityManager == _em1, "should still be associated with em1");
        // therefore this should be ok
        var em2 = new EntityManager(_em1);
        em2.AttachEntity(cust);
        Assert.IsTrue(cust.EntityAspect.EntityManager == em2, "should be on em2");
    }
开发者ID:udomsak,项目名称:Breeze,代码行数:17,代码来源:AttachTests.cs

示例13: AttachToDifferentManager

    public async Task AttachToDifferentManager() {
      await _emTask;

      var cust = _em1.CreateEntity<Customer>(EntityState.Unchanged);
      var order = _em1.CreateEntity<Order>(EntityState.Unchanged);
      cust.Orders.Add(order);
      Assert.IsTrue(cust.Orders.Count == 1);
      var em2 = new EntityManager(_em1);
      try {
        em2.AttachEntity(cust);
        Assert.Fail("should not get here");
      } catch {
        // expected
      }

      _em1.DetachEntity(cust);
      Assert.IsTrue(order.Customer == null);
      Assert.IsTrue(cust.Orders.Count == 0);
      em2.AttachEntity(cust);
      Assert.IsTrue(cust.EntityAspect.EntityManager == em2);

    }
开发者ID:udomsak,项目名称:Breeze,代码行数:22,代码来源:AttachTests.cs

示例14: ErrorOnAttachMultiple

 public async Task ErrorOnAttachMultiple() {
   var em1 = await TestFns.NewEm(_serviceName);
 
   var order = em1.CreateEntity<Order>(null, EntityState.Unchanged);
   var em2 = new EntityManager(em1);
   try {
     em2.AttachEntity(order);
     Assert.Fail("should not get here");
   } catch (Exception e) {
     Assert.IsTrue(e.Message.Contains("EntityManager"), "message should mention 'EntityManager'");
   }
 }
开发者ID:hiddenboox,项目名称:breeze.sharp,代码行数:12,代码来源:AttachTests.cs

示例15: AttachToDifferentManager

    public async Task AttachToDifferentManager() {
      var em1 = await TestFns.NewEm(_serviceName);

      var cust = em1.CreateEntity<Customer>(null, EntityState.Unchanged);
      var order = em1.CreateEntity<Order>(entityState: EntityState.Unchanged);
      cust.Orders.Add(order);
      Assert.IsTrue(cust.Orders.Count == 1);
      var em2 = new EntityManager(em1);
      try {
        em2.AttachEntity(cust);
        Assert.Fail("should not get here");
      } catch {
        // expected
      }

      em1.DetachEntity(cust);
      Assert.IsTrue(order.Customer == null);
      Assert.IsTrue(cust.Orders.Count == 0);
      em2.AttachEntity(cust);
      Assert.IsTrue(cust.EntityAspect.EntityManager == em2);

    }
开发者ID:hiddenboox,项目名称:breeze.sharp,代码行数:22,代码来源:AttachTests.cs


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