☰
✕
Главная
© 2002 - 2025
Diary.ru
NHibernate - Lesson 2. Mapping Components
netcat-na-diary
| понедельник, 29 июля 2013
Класс Customer.cs
using System; namespace NHibernateDemo { public class Customer { public Customer() { MemberSince = DateTime.UtcNow; } public virtual int Id { get; set; } public virtual string FirstName { get; set; } public virtual string LastName { get; set; } public virtual double AverageRating { get; set; } public virtual int Points { get; set; } public virtual bool HasGoldStatus { get; set; } public virtual DateTime MemberSince { get; set; } public virtual CustomerCreditRating CreditRating { get; set; } public virtual Location Adress { get; set; } public override string ToString() { return string.Format("{1} {2} ({0}), " + "\nPoints {3}, \nHasGoldStatus: {4}, \nMemberSince: {5} ({7})," + "\nCreditRating: {6}, \nAverageRating: {8}", Id, FirstName, LastName, Points, HasGoldStatus, MemberSince, CreditRating, MemberSince.Kind, AverageRating); } } public class Location { public virtual string Street { get; set; } public virtual string City { get; set; } public virtual string Province { get; set; } public virtual string Country { get; set; } } public enum CustomerCreditRating { Excellent, Good, Neutral, Poor, Terrible } }
XML-файл Customer.hbm.xml
<?xml version="1.0" encoding="utf-8" ?> <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="NHibernateDemo" namespace="NHibernateDemo"> <class name="Customer"> <id name="Id"> <generator class="native" /> </id> <property name="FirstName"/> <property name="LastName"/> <property name="AverageRating"/> <property name="Points"/> <property name="HasGoldStatus"/> <property name="MemberSince" type="UtcDateTime"/> <property name="CreditRating" type="CustomerCreditRatingType"/> <component name="Adress"> <property name="Street"/> <property name="City"/> <property name="Province"/> <property name="Country"/> </component> </class> </hibernate-mapping>
Класс Program.cs
using System; using System.Linq; using System.Reflection; using HibernatingRhinos.Profiler.Appender.NHibernate; using NHibernate.Cfg; using NHibernate.Dialect; using NHibernate.Driver; using NHibernate.Linq; namespace NHibernateDemo { internal class Program { static void Main(string[] args) { NHibernateProfiler.Initialize(); var cfg = new Configuration(); cfg.DataBaseIntegration(x => { //x.LogSqlInConsole = true; x.ConnectionString = "Server=.; Database=NHibernateDemo; Integrated Security = SSPI;"; x.Driver<SqlClientDriver>(); x.Dialect<MsSql2008Dialect>(); }); cfg.AddAssembly(Assembly.GetExecutingAssembly()); var sessionFactory = cfg.BuildSessionFactory(); int newId; using (var session = sessionFactory.OpenSession()) using (var tx = session.BeginTransaction()) { var newCustomer = CreateCustomer(); Console.WriteLine("Before saving:"); Console.WriteLine(newCustomer); session.Save(newCustomer); newId = newCustomer.Id; tx.Commit(); } using (var session = sessionFactory.OpenSession()) using (var tx = session.BeginTransaction()) { var newCustomer = session.Load<Customer>(newId); Console.WriteLine("\nAfter saving:"); Console.WriteLine(newCustomer); session.Save(newCustomer); tx.Commit(); } Console.WriteLine("Enter any key to exit..."); Console.ReadKey(); } private static Customer CreateCustomer() { return new Customer { FirstName = "Jonh", LastName = "Doe", Points = 100, HasGoldStatus = true, // MemberSince = new DateTime(2012, 1, 1), CreditRating = CustomerCreditRating.Good, AverageRating = 42.44454647, Adress = new Location() { Street = "123 Somewhere Avenue", City = "Nowhere", Province = "Alberta", Country = "Canada" } }; } } }
БД
БД
NHibernate
NHibernate Pluralsight Training
Смотрите также
Глаза - омут
Программы / ненависть
Вечер!
Музыка
Франция
...и ещё одно поздравление))))