views:

21

answers:

1

I have two Classes. "Product" and Product can have a number of objects of class "Attribute"

When someone edits the "Product" I would like to delete all Attributes associated with that product and recreate the new ones (second part works fine) but I'm getting a error which stops me removing the attributes.

public ProductMapping()
    {
        Id(x => x.Id);
        Map(x => x.ProductName);
        Map(x => x.Description);
        Map(x => x.Quantity);
        Map(x => x.Price);
        Map(x => x.Live);
        HasMany(x => x.Images)
            .KeyColumn("ProductId")
            .Cascade.All()
            .Inverse();
        HasMany(x => x.Attribute)
            .KeyColumn("ProductId")
            .Cascade.All();

    }

And my Attribute mapping

    public AttributeMapping()
    {
        Id(x => x.Id);
        Map(x => x.Name);
        Map(x => x.Value);
        Map(x => x.Live);

        References(x => x.Product)
            .ForeignKey()
            .Column("ProductId")
            .Cascade.SaveUpdate();

    }

I'm not sure if my cascading is correct but I tried them all!

I've tried to get all the attributes and do

var AttRepository = GetRepository<ForSale.Domain.Attribute>();
foreach (var a in dbProduct.Attribute)
            {
                AttRepository.Delete(a);
            }

removing each one individually, also tried doing a:

dbProduct.Attributes.Clear();

Again it doesn't succeed! The error I get is

"Cannot insert the value NULL into column 'ProductId', table '{LocalFilePath}/PRODUCTS.MDF.dbo.Attribute'; column does not allow nulls. UPDATE fails. The statement has been terminated."

dbProduct

+1  A: 

You should map Product.Attribute as Inverse.

Diego Mijelshon
think i might have tried that already, but will test it again when home!
Steve
This worked.... I must have not mixed this with Cascade.All() or something!!
Steve