我在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();
可能出现什么问题?
答案 0 :(得分:0)
尝试在CategoriaMap
构造函数
Table("Categoria");
并跟随ProdottiMap
构造函数
Table("Prodotti");
答案 1 :(得分:0)
我已经解决了,错误的行代码是: Map(x =&gt; x.Active).Default(“True”);
因为数据库需要数字1 = True
答案 2 :(得分:0)
只需添加fuent-nHibernate配置文件
var props = new Dictionary<string, string>();
props.Add("query.substitutions", "true 1, false 0");
然后
.BuildConfiguration().AddProperties(props);