复杂的购物车类如何使用Fluentautomappping进行映射

时间:2019-05-01 14:40:52

标签: asp.net fluent-nhibernate automapping

我正在开发Web应用程序。我想自动映射购物车类。

Asp.net Mvc FluentNhibernate

我的购物车课程------------------------

public class Cart : IEntity
    {    
        public virtual int CartId { get; set; }
        public
        List<CartLine> _cartLines = new List<CartLine>();
        public void AddToCart(Product product, int quantity)
        {
            CartLine cartLine = _cartLines.FirstOrDefault(c => 
            c.Product.ProductId == product.ProductId);
            if (cartLine == null)
            {
                _cartLines.Add(new CartLine { Product = product, Qunatity = quantity });
            }
            else
            {
                cartLine.Qunatity += quantity;
            }
        }
        public void RemoveFromCart(Product product)
        {
            _cartLines.RemoveAll(p => p.Product.ProductId == product.ProductId);
        }

        public decimal Total
        {
            get
            {
                return _cartLines.Sum(c => c.Product.UnitPrice * c.Qunatity);
            }
        }
        public void Clear()
        {
            _cartLines.Clear();
        }
        public List<CartLine> Lines
        {
            get
            {
                return _cartLines;
            }
        }
    }

    public class CartLine
    {
        public Product Product { get; set; }
        public int Qunatity { get; set; }
    }
}

我的自动映射配置-------

public class AutomappingConfiguration : DefaultAutomappingConfiguration
    {

        public override bool IsId(Member member)
        {
            if (member.Name == "CustomerTypeId")
            {
                return true;
            }
            return member.Name == member.DeclaringType.Name + "Id";
        }
        public override bool ShouldMap(Member member)
        {
            if (member.MemberInfo.ReflectedType.FullName.Contains("Entities.Concrete"))
            {
                return base.ShouldMap(member);
            }



            return false;
        }

        public override bool ShouldMap(Type type)
        {
            return type.Namespace != null && type.Namespace.Contains("Entities.Concrete");
        }

    }

我的映射配置-------

  

.Mappings(d => d.AutoMappings.Add(AutoMap.AssemblyOf(             新的AutomappingConfiguration())。Conventions.Add()             .Conventions.Add()            .Conventions.Add(FluentNHibernate.Conventions.Helpers.DefaultLazy.Never())

The entity '<>c' doesn't have an Id mapped. Use the Id method to map your identity property. For example: Id(x => x.Id).

Name =“ <> c” FullName =“ MyShop.Northwind.Entities.Concrete.Cart+<>c”} System.Type {System.RuntimeType}

0 个答案:

没有答案