流利的NHibernate正在创建一个额外的外键列......为什么?

时间:2011-11-01 17:52:24

标签: c# sql-server fluent-nhibernate

我有几个看起来像这样的POCO对象:

public abstract class DataObject : IDataObject
{
    public virtual Guid ObjectID { get; set; }
    public virtual IList<ObjectProperty> Properties { get; set; }
}

public class ObjectProperty : IProperty
{
    public virtual Guid ObjectPropertyID { get; set; }
    public virtual DataObject Object { get; set; }
    public virtual string Name { get; set; }
    public virtual string Value { get; set; }
    public virtual string Type { get; set; }
    public virtual int? Scale { get; set; }
    public virtual int? Precision { get; set; }
}

我有这样的映射:

public class DataObjectMapping : ClassMap<DataObject>
{
    public DataObjectMapping()
    {
        Table("DataObjects");

        this.MapIDataObjectProperties();
        this.MapIHasProperties();
    }
}

    public ObjectPropertyMapping()
    {
        Table("ObjectProperties");
        Id(x => x.ObjectPropertyID).Column("ObjectPropertyID");
        References(x => x.Object).Index("ixObject.Name").Column("ObjectID").Not.Nullable();
        Map(x => x.Name).Index("ixObject.Name").Length(100).Not.Nullable();
        Map(x => x.Scale);
        Map(x => x.Precision);
        Map(x => x.Type);
        Map(x => x.Value);
    }
}

我正在映射接口属性,如下所示:

    public static void MapIHasProperties<T>(this ClassMap<T> target) where T : IHasProperties
    {
        target.HasMany(x => x.Properties);
    }

使用

生成数据库(MSSQL 2005)时
    public static ISessionFactory SessionFactory
    {
        get
        {
            if (_SessionFactory == null)
            {
                var config = new Configuration().Configure();
                _SessionFactory = Fluently.Configure(config)
                    .Mappings(m=>m.FluentMappings.AddFromAssemblyOf<Site>()
                        .Conventions.Add<CustomForeignKeyConvention>())
                    .ExposeConfiguration(c => { new SchemaExport(c).Create(false, true); })
                    .BuildSessionFactory();
            }
            return _SessionFactory;
        }
    }

我最终得到了正确连接的东西,但我的ObjectProperties表中有一个额外的列,我不明白为什么:

USE [Sandbox]
GO
/****** Object:  Table [dbo].[ObjectProperties]    Script Date: 11/01/2011 13:50:18 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[ObjectProperties](
    [ObjectPropertyID] [uniqueidentifier] NOT NULL,
    [Name] [nvarchar](100) NOT NULL,
    [Scale] [int] NULL,
    [Precision] [int] NULL,
    [Type] [nvarchar](255) NULL,
    [Value] [nvarchar](255) NULL,
    [ObjectID] [uniqueidentifier] NOT NULL,
    [DataObject_id] [uniqueidentifier] NULL,
PRIMARY KEY CLUSTERED 
(
    [ObjectPropertyID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO
ALTER TABLE [dbo].[ObjectProperties]  WITH CHECK ADD  CONSTRAINT [FKFBE776BE7C346D9] FOREIGN KEY([DataObject_id])
REFERENCES [dbo].[DataObjects] ([ObjectID])
GO
ALTER TABLE [dbo].[ObjectProperties] CHECK CONSTRAINT [FKFBE776BE7C346D9]
GO
ALTER TABLE [dbo].[ObjectProperties]  WITH CHECK ADD  CONSTRAINT [FKFBE776BF671B18E] FOREIGN KEY([ObjectID])
REFERENCES [dbo].[DataObjects] ([ObjectID])
GO
ALTER TABLE [dbo].[ObjectProperties] CHECK CONSTRAINT [FKFBE776BF671B18E]

有人可以向我解释为什么我有2列外键被映射到DataObjects表中的同一列,以及我如何防止这种情况发生?

1 个答案:

答案 0 :(得分:2)

我在这里找到答案:Fluent NHibernate generates extra columns基本上我需要在我的属性集合中指定键列,如下所示:

public DataObjectMapping()
{
    Table("DataObjects");
    Id(x => x.ObjectID).Column("ObjectID");
    HasMany(x => x.Properties).KeyColumn("ObjectID");
}

一切都好了。