NHibernate: à l'Aide de Fluent Nhibernate pour enregistrer les objets enfants

Dans mon système, j'ai deux entités - ShoppingCart et ShoppingCartItem. Relativement générique de cas d'utilisation. Cependant, lorsque je sauvegarde mon ShoppingCart, aucun des articles sont enregistrées dans la bd.

Au sein de mon objet, je crée un nouveau ShoppingCart objet.

ShoppingCart cart = CreateOrGetCart();

Je puis ajouter un Produit existant que j'ai obtenu à partir de la base de données pour le début.

cart.AddItem(product);

C'est juste un simple wrapper pour ajouter l'élément à la IList.

    public virtual void AddItem(Product product)
    {
        Items.Add(new ShoppingCartItem { Quantity = 1, Product = product });
    }

Je puis appeler SaveOrUpdate sur le Référentiel

Repository.SaveOrUpdate(cart);

Qui ressemble à ceci:

   public T SaveOrUpdate(T entity)
    {
        Session.SaveOrUpdate(entity);
        return entity;
    }

J'utilise Couramment NHibernate pour la cartographie:

    public ShoppingCartItemMap()
    {
        WithTable("ShoppingCartItems");

        Id(x => x.ID, "ShoppingCartItemId");
        Map(x => x.Quantity);

        References(x => x.Cart, "ShoppingCartId").Cascade.SaveUpdate();
        References(x => x.Product, "ProductId");
    }


    public ShoppingCartMap()
    {
        WithTable("ShoppingCarts");

        Id(x => x.ID, "ShoppingCartId");
        Map(x => x.Created);
        Map(x => x.Username);

        HasMany<ShoppingCartItem>(x => x.Items)
            .IsInverse().Cascade.SaveUpdate()
            .WithKeyColumn("ShoppingCartId")
            .AsBag();
    }

Schéma de base de données (SQL Server 2005) est également assez générique:

CREATE TABLE [dbo].[ShoppingCarts]
(
[ShoppingCartID] [int] NOT NULL IDENTITY(1, 1),
[Username] [nvarchar] (50) NOT NULL,
[Created] [datetime] NOT NULL
)
GO
ALTER TABLE [dbo].[ShoppingCarts] ADD CONSTRAINT [PK_ShoppingCarts] PRIMARY KEY CLUSTERED ([ShoppingCartID])
GO



CREATE TABLE [dbo].[ShoppingCartItems]
(
[ShoppingCartItemId] [int] NOT NULL IDENTITY(1, 1),
[ShoppingCartId] [int] NOT NULL,
[ProductId] [int] NOT NULL,
[Quantity] [int] NOT NULL
)
GO
ALTER TABLE [dbo].[ShoppingCartItems] ADD CONSTRAINT [PK_ShoppingCartItems] PRIMARY KEY CLUSTERED ([ShoppingCartItemId])
GO
ALTER TABLE [dbo].[ShoppingCartItems] ADD CONSTRAINT [FK_ShoppingCartItems_Products] FOREIGN KEY ([ProductId]) REFERENCES [dbo].[Products] ([ProductId])
GO
ALTER TABLE [dbo].[ShoppingCartItems] ADD CONSTRAINT [FK_ShoppingCartItems_ShoppingCarts] FOREIGN KEY ([ShoppingCartId]) REFERENCES [dbo].[ShoppingCarts] ([ShoppingCartID])
GO

Quand je SaveOrUpdate mon ShoppingCart, pourquoi n'est-il pas tout ShoppingCartItems également être sauvé?

S'il vous plaît aider.

Grâce

Ben

Mise à JOUR:
En l'enveloppant dans une transaction providng moi avec un peu plus d'info:

Impossible d'insérer la valeur NULL dans la colonne 'ShoppingCartId", table
'WroxPizza.dbo.ShoppingCartItems'; la colonne n'autorise pas les valeurs null.
INSERTION échoue. La déclaration a été résilié.

C'est parce que c'est un nouveau panier.

  • James Grégoire solution a fonctionné pour moi.
InformationsquelleAutor Ben Hall | 2009-02-08