English 中文(简体)
NHibernate正在制造一个额外的外国关键一栏......为什么?
原标题:Fluent NHibernate is creating an extra foreign key column... why?

我有一对PO:

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年)时,我会利用这一数据库(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;
        }
    }

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?

最佳回答

我在此找到答案:。 液化NHibernate产生额外栏目 基本上,我需要具体说明我财产收集中的关键一栏:

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

情况更好。

问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签