Example #1
0
        public ActionResult CreateNewTestModelEntity(FormCollection formCollection)
        //public ActionResult CreateNewTestModelEntity(string Name, string Location)
        {
            //foreach(string key in formCollection.AllKeys)
            //{
            //    Response.Write("Key [" + key + "]");
            //    Response.Write(formCollection[key]);
            //    Response.Write("<br />");
            //}
            //return View();

            var newTestModel = new TestModel();

            // newTestModel.Name = formCollection["Name"];
            // newTestModel.Location = formCollection["Location"];

            // newTestModel.Name = Name;
            // newTestModel.Location = Location;

            TryUpdateModel(newTestModel, formCollection);

            var db = new TestModelContext();

            db.TestModels.Add(newTestModel);
            db.SaveChanges();

            return(RedirectToAction("TestModelEntityGetAll2"));
        }
Example #2
0
        static void Main(string[] args)
        {
            var options   = SqliteInMemory.CreateOptions <TestModelContext>();
            var dbContext = new TestModelContext(options);


            dbContext.Database.EnsureCreated();

            //create book with one page and one image ( image has navigation props to book and bookpage)
            var book  = CreateBasicBook();
            var page  = CreateBasicBookPage(book);
            var image = CreateBasicImage(book, page);

            dbContext.Add(book);
            dbContext.Add(page);
            dbContext.Add(image);
            dbContext.SaveChanges();

            //take existing book
            var bookToCopy = dbContext.Books.Include(b => b.BookPages).Include(b => b.Images).First() as Book;

            //reset Connection to Database
            dbContext.Dispose();
            dbContext = new TestModelContext(options);

            // set keyproperties to zero
            bookToCopy.Id = 0;
            //set keyproperties of children to zero
            foreach (var i in bookToCopy.Images)
            {
                i.Id = 0;
            }
            foreach (var p in bookToCopy.BookPages)
            {
                p.Id = 0;
            }

            //add to context
            dbContext.Add(bookToCopy);
            //save context
            dbContext.SaveChanges();

            //reset Connection to Database changes everything
            //dbContext.Dispose();
            //dbContext = new TestModelContext(options);

            //output
            var allBooks = dbContext.Books.Include(b => b.BookPages).Include(b => b.Images).ToList();

            Console.WriteLine("Initial Book: " + JsonConvert.SerializeObject(allBooks[0], new JsonSerializerSettings
            {
                ReferenceLoopHandling = ReferenceLoopHandling.Ignore
            }));
            Console.WriteLine("Copied Book: " + JsonConvert.SerializeObject(allBooks[1], new JsonSerializerSettings
            {
                ReferenceLoopHandling = ReferenceLoopHandling.Ignore
            }));
        }
Example #3
0
        public ActionResult DeleteTestModelEntity(int Id, FormCollection formCollection)
        {
            var db = new TestModelContext();

            var model = db.TestModels.FirstOrDefault(x => x.Id == Id);

            db.TestModels.Remove(model);
            db.SaveChanges();

            return(RedirectToAction("TestModelEntityGetAll2"));
        }
Example #4
0
        public ActionResult EditTestModelEntity(int Id, FormCollection formCollection)
        {
            var db = new TestModelContext();

            var model = db.TestModels.FirstOrDefault(x => x.Id == Id);

            TryUpdateModel(model, formCollection);
            db.SaveChanges();

            return(RedirectToAction("TestModelEntityGetAll2"));
        }
Example #5
0
        private static void LoadMockData(TestModelContext context)
        {
            context.Parent.Add(new ParentModel()
            {
                Key = 1, Name = "1"
            });
            context.Parent.Add(new ParentModel()
            {
                Key = 2, Name = "2"
            });
            context.Parent.Add(new ParentModel()
            {
                Key = 3, Name = "3"
            });
            context.Parent.Add(new ParentModel()
            {
                Key = 4, Name = "4"
            });

            context.Children.Add(new ChildModel()
            {
                Key = 1, ParentKey = 1, Display = "1.1"
            });
            context.Children.Add(new ChildModel()
            {
                Key = 2, ParentKey = 1, Display = "2.1"
            });
            context.Children.Add(new ChildModel()
            {
                Key = 3, ParentKey = 2, Display = "3.2"
            });
            context.Children.Add(new ChildModel()
            {
                Key = 4, ParentKey = 4, Display = "4.4"
            });
            context.Children.Add(new ChildModel()
            {
                Key = 5, ParentKey = 3, Display = "5.3"
            });

            context.SaveChanges();
        }
Example #6
0
        [HttpDelete] // Can use custom method name then.
        public HttpResponseMessage Delete(int id)
        {
            // data.RemoveAt(id);


            //var db = new TestModelContext();

            //var oldTestModel = db.TestModels.Where(x => x.Id == id).FirstOrDefault();

            //db.TestModels.Remove(oldTestModel);
            //db.SaveChanges();


            HttpResponseMessage result;

            try
            {
                var db = new TestModelContext();

                var oldTestModel = db.TestModels.Where(x => x.Id == id).FirstOrDefault();

                if (oldTestModel != null)
                {
                    db.TestModels.Remove(oldTestModel);
                    db.SaveChanges();

                    result = Request.CreateResponse(HttpStatusCode.OK);
                }
                else
                {
                    result = Request.CreateErrorResponse(HttpStatusCode.NotFound, "TestModel id = [" + id.ToString() + "] was not found.");
                }
            }
            catch (Exception exception)
            {
                result = Request.CreateErrorResponse(HttpStatusCode.BadRequest, exception);
            }

            return(result);
        }
Example #7
0
        [HttpPut] // Can use custom method name then.
        // public HttpResponseMessage Put(int id, [FromBody] TestModel testModel)
        // public HttpResponseMessage Put([FromBody] int id, [FromUri] TestModel testModel)
        public HttpResponseMessage Put([FromUri] int id, [FromUri] TestModel testModel)
        {
            // data[id] = value;


            //var db = new TestModelContext();

            //var oldTestModel = db.TestModels.Where(x => x.Id == id).FirstOrDefault();

            //oldTestModel.Name = testModel.Name;
            //oldTestModel.Location = testModel.Location;

            //db.SaveChanges();


            HttpResponseMessage result;

            var db = new TestModelContext();

            var updateTestModel = db.TestModels.Where(x => x.Id == id).FirstOrDefault();

            if (updateTestModel != null)
            {
                updateTestModel.Name     = testModel.Name;
                updateTestModel.Location = testModel.Location;

                db.SaveChanges();

                result = Request.CreateResponse(HttpStatusCode.OK, updateTestModel);
                result.Headers.Location = new Uri(Request.RequestUri.ToString());
            }
            else
            {
                result = Request.CreateErrorResponse(HttpStatusCode.NotFound, "TestModel id = [" + id.ToString() + "] was not found.");
            }

            return(result);
        }
Example #8
0
        [HttpPost] // Can use custom method name then.
        public HttpResponseMessage Post([FromBody] TestModel testModel)
        {
            // data.Add(value);


            HttpResponseMessage result;

            try
            {
                var db = new TestModelContext();

                db.TestModels.Add(testModel);
                db.SaveChanges();

                result = Request.CreateResponse(HttpStatusCode.Created, testModel);
                result.Headers.Location = new Uri(Request.RequestUri + "/" + testModel.Id.ToString());
            }
            catch (Exception exception)
            {
                result = Request.CreateErrorResponse(HttpStatusCode.BadRequest, exception);
            }

            return(result);
        }
Example #9
0
        public static Guid CreateFullDbHierarchy()
        {
            Guid projectId;

            using (var db = new TestModelContext())
            {
                var newProject = new Project
                {
                    Id          = Guid.NewGuid(),
                    Name        = "ProjectName",
                    Description = "ProjectDescription"
                };
                db.Projects.Add(newProject);
                projectId = newProject.Id;

                var newCredentialType = new CredentialType
                {
                    Id             = Guid.NewGuid(),
                    Name           = "CredType",
                    RequiredValues = "None"
                };
                db.CredentialTypes.Add(newCredentialType);

                var newCredentialsDefinition = new CredentialDefinition
                {
                    Id          = Guid.NewGuid(),
                    Name        = "Creds",
                    DisplayName = "Creds",
                    Project     = newProject,
                    Type        = newCredentialType,
                };
                db.CredentialDefinitions.Add(newCredentialsDefinition);
                newProject.CredentialDefinitions.Add(newCredentialsDefinition);

                var newEnvironment = new Models.Environment
                {
                    Id      = Guid.NewGuid(),
                    Name    = "EnvironmentName",
                    Project = newProject,
                    BaseUrl = "https://skywardapps.us"
                };
                db.Environments.Add(newEnvironment);
                newProject.Environments.Add(newEnvironment);

                var newCredentials = new Credential
                {
                    Id          = Guid.NewGuid(),
                    Environment = newEnvironment,
                    Definition  = newCredentialsDefinition,
                    Values      = new List <CredentialKeyValue> {
                        new CredentialKeyValue
                        {
                            Id    = Guid.NewGuid(),
                            Key   = "Key",
                            Value = "Value",
                        }
                    }
                };
                db.Credentials.Add(newCredentials);
                newEnvironment.Credentials.Add(newCredentials);

                db.SaveChanges();
            }

            return(projectId);
        }