2

I have a couple of POCO objects that look like this:

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; }
}

I have mappings like so:

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);
    }
}

I am mapping the interface properties like so:

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

When I generate my database (MSSQL 2005) using

    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;
        }
    }

I end up with things linking properly, but with an extra column in my ObjectProperties table, and I don't understand why:

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]

Can someone explain to me why I have 2 columns with foreign keys being mapped to the same column in the DataObjects table, and how I can prevent that from happening?

Jeremy Holovacs
  • 22,480
  • 33
  • 117
  • 254

1 Answers1

2

I found the answer here: Fluent NHibernate generates extra columns Basically I needed to specify the key column in my properties collection like so:

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

and all was better.

Community
  • 1
  • 1
Jeremy Holovacs
  • 22,480
  • 33
  • 117
  • 254