2008-10-05 25 views
5

Tôi vừa yêu NHibernate và giao diện thông thạo. Sau này cho phép ánh xạ rất đẹp với hỗ trợ tái cấu trúc (không cần nhiều hơn cho các tệp xml).Kết hợp ánh xạ XML và Linh hoạt cho NHibnernate

Nhưng không ai là hoàn hảo, vì vậy tôi thiếu bản đồ nhiều đối tượng thành thạo. Có ai biết nếu nó đã có? Nếu vậy, một dòng mã đơn giản sẽ rất tuyệt.

Nhưng để dính vào tiêu đề của câu hỏi, có cách nào kết hợp ánh xạ NHibernate thông thường và thông thường không.

Hiện tại tôi sử dụng các dòng sau cho thiết lập thử nghiệm của tôi VỚI thông thạo, và khối mã thứ hai cho thử nghiệm của tôi KHÔNG thông thạo (với ánh xạ XML). Làm thế nào tôi có thể nói thông thạo sử dụng thông thạo NẾU CÓ và XML khác ...

 var cfg = new Configuration(); 
     cfg.AddProperties(MsSqlConfiguration.MsSql2005.ConnectionString.Is(_testConnectionstring).ToProperties()); 
     cfg.AddMappingsFromAssembly(typeof(CatMap).Assembly); 
     new SchemaExport(cfg).Create(true, true); 

     var persistenceModel = new PersistenceModel(); 
     persistenceModel.addMappingsFromAssembly(typeof(CatMap).Assembly); 
     IDictionary<string, string> properties = MsSqlConfiguration.MsSql2005.UseOuterJoin().ShowSql().ConnectionString.Is(_testConnectionstring).ToProperties(); 
     properties.Add("command_timeout", "340"); 

     session = new SessionSource(properties, persistenceModel).CreateSession(); 

Nếu không thạo ...

 config = new Configuration(); 
     IDictionary props = new Hashtable(); 

     props["connection.provider"] = "NHibernate.Connection.DriverConnectionProvider"; 
     props["dialect"] = "NHibernate.Dialect.MsSql2005Dialect"; 
     props["connection.driver_class"] = "NHibernate.Driver.SqlClientDriver"; 
     props["connection.connection_string"] = "Server=localhost;initial catalog=Debug;Integrated Security=SSPI"; 
     props["show_sql"] = "true"; 
     foreach (DictionaryEntry de in props) 
     { 
      config.SetProperty(de.Key.ToString(), de.Value.ToString()); 
     } 
     config.AddAssembly(typeof(CatMap).Assembly); 

     SchemaExport se = new SchemaExport(config); 
     se.Create(true, true); 

     factory = config.BuildSessionFactory(); 
     session = factory.OpenSession(); 

Vậy đó ... Chris

PS: Tôi thực sự giống như trang web này, GUI là hoàn hảo và chất lượng của tất cả các bài viết là không thể tin được. Tôi nghĩ rằng nó sẽ được :-) khổng lồ Phải đăng ký ...

Trả lời

2

Mapping từ Foo để Baa:

HasManyToMany<Baa> (x => Baas) 
    .AsBag () //can also be .AsSet() 
    .WithTableName ("foobar") 
    .WithParentKeyColumn ("fooId") 
    .WithChildKeyColumn ("barId") ; 

Kiểm tra các ví dụ trong ClassMapXmlCreationTester - họ cũng hiển thị những gì các tên cột mặc định là.

2

ManyToAny hiện không được triển khai (tính đến thời điểm viết).

Về thiết lập của bạn cho ánh xạ trôi chảy và không thông thạo, bạn gần như ở đó với ví dụ đầu tiên của bạn.

var cfg = MsSqlConfiguration.MsSql2005 
    .ConnectionString.Is(_testConnectionstring) 
    .ConfigureProperties(new Configuration()); 

cfg.AddMappingsFromAssembly(typeof(CatMap).Assembly); // loads hbm.xml files 

var model = new PersistenceModel(); 
model.addMappingsFromAssembly(typeof(CatMap).Assembly); // loads fluent mappings 
mode.Configure(cfg); 

new SchemaExport(cfg).Create(true, true); 

Sự khác biệt chính là SchemaExport là cuối cùng. Tôi cho rằng ví dụ đầu tiên của bạn thực sự đang tải các ánh xạ thông thạo, nhưng nó đã tạo ra lược đồ theo điểm đó.

2

Bạn có thể thực hiện chính xác những gì bạn muốn làm hoàn toàn trong Fluent NHibernate.

Đoạn mã sau sẽ sử dụng cú pháp Fluent NHibernate để cấu hình thành thạo nhà máy phiên tìm kiếm các tệp ánh xạ HBM (xml), ánh xạ trôi chảy và các quy ước từ nhiều hội đồng có thể.

var _mappingAssemblies = new Assembly[] { typeof(CatMap).Assembly }; 
var _autoPersistenceModel = CreateAutoPersistenceModel(); 
Fluently.Configure() 
     .Database(MsSqlConfiguration.MsSql2005.ConnectionString(_testConnectionstring)) 
     .Mappings(m => 
        { 
         foreach (var assembly in _mappingAssemblies) 
         { 
          m.HbmMappings.AddFromAssembly(assembly); 
          m.FluentMappings.AddFromAssembly(assembly) 
           .Conventions.AddAssembly(assembly); 
         } 
         m.AutoMappings.Add(_autoPersistenceModel); 
        }) 
     .ExposeConfiguration(c => c.SetProperty("command_timeout", "340")) 
     .BuildSessionFactory(); 

Có rất nhiều lựa chọn khác có sẵn cho bạn cũng như: Fluent NHibernate Database Configuration

Các vấn đề liên quan