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


C# EntityManager.AddEntity方法代码示例

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


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

示例1: CreatingEntities

        public async Task CreatingEntities()
        {
            var manager = new EntityManager(_serviceName);

            // Metadata must be fetched before CreateEntity() can be called
            await manager.FetchMetadata();

            //Snippet1
            var newCustomer = new Customer(); // rarely done in Breeze

            //Snippet2
            // Order uses an auto-generated key value
            var order = manager.CreateEntity<Order>();

            //Snippet3
            // If the key is not auto generated, it must be initialized by CreateEntity()
            var alpha = manager.CreateEntity<Customer>(new { CustomerID = Guid.NewGuid(), CompanyName = "Alpha" });

            //Snippet4
            // Unattached new customer so you can keep configuring it and add/attach it later
            // Key value initializer not required because new entity is not attached to entity manager
            var beta = manager.CreateEntity<Customer>(new { CompanyName = "Beta" }, EntityState.Detached);

            // Attached customer, as if retrieved from the database
            // Note that the key must be initialized when new entity will be in an attached state
            var gamma = manager.CreateEntity<Customer>(new { CustomerID = Guid.NewGuid(), CompanyName = "Gamma" }, EntityState.Unchanged);

            //Snippet5
            // Only need to do this once
            var metadataStore = manager.MetadataStore;                           // The model metadata known to this EntityManager instance
            var customerType = metadataStore.GetEntityType(typeof(Customer));    // Metadata about the Customer type

            // Do this for each customer to be created
            var acme = customerType.CreateEntity() as Customer;     // Returns Customer as IEntity
            acme.CompanyName = "Acme";                              // CompanyName is a required field
            acme.CustomerID = Guid.NewGuid();                       // Must set the key field before attaching to entity manager
            manager.AddEntity(acme);                                // Attach the entity as a new entity; it's EntityState is "Added"
        }
开发者ID:baotnq,项目名称:breeze.sharp.samples,代码行数:38,代码来源:CreateEntityTests.cs

示例2: USCustomerMustHaveValidZipCode

        public async Task USCustomerMustHaveValidZipCode()
        {
            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

            try
            {
                // add US zip code validator to the entity (not to a property)
                customerType.Validators.Add(new ZipCodeValidator());

                var customer = new Customer { CustomerID = Guid.NewGuid(), CompanyName = "Boogaloo Board Games" };
                customer.Country = "USA";
                customer.PostalCode = "N2L 3G1"; // a Canadian postal code
                manager.AddEntity(customer);
                // force validation of  customer
                var errors = customer.EntityAspect.Validate();

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

                customer.Country = "Canada";

                errors = customer.EntityAspect.Validate();

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

示例3: CreateParentAndChildren

 private TwoCusts CreateParentAndChildren(EntityManager em) {
   var cust1 = new Customer();
   cust1.CompanyName = "Test1_" + TestFns.RandomSuffix(8);
   cust1.City = "Oakland";
   cust1.RowVersion = 13;
   cust1.Fax = "510 999-9999";
   em.AddEntity(cust1);
   var cust2 = em.CreateEntity<Customer>();
   cust2.CompanyName = "Test2_" + TestFns.RandomSuffix(8);
   cust2.City = "Emeryville";
   cust2.RowVersion = 1;
   cust2.Fax = "510 888-8888";
   var order1 = new Order();
   order1.OrderDate = DateTime.Today;
   var order2 = em.CreateEntity<Order>();
   order1.OrderDate = DateTime.Today;
   cust1.Orders.Add(order1);
   cust1.Orders.Add(order2);
   Assert.IsTrue(cust1.Orders.Count == 2);
   Assert.IsTrue(cust2.Orders.Count == 0);
   return new TwoCusts() { Cust1 = cust1, Cust2 = cust2 };
 }
开发者ID:Cosmin-Parvulescu,项目名称:Breeze,代码行数:22,代码来源:SaveTests.cs

示例4: CreateOrderDetail

 private OrderDetail CreateOrderDetail(EntityManager em, Order order, Product product) {
   var od = new OrderDetail();
   var orderID = order.OrderID;
   var productID = product.ProductID;
   od.ProductID = productID;
   od.OrderID = orderID;
   od.Quantity = 1;
   od.UnitPrice = 3.14m;
   em.AddEntity(od);
   return od;
 }
开发者ID:Cosmin-Parvulescu,项目名称:Breeze,代码行数:11,代码来源:SaveTests.cs

示例5: CreateProduct

 private Product CreateProduct(EntityManager em) {
   var product = new Product();
   em.AddEntity(product);
   product.ProductName = "Test.NET_" + TestFns.RandomSuffix(7);
   return product;
 }
开发者ID:Cosmin-Parvulescu,项目名称:Breeze,代码行数:6,代码来源:SaveTests.cs

示例6: CreateOrder


//.........这里部分代码省略.........

    //    var em = newEm();
    //    var zzz = createParentAndChildren(em);
    //    var cust1 = zzz.cust1;
    //    cust1.setProperty("companyName", "error");
    //    stop();
    //    em.saveChanges().then(function(sr) {
    //        ok(false, "should not get here");
    //    }).fail(function (e) {
    //        ok(e.entityErrors, "should be a server error");
    //        ok(e.entityErrors.length === 1, "should be only one server error");
    //        var errors = cust1.entityAspect.getValidationErrors();
    //        ok(errors[0].errorMessage === e.entityErrors[0].errorMessage, "error message should appear on the cust");
    //    }).fin(start);
    //});

    //test("save with server side entity level validation error + repeat", function () {
    //    if (testFns.DEBUG_ODATA) {
    //        ok(true, "Skipped test - OData does not support server interception or alt resources");
    //        return;
    //    };

    //    if (testFns.DEBUG_MONGO) {
    //        ok(true, "Skipped test - Mongo does not YET support server side validation");
    //        return;
    //    };

    //    var em = newEm();
    //    var zzz = createParentAndChildren(em);
    //    var cust1 = zzz.cust1;
    //    cust1.setProperty("companyName", "error");
    //    stop();
    //    em.saveChanges().then(function (sr) {
    //        ok(false, "should not get here");
    //    }).fail(function (e) {
    //        ok(e.entityErrors, "should be a server error");
    //        ok(e.entityErrors.length === 1, "should be only one server error");
    //        var errors = cust1.entityAspect.getValidationErrors();
    //        ok(errors.length === 1, "should only be 1 error");
    //        ok(errors[0].errorMessage === e.entityErrors[0].errorMessage, "error message should appear on the cust");
    //        return em.saveChanges();
    //    }).fail(function(e2) {
    //        ok(e2.entityErrors, "should be a server error");
    //        ok(e2.entityErrors.length === 1, "should be only one server error");
    //       var errors = cust1.entityAspect.getValidationErrors();
    //       ok(errors.length === 1, "should only be 1 error");
    //       ok(errors[0].errorMessage === e2.entityErrors[0].errorMessage, "error message should appear on the cust");
    //    }).fin(start);
    //});
 
    //test("add UserRole", function () {
    //    if (testFns.DEBUG_MONGO) {
    //        ok(true, "TODO for Mongo - needs to be written specifically for Mongo - should succeed in Mongo");
    //        return;
    //    }

    //    var em = newEm();
    //    var roleId;
    //    var userId = 6;
    //    var p2 = breeze.Predicate.create("userId", "ne", userId);
    //    var p1 = breeze.Predicate.create("userRoles", "all", p2);

    //    var q = new EntityQuery("Roles").where(p1).take(1);
    //    stop();
    //    q.using(em).execute().then(function (data) {
    //        ok(data.results.length === 1, "should be one result");
    //        var role = data.results[0];
    //        roleId = role.getProperty("id");

    //        var newUserRole = em.createEntity('UserRole', {
    //            userId: userId,
    //            roleId: roleId
    //        });

    //        return em.saveChanges();
    //    }).then(function (sr) {
    //        ok(true, "save succeeded");
    //        var resultRole = sr.entities[0];
    //        var roleId2 = resultRole.getProperty("roleId");
    //        ok(roleId2 === roleId, "roleIds match");
    //        var userId2 = resultRole.getProperty("userId");
    //        ok(userId2 === userId, "userIds match");
            
    //        // delete entity
    //        resultRole.entityAspect.setDeleted();
    //        return em.saveChanges();
    //    }).then(function (sr) {
    //        ok(true, "delete succeeded");
    //    }).fail(function (e) {
    //        ok(false, "error on save: " + e.message);
    //    }).fail(testFns.handleFail).fin(start);

    //});

    private Order CreateOrder(EntityManager em) {
      var order = new Order();
      em.AddEntity(order);
      order.ShipName = "Test.NET_" + TestFns.RandomSuffix(7);
      return order;
    }
开发者ID:Cosmin-Parvulescu,项目名称:Breeze,代码行数:101,代码来源:SaveTests.cs

示例7: CreateProduct

 private Product CreateProduct(EntityManager em) {
   var product = new Product();
   em.AddEntity(product);
   product.ProductName = "Test.NET_" + TestFns.RandomSuffix(7);
   // only needed because of NH bug where we made 'rowVersion' nullable on the client.
   product.RowVersion = 0;
   return product;
 }
开发者ID:hiddenboox,项目名称:breeze.sharp,代码行数:8,代码来源:SaveTests.cs


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