我在 mvc4 应用程序中使用流利的多对多映射存在问题,当创建数据库(SQL2012Express)时,没有创建要映射的两个表。
我想映射到多对多关系,因为一个产品可以与许多类别相关联,而类别可以是许多产品。在我的班级类别中,我有:
public class Categoria : BaseEntity
{
public virtual string Name { get; set; }
public virtual ICollection<Prodotti> Prodotti { get; set; }
public Categoria()
{
Prodotti = new List<Prodotti>();
}
public virtual void AddProdotti(Prodotti pro)
{
Prodotti.Add(pro);
}
}
在产品类中:
public class Prodotti:BaseEntity
{
public virtual string Name { get; set; }
public virtual ICollection<Categoria> Categoria { get; set; }
public virtual void AddCategorie(Categoria cat)
{
Categoria.Add(cat);
}
public Prodotti()
{
Categoria = new List<Categoria>();
}
}
类图分别为:
public class CategoriaMap : ClassMap<Categoria>
{
public CategoriaMap()
{
Id(x => x.Id).GeneratedBy.HiLo("Id");
Map(x => x.Name);
Map(x => x.Description);
Map(x => x.Active).Default("True");
HasManyToMany(x => x.Prodotti)
.Inverse()
.Table("CategoryProductsMap")
.ParentKeyColumn("CategoriaId").ChildKeyColumn("ProdottiId")
.Cascade.All(); ;
}
}
public class ProdottiMap :ClassMap<Prodotti>
{
public ProdottiMap()
{
Id(x=>x.Id).GeneratedBy.HiLo("Id");
Map(x => x.Name).Not.Nullable();
Map(x => x.Price).Precision(3);
Map(x => x.isFeatured).Nullable();
Map(x => x.ShortDescription);
Map(x => x.FullDescription);
Map(x => x.ShowOnHomePage).Nullable();
Map(x => x.Images);
Map(x => x.Published).Default("False");
Map(x => x.MetaDescription);
Map(x => x.MetaKeywords);
Map(x => x.MetaTitle);
Map(x => x.SeName);
Map(x => x.Deleted).Default("False");
Map(x => x.MinimumPurchaseCount);
Map(x => x.ShippingWeight);
Map(x => x.ShippingHeight);
Map(x => x.ShippingLength);
Map(x => x.ShippingWidth);
Map(x => x.CreatedOnUtc);
Map(x => x.UpdatedOnUtc);
HasManyToMany(x => x.Categoria)
.Table("CategoryProductsMap")
.ParentKeyColumn("ProdottiId").ChildKeyColumn("CategoriaId");
}
}
配置是:
var configuration =Fluently.Configure()
.Database(MsSqlConfiguration.MsSql2008.ShowSql().ConnectionString(c =>
c.FromConnectionStringWithKey("Connection1")))
.Mappings(m => m.FluentMappings
.AddFromAssemblyOf<latticinibufala.Entities.Maps.CustomerMap>() )
// Set session context class to enable ManagedWebSessionContext usage
.ExposeConfiguration(cfg => cfg.SetProperty("current_session_context_class", "managed_web"))
.ExposeConfiguration(cfg => cfg.SetProperty("adonet.batch_size", "10"))
.ExposeConfiguration(BuildSchema)
.BuildConfiguration()
;
sessionFactory = configuration.BuildSessionFactory();
有什么问题?