【问题标题】:Why does Fluent NHibernate AutoMappings add an underscore to Id (e.g. Entity_id)?为什么 Fluent NHibernate AutoMappings 为 Id 添加下划线(例如 Entity_id)?
【发布时间】:2010-08-03 09:48:51
【问题描述】:

嗨,使用流畅的 nibernate 自动映射

映射这个

    public virtual int Id { get; set; }
    /*...snip..*/
    public virtual MapMarkerIcon MapMarkerIcon { get; set; }
}

到这里

CREATE TABLE [Attraction](
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [MapMarkerIconId] [int] NULL
)

用这个:

var cfg = Fluently.Configure()


            .Database(MsSqlConfiguration.MsSql2005.ConnectionString(connectionString)
                .DefaultSchema("xxx"))

            .Mappings(m =>
                          {
                              m.AutoMappings
                                  .Add(
                                  AutoMap.AssemblyOf<Partner>().Where(
                                      n => n.Namespace == "xxx.Core.Domain")

                                  );

                              m.FluentMappings.Conventions.Add(PrimaryKey.Name.Is(x => "Id"),
                                                               DefaultLazy.Always(),
                                                               ForeignKey.EndsWith("Id")
                                  );
                          }


            )

            .ExposeConfiguration(c => c.SetProperty(Environment.ReleaseConnections, "on_close"))
            .ExposeConfiguration(c => c.SetProperty(Environment.ProxyFactoryFactoryClass, typeof(ProxyFactoryFactory).AssemblyQualifiedName))
            .BuildConfiguration();

我为什么会得到

“/XXX.Web”中的服务器错误 应用。列名无效 'MapMarkerIcon_id'。

如何让 fluentnibernate 使用 MapMarkerIconId 而不是 MapMarkerIcon_id?

【问题讨论】:

    标签: fluent-nhibernate


    【解决方案1】:
    【解决方案2】:

    以上链接均无效。这是带有当前 Fluent NHibernate 和自动映射文档链接的解决方案。

    问题(一个简单的例子):

    假设您有一个简单的示例(来自 fluent 的 wiki),其中包含一个实体及其列表中的值对象:

    public class Product
    {
      public virtual int Id { get; set; }
      //..
      public virtual Shelf { get; set; }
    }
    
    public class Shelf
    {
      public virtual int Id { get;  set; }
      public virtual IList<Product> Products { get; set; }
    
      public Shelf()
      {
        Products = new List<Product>();
      }
    }
    

    对于具有例如的表

    Shelf 
    id int identity
    
    Product 
    id int identity 
    shelfid int
    

    还有一个shelfid的外键-> Shelf.Id


    你会得到错误: 列名无效...shelf_id


    解决方案:

    添加一个约定,它可以是系统范围的,也可以是更多限制的。

    ForeignKey.EndsWith("Id")
    

    代码示例:

    var cfg = new StoreConfiguration();
    var sessionFactory = Fluently.Configure()
      .Database(/* database config */)
      .Mappings(m =>
        m.AutoMappings.Add(
          AutoMap.AssemblyOf<Product>(cfg)
              .Conventions.Setup(c =>
                  {
                      c.Add(ForeignKey.EndsWith("Id"));
                  }
        )
      .BuildSessionFactory();
    

    现在它会将ShelfId 列自动映射到Product 中的Shelf 属性。


    更多信息

    Wiki for Automapping

    Table.Is(x => x.EntityType.Name + "Table")
    PrimaryKey.Name.Is(x => "ID")
    AutoImport.Never()
    DefaultAccess.Field()
    DefaultCascade.All()
    DefaultLazy.Always()
    DynamicInsert.AlwaysTrue()
    DynamicUpdate.AlwaysTrue()
    OptimisticLock.Is(x => x.Dirty())
    Cache.Is(x => x.AsReadOnly())
    ForeignKey.EndsWith("ID")
    

    See more about Fluent NHibernate automapping conventions

    【讨论】:

      猜你喜欢
      • 2014-09-28
      • 2011-04-20
      • 1970-01-01
      • 1970-01-01
      • 2021-10-02
      • 1970-01-01
      • 1970-01-01
      • 2011-04-10
      • 2017-03-17
      相关资源
      最近更新 更多