Any ideas? I could really do with some help on this one.
Much appriciated :)

On Mar 6, 11:40 pm, sianabanana <sianm...@hotmail.com> wrote:
> I have a more complex many to many relationship and need to create the
> link between the manys as an object to store further data.
>
> Order  - id, date
>
> Order Product -  orderid, productid, quantity
>
> Product - id, name
>
> I am using auto mapping, and i am overriding the mapping to specify
> anything that doesnt fall in to the convention.
>
> No matter what conbination of properties i use, i cannot get the order
> product object to persis to the database.
>
> When profiling, the order object causes an insert, the product causes
> an insert
> but the order product causes an update - but there is nothing to
> update and this throws the following error. Unexpected row count: 0;
> expected: 1 - as you would expect.
>
> I would love if someone could show me a working example of how to get
> this working.
>
> public class Order
>     {
>         public virtual int Id { get; set; }
>
>         public virtual DateTime OrderDate { get; set; }
>
>         public virtual IList<OrderProduct> Products { get; set; }
>
> }
>
> public class Product
>     {
>         public virtual int Id { get; set; }
>         public virtual string Name { get; set; }
>
>         public virtual IList<OrderProduct> Orders { get; set; }
>     }
>
>     public class OrderProduct
>     {
>         public virtual Order Order {get;set;}
>         public virtual Product Product {get;set;}
>         public virtual int Quantity {get;set;}
>
>         public override bool Equals(object obj)
>         {
>             if (obj == null)
>                 return false;
>
>             OrderProduct other = (OrderProduct)obj;
>
>             return (Order.Id == other.Order.Id && Product.Id ==
> other.Product.Id);
>         }
>
>         public override int GetHashCode()
>         {
>             return string.Format("{0}|{1}",
>                 Order.Id, Product.Id).GetHashCode();
>         }
>     }
>
>         public class OrderMapOverride : IAutoMappingOverride<Order>
>     {
>         public void Override(AutoMap<Order> map)
>         {
>             map.HasMany(x => x.Products)
>                 .Inverse();
>         }
>     }
>
> public class OrderProductMapOverride :
> IAutoMappingOverride<OrderProduct>
>     {
>         public void Override(AutoMap<OrderProduct> map)
>         {
>             map.UseCompositeId()
>                 .WithKeyReference(x => x.Order, "OrderId")
>                 .WithKeyReference(x => x.Product, "ProductId");
>
>             //map.HasOne(x => x.Order)
>             //    .WithForeignKey("OrderId");
>
>             //map.HasOne(x => x.Product)
>             //    .WithForeignKey("ProductId");
>
>             //map.References(x => x.Order)
>             //    .WithForeignKey("Id").TheColumnNameIs("OrderId");
>             //map.References(x => x.Product)
>             //    .WithForeignKey("Id").TheColumnNameIs("ProductId");
>         }
>     }
>
> public class ProductMapOverride : IAutoMappingOverride<Product>
>     {
>         public void Override(AutoMap<Product> map)
>         {
>             map.HasMany(x => x.Orders)
>                 .Inverse();
>         }
>     }
>
> ps. i set the following in my conventions.
> convention.GetPrimaryKeyNameFromType = type => type.Name + "Id";
> convention.GetForeignKeyNameOfParent = type => type.Name + "Id";
> convention.GetForeignKeyName = type => type.Name + "Id";
> convention.GetManyToManyTableName = (parent, child) => parent.Name +
> "_" + child.Name;
> convention.OneToManyConvention = o => o.Cascade.All();
> convention.OneToOneConvention = o => o.Cascade.All();
> convention.ManyToOneConvention = o => o.Cascade.All();
>
> I have puzzled over this for days and really could do with some help
> on this one.
>
> Thanks
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Fluent NHibernate" group.
To post to this group, send email to fluent-nhibernate@googlegroups.com
To unsubscribe from this group, send email to 
fluent-nhibernate+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/fluent-nhibernate?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to