1

StockPurchase我有一个 Fluent NHibernate 基于约定的映射,我的实体和我的实体之间存在多对多关系StockSale

我创建了以下约定:

public class ManyToManyLinkedTableConvention : IHasManyToManyConvention
{
    public void Apply(FluentNHibernate.Conventions.Instances.IManyToManyCollectionInstance instance)
    {
        var naming = new NamingStrategy();
        if (StringComparer.InvariantCultureIgnoreCase.Compare(instance.EntityType.Name, instance.OtherSide.EntityType.Name) > 0)
        {
            instance.Table(naming.Quote(
                string.Format(
                    "{0}To{1}",
                    Inflector.Inflector.Pluralize(instance.EntityType.Name),
                    Inflector.Inflector.Pluralize(instance.ChildType.Name))));
            instance.Not.Inverse();
        }
        else
        {
            instance.Inverse();
        }
    }
}

当我使用 hbm2ddl 函数时,它会完全按照我的需要构建表:

CREATE TABLE "StockSalesToStockPurchases"
(
  stocksale_id uuid NOT NULL,
  stockpurchase_id uuid NOT NULL,
  CONSTRAINT fkee886e07285f29f1 FOREIGN KEY (stockpurchase_id)
      REFERENCES "StockPurchases" (stockpurchaseid) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION,
  CONSTRAINT fkee886e073b8be3d9 FOREIGN KEY (stocksale_id)
      REFERENCES "StockSales" (stocksaleid) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION
);

...奇怪的是,它还创建了第二个表:

CREATE TABLE stockpurchasestostocksales
(
  stockpurchase_id uuid NOT NULL,
  stocksale_id uuid NOT NULL,
  CONSTRAINT fk396e0343285f29f1 FOREIGN KEY (stockpurchase_id)
      REFERENCES "StockPurchases" (stockpurchaseid) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION,
  CONSTRAINT fk396e03433b8be3d9 FOREIGN KEY (stocksale_id)
      REFERENCES "StockSales" (stocksaleid) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION
);

为什么会这样做,我该如何阻止它?

4

1 回答 1

2

好的,我发现了如何做到这一点。有人让我知道是否有更好的方法。

public class ManyToManyLinkedTableConvention : IHasManyToManyConvention
{
    public void Apply(IManyToManyCollectionInstance instance)
    {
        var naming = new NamingStrategy();
        string firstName;
        string secondName;

        if (StringComparer.InvariantCultureIgnoreCase.Compare(instance.EntityType.Name, instance.OtherSide.EntityType.Name) >
            0)
        {
            firstName = instance.EntityType.Name;
            secondName = instance.OtherSide.EntityType.Name;
        }
        else
        {
            secondName = instance.EntityType.Name;
            firstName = instance.OtherSide.EntityType.Name;
            instance.Not.Inverse();
        }

        instance.Table(naming.Quote(
            string.Format(
                "{0}To{1}",
                Inflector.Inflector.Pluralize(firstName),
                Inflector.Inflector.Pluralize(secondName))));
    }
}
于 2013-04-13T01:21:07.830 回答