Fluent Nhibernate Schema Generation

2019-02-18 12:39发布

问题:

I have been playing about with FluentNhibernate as part of the S#arp Architecture. Below is an example mapping.

public class EventBaseMap : ClassMap<EventBase>
{
    public EventBaseMap()
    {
        WithTable("Event_Header");
        //NotLazyLoaded(); 

        Id(x => x.Id).WithUnsavedValue(-1).GeneratedBy.Native();

        Map(x => x.Name).WithLengthOf(50).Not.Nullable();
        Map(x => x.Description).WithLengthOf(255);
        Map(x => x.Rating);
        Map(x => x.Price);
        Map(x => x.PhoneNumber).WithLengthOf(20).Not.Nullable();
        Map(x => x.EmailAddress);
        Map(x => x.Website);
        Map(x => x.State).Not.Nullable().CustomSqlTypeIs("INT");

        Component(x => x.Ages, m =>
         {
             m.Map(x => x.From).TheColumnNameIs("AgeFrom");
             m.Map(x => x.To).TheColumnNameIs("AgeTo");
         });

        HasMany(x => x.Calendar).AsBag();

        HasManyToMany(x => x.Tags)
            .WithTableName("Event_Tags")
            .WithParentKeyColumn("EventId")
            .WithChildKeyColumn("TagId").AsBag();
    }
}

I then use the Nhibernate schema generation to output my ddl to a file.

FileInfo t = new FileInfo(Server.MapPath("~/bin/MyDDL.sql"));
        StreamWriter writer = t.CreateText();

        new SchemaExport(cfg).Execute(true, false, false, true, NHibernateSession.Current.Connection, writer);

So far so good. However the generated ddl for this table doesn't match and actually contains an error.

create table Event_Header (
   Id INT IDENTITY NOT NULL,
   EmailAddress NVARCHAR(255) null,
   PhoneNumber NVARCHAR(255) null,
   State string null,
   Website NVARCHAR(255) null,
   Description NVARCHAR(255) null,
   Name NVARCHAR(255) null,
   Price DECIMAL(19,5) null,
   Rating INT null,
   AgeTo INT null,
   AgeFrom INT null,
   primary key (Id)
)
  • The Enum State is represented as a string even though I tried to force it to use INT
  • The phone number length does not match the mapping.

I was wondering how I go about debugging this. Is this a problem with the mapping in FluentNH or is it a problem with the schema generator. If I could output the xml produced then I could verify. Does anyone know how to do this?

Thanks,

回答1:

Fluent Configuration allows you to export the XML.

How recent is your copy of #arch, and more specifically, do you know what revision of Fluent NHibernate it's using?

The enum is type is being overridden by a convention which specifies enums should be mapped as strings, instead of using CustomSqlType try just using CustomTypeIs<int>().

As for the length of the columns, that sounds like a bug, but whether it's still an issue will depend on what version you're running.