17

Làm thế nào để tạo ra xml ánh xạ các file như là một phần của các bài kiểm tra của tôi trong MappingIntegrationTestsTạo ánh xạ XML từ thạo Nhibernate

tôi cần phải tự kiểm tra xem ánh xạ thông thạo tương quan với các ánh xạ trong dự án leagcy.

Trả lời

18

Bạn có thể làm một cái gì đó như:

config.Mappings(m => 
    { 
     m.FluentMappings.ExportTo("...file path here..."); 
     m.HbmMappings.ExportTo("...file path here..."); 
     m.AutoMappings.ExportTo("...file path here..."); 
    { 
); 

tôi không thích nó bản thân mình. Nếu tôi tìm thấy một số cách tốt hơn (nếu có tồn tại ở tất cả) tôi sẽ cập nhật câu trả lời.

Xem
http://blog.jagregory.com/2009/02/03/fluent-nhibernate-configuring-your-application/
Hoặc nếu bị hỏng, thấy điều này thay vì
https://github.com/jagregory/fluent-nhibernate/wiki/Database-configuration

+0

Ít nhất với phiên bản hiện tại của FluentNH (ví dụ 1.3), 'm.HbmMappings.ExportTo()' không tồn tại - điều này có ý nghĩa gì đó vì ánh xạ .hbm đã là các tệp. – Oliver

+1

Điều này đã giúp tôi rất nhiều. +1 –

+0

Liên kết bị hỏng –

8

Bạn tạo ánh xạ XML bằng cách gọi phương thức ExportTo().

Ví dụ:

ISessionFactory sessionFactory = FluentNHibernate.Cfg.Fluently.Configure() 
    .Database(FluentNHibernate.Cfg.Db.MsSqlConfiguration.MsSql2008 
    .ConnectionString(connectionString) 
) 
    .Mappings(m => m.FluentMappings.AddFromAssembly(assembly) 
    .ExportTo(@"C:\your\export\path") 
) 
    .BuildSessionFactory(); 

Xem ở đây cho tài liệu:

http://wiki.fluentnhibernate.org/Fluent_configuration

+0

Liên kết bị hỏng –

3

tôi sử dụng (gần như) phương pháp mở rộng này để có được những XBM trong bộ nhớ để tôi có thể xem nó trong dự án thử nghiệm của tôi:

public static IDictionary<string, string> LoadHBM(this FluentConfiguration cfg) 
    { 
     var result = new Dictionary<string, string>(); 
     var mem = new MemoryStream(); 
     var writer = new StreamWriter(mem); 
     var reader = new StreamReader(mem); 

     cfg.Mappings(x => 
     { 
      x.FluentMappings.ExportTo(writer); 
      x.AutoMappings.ExportTo(writer); 
     }); 

     cfg.BuildConfiguration(); 
     writer.Flush(); 
     mem.Seek(0, 0); 
     var hbm = reader.ReadToEnd(); 

     var objects = XElement.Parse("<junk>" + hbm + "</junk>").Elements(); 
     objects.ToList().ForEach(x => result.Add(x.Elements().First().Attribute("name").Value, x.ToString())); 
     return result; 
    } 

Chỉnh sửa: Đã cập nhật cho FNH 1.2.

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