What is the difference between Fluent Mapping and Auto mapping in Fluent NHibernate

After reading some articles on Fluent NHibernate, I was embarrassed about where to start.

I have an existing database for which I need to create a DataAccessLayer. I am new to NHibernate and FluentNhibernate. Since I realized that there was no need to write hbm.xml files, I chose Fluent Nhibernate.

So what is FluentMapping? and AutoMapping?

I created a LibProProject class named FirstProject.Entities

I created a class called "Client"

namespace FirstProject.Entities { public class Customer { public virtual int CustomerID { get; set; } public virtual string CustomerName { get; set; } public virtual string Address1 { get; set; } public virtual string Address2 { get; set; } public virtual string City { get; set; } public virtual string State { get; set; } public virtual int Zip { get; set; } } } 

Then I created a mapping class

 namespace FirstProject.Entities { public class CusotmerMap : ClassMap<Customer> { public CustomerMap() { Id(x => x.CustomerID).Column("CustomerID").GeneratedBy.Assigned(); Map(x => x.CustomerName); Map(x => x.Address1); Map(x => x.Address2); Map(x => x.City); Map(x => x.Zip); } } } 

Now I do not know how to move on. I am doing it right. Please suggest

how to set up and continue next

+4
source share
1 answer

Below is the Fluent

 Id(x => x.CustomerID).Column("CustomerID").GeneratedBy.Assigned(); 

I am using Fluent assertions like the following

 actual.Should().BeGreaterThan(1).And().LessThan(2); 

Fluent is basically the place where you combine teams so that they are read quite well.

Automatic matching is where you do nothing. Everything is done by agreement. I am using Auto. Free is good if you do not follow the conventions.

Based on your mapping, the CustomerId assigned is not a conditional agreement. So you need either

  • Use Freely to specify exactly how it should be displayed. This is similar to the standard use of XML, but with a smooth interface.
  • Use Auto and specify a convention that will automatically change CustomerId for the assignment.
  • Use Auto and specify an Override that Auto will use, but will override CustomerId for the destination.

If you want to make option 3, here is the code:

 var model = AutoMap .AssemblyOf<Customer>() .Where(IsMapped) .Override<Customer>(a => a.Id(b => b.CustomerId, "CustomerId").GeneratedBy.Assigned()); 

The IsMapped function should return True for the objects you want to display.

+1
source

Source: https://habr.com/ru/post/1386644/


All Articles