Пример #1
0
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                NHibernateHelper nhibernateHelper = new NHibernateHelper(@"fluentData.db");
                using(ISessionFactory sessionFactory = nhibernateHelper.GetSessionFactory())
                {
                    using (ISession session = sessionFactory.OpenSession())
                    {
                        using (ITransaction transaction = session.BeginTransaction())
                        {
                            var company = new Company
                            {
                                OrganizationNumber = collection.GetValue("OrganizationNumber").AttemptedValue,
                                Name = collection.GetValue("Name").AttemptedValue
                            };

                            session.SaveOrUpdate(company);

                            transaction.Commit();

                            //collection.Set("Id", company.Id.ToString());

                            return RedirectToAction("Edit", new { id = company.Id });
                        }
                    }
                }

            }
            catch
            {
                return View();
            }
        }
Пример #2
0
 private static void AddEmployeesToCompany(Company company, params Employee[] employees)
 {
     foreach (var employee in employees)
     {
         company.AddEmployee(employee);
     }
 }
Пример #3
0
        static void Main(string[] args)
        {
            ISessionFactory sessionFactory = CreateSessionFactory();
            using (ISession session = sessionFactory.OpenSession())
            {
                using (ITransaction transaction = session.BeginTransaction())
                {
                    var khufu = new Company
                    {
                        OrganizationNumber = "123456789",
                        Name = "Khufu"
                    };

                    var okado = new Company
                    {
                        OrganizationNumber = "234567890",
                        Name = "Okado"
                    };

                    var kenneth = new Employee
                    {
                        BirthNumber = "16027512345",
                        Firstname = "Kenneth",
                        Surname = "Myhra"
                    };
                    var paras = new Employee
                    {
                        BirthNumber = "01017312345",
                        Firstname = "Paras",
                        Surname = "Kafley"
                    };

                    var christian = new Employee
                    {
                        BirthNumber = "01057523456",
                        Firstname = "Christian",
                        Surname = "Bogstad"
                    };

                    var padraic = new Employee
                    {
                        BirthNumber = "01017634567",
                        Firstname = "Padraic",
                        Surname = "Woods"
                    };

                    var axel = new Employee
                    {
                        BirthNumber = "14047512335",
                        Firstname = "Axel",
                        Surname = "Haugan"
                    };

                    AddEmployeesToCompany(khufu, kenneth, paras, axel, christian, padraic);

                    AddEmployeesToCompany(okado, padraic, christian);

                    session.SaveOrUpdate(khufu);

                    session.SaveOrUpdate(okado);

                    transaction.Commit();
                }
                using (session.BeginTransaction())
                {
                    var companies = session.CreateCriteria<Company>()
                        .List<Company>();
                    foreach (var company in companies)
                    {
                        WriteCompanyPretty(company);
                    }
                }
            }
            Console.ReadKey(true);
        }
Пример #4
0
 private static void WriteCompanyPretty(Company company)
 {
     Console.WriteLine(company.Name);
     Console.WriteLine("\tEmployees:");
     foreach (var employee in company.Employees)
     {
         Console.WriteLine("\t\t" + employee.Firstname + " " + employee.Surname);
     }
     Console.WriteLine();
 }