示例#1
0
 public IActionResult AddNewPerson(Person personIn)
 {
     ViewData["Message"] = "You Have added :";
     context.Add(personIn);
     context.SaveChanges();
     return(View(personIn));
 }
示例#2
0
        public async Task <CustomerDetailItem> CreateCustomerAsync([FromBody] CustomerPayloadItem payload)
        {
            var dbItem = new Customer
            {
                Id        = Guid.NewGuid(),
                City      = payload.City,
                CountryId = payload.Country.Id,
                FirstName = payload.FirstName,
                LastName  = payload.LastName,
                Phone     = payload.Phone,
                Street    = payload.Street,
                ZipCode   = payload.ZipCode,
                Created   = DateTime.UtcNow,
            };

            _db.Add(dbItem);

            var entry = _db.Entry(dbItem);

            entry.UpdateAdditionalProperties(payload);

            await _db.SaveChangesAsync();

            return(await GetCustomerDetailItemAsync(dbItem.Id));
        }
示例#3
0
        public async Task <IActionResult> Create([Bind("FullName,Phone,Email,Company,Status,Id,DateCreated,CreatedBy")] Customer customer)
        {
            if (ModelState.IsValid)
            {
                _context.Add(customer);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(customer));
        }
示例#4
0
        public async Task <IActionResult> Create([Bind("Description,IsBinary,Id,DateCreated,CreatedBy")] Question question)
        {
            if (ModelState.IsValid)
            {
                _context.Add(question);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(question));
        }
示例#5
0
        public async Task <CountryInfo> CreateCountryAsync([FromBody] CountryPayload payload)
        {
            var dbItem = new Country
            {
                Id   = Guid.NewGuid(),
                Name = payload.Name,
                Iso3 = payload.Iso3,
            };

            _db.Add(dbItem);

            await _db.SaveChangesAsync();

            return(await GetCountryInfoByIdAsync(dbItem.Id));
        }
        public async Task AddPropertyAsync(string entityName,
                                           [FromQuery] string propertyName,
                                           [FromQuery] PropertyType propertyType,
                                           [FromQuery] bool isNullable)
        {
            var dbEntiy = _db.Model.FindEntityType(entityName);

            MetadataModel model = null;

            var customization = await _db.Customizations.OrderByDescending(p => p.Id).FirstOrDefaultAsync();

            if (customization != null)
            {
                var entities = JsonSerializer.Deserialize <IEnumerable <EntityMetadata> >(customization.Metadata);

                model = new MetadataModel(customization.Id, entities);
            }
            else
            {
                model = new MetadataModel(0, new EntityMetadata[] { });
            }

            EntityMetadata entity = model.Entities.FirstOrDefault(p => p.EntityName == entityName);

            if (entity == null)
            {
                entity = new EntityMetadata(entityName, new PropertyMetadata[] { });
                model.Entities.Add(entity);
            }

            var prop = new PropertyMetadata(propertyName, isNullable, propertyType);

            entity.Properties.Add(prop);

            var addColumn = new AddColumnOperation
            {
                Name       = propertyName,
                ClrType    = CrmContext.GetClrType(prop),
                IsNullable = prop.IsNullable,
                Table      = dbEntiy.GetTableName(),
                Schema     = dbEntiy.GetSchema(),
            };

            var generator = ((IInfrastructure <IServiceProvider>)_db).Instance.GetRequiredService <IMigrationsSqlGenerator>();

            var scripts = generator.Generate(new[] { addColumn });

            await using (var transaction = await _db.Database.BeginTransactionAsync())
            {
                foreach (var script in scripts)
                {
                    await _db.Database.ExecuteSqlRawAsync(script.CommandText);
                }

                var dbCustomization = new Customization {
                    Created = DateTime.UtcNow, Metadata = JsonSerializer.Serialize(model.Entities)
                };
                _db.Add(dbCustomization);

                await _db.SaveChangesAsync();

                var newId = dbCustomization.Id;

                var newModel = new MetadataModel(newId, model.Entities);

                await transaction.CommitAsync();

                _setter.SetModel(newModel);
            }
        }