流畅的Nhibernate模式生成 [英] Fluent Nhibernate Schema Generation

查看:129
本文介绍了流畅的Nhibernate模式生成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在玩FluentNhibernate作为S#arp架构的一部分。下面是一个示例映射。

  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).ColumnNameIs(AgeFrom)) ;
m.Map(x => x.To).ColumnNameIs(AgeTo);
});

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

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




$ b $ p
$ b然后我使用Nhibernate模式生成来输出我的ddl

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

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

到目前为止,然而,这个表生成的DDL不匹配,实际上包含一个错误。

  create table Event_Header(
Id INT IDENTITY NOT NULL,
EmailAddress NVARCHAR(255)null,
PhoneNumber NVARCHAR(255)null,
状态字符串null,
网站NVARCHAR(255)空,
说明NVARCHAR(255)空,
名称NVARCHAR(255)null ,
价格DECIMAL(19,5)null,
Rating INT空,
AgeTo INT空,
AgeFrom INT空,
主键(Id)




  • 尽管我尝试过Enum状态,强制它使用INT

  • 电话号码长度与映射不匹配



  • 我想知道如何去调试这个。这是FluentNH中映射的问题吗?还是模式生成器有问题?如果我可以输出生成的XML然后我可以验证。有没有人知道如何做到这一点?



    谢谢,

    解决方案

    Fluent Configuration 允许您导出XML。



    你最近的拷贝是否是#arch,更具体地说,你知道它使用的是什么样的Fluent NHibernate的修订版吗?

    枚举类型被覆盖通过指定枚举的约定应映射为字符串,而不是使用 CustomSqlType 尝试使用 CustomTypeIs< int>()



    至于列的长度,这听起来像是一个错误,但它是否仍然是一个问题将取决于你正在运行的版本。 >

    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,

    解决方案

    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.

    这篇关于流畅的Nhibernate模式生成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆