1

我在 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();

有什么问题?

4

3 回答 3

0

尝试将以下内容添加到您的CategoriaMap构造函数中

Table("Categoria");

并遵循ProdottiMap构造函数

Table("Prodotti");
于 2012-11-15T19:05:14.590 回答
0

只需添加到您的 fuent-nHibernate 配置文件中

var props = new Dictionary<string, string>();
props.Add("query.substitutions", "true 1, false 0");

接着

.BuildConfiguration().AddProperties(props);
于 2012-11-17T09:47:17.480 回答
0

我已经解决了,错误的代码是: Map(x => x.Active).Default("True");

因为数据库想要一个数字 1 = True

于 2012-11-17T09:37:31.603 回答