Esempio n. 1
0
        public bool CreateInsurance(Insurance insurance)
        {
            bool success;

            try
            {
                context.Insurances.Add(insurance);
                context.SaveChanges();
                success = true;
            }
            catch
            {
                success = false;
            }
            return(success);
        }
        /// <summary>
        /// Initializes the context.
        /// </summary>
        private void InitContext()
        {
            var builder = new DbContextOptionsBuilder <InsuranceCompanyContext>().UseInMemoryDatabase();

            var context = new InsuranceCompanyContext(builder.Options);

            context.Database.EnsureDeleted();

            var risks = Enumerable.Range(1, 10)
                        .Select(i => new Risk()
            {
                Name = $"Risk_{i}", YearlyPrice = i / 10m
            }).ToList();

            context.Risks.AddRange(risks);

            var policies = Enumerable.Range(1, 10)
                           .Select(i => new Policy
            {
                NameOfInsuredObject = $"Policy_{i}",
                Premium             = i / 100m,
                ValidFrom           = new DateTime(2019, 1, 1),
                ValidTill           = new DateTime(2020, 1, 1),
                InsuredRisks        = risks
            });

            context.Policies.AddRange(policies);
            int changed = context.SaveChanges();

            _dbContext = context;
        }
Esempio n. 3
0
        public bool CreateClient(Client client)
        {
            bool success;

            try
            {
                context.Clients.Add(client);
                context.SaveChanges();
                success = true;
            }
            catch
            {
                success = false;
            }
            return(success);
        }
Esempio n. 4
0
        public static void EnsureSeedDataForContext(this InsuranceCompanyContext context)
        {
            context.Database.EnsureCreated();
            //If we already have data, we don't insert anything.
            if (context.Clients.Any())
            {
                return;
            }

            InsertClients(context);
            InsertPolicies(context);
            InsertUsers(context);

            context.SaveChanges();
        }