Mazhar and Judy - thank you for your help!
I've tried everything(?) from both of your suggestions and I'm still having problems. I looked at the link from Mazhar and added the mapping files to the nhibernate configuration. Here's that code:
Code: Select all
NHibernate.Cfg.Configuration cfg = new NHibernate.Cfg.Configuration();
cfg.Configure(ConfigUtility.LocateConfigFile("nhibernate.config"));
string mappingFile = Server.MapPath("App_Data\\") + "Rebate.hbm.xml";
cfg.AddFile(mappingFile);
ISessionFactory factory = AbleContext.Current.Database.GetSession().SessionFactory;
factory = cfg.BuildSessionFactory();
I put a break point just after this code and looked at the ClassMappings in the cfg variable and it had added my new mapping. Here are a few of the values from that ClassMapping. I compared them others and they were similar although I didn't compare property by property. But it looks like the mapping took and
Code: Select all
ClassName: "MEDCOData.Rebate, App_Code.jehzjm01, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null"
Table: "NHibernate.Mapping.Table(MEDCO_Rebates)"
EntityName: "MEDCOData.Rebate"
HasPocoRepresentation: true
MappedClass: "MEDCOData.Rebate"
ProxyInterfaceName: "MEDCOData.Rebate, App_Code.jehzjm01, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null"
Key: "NHibernate.Mapping.SimpleValue(NHibernate.Mapping.Column(RebateId))"
Here is the mapping file:
Code: Select all
<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="MEDCOData.Rebate, __code" table="`MEDCO_Rebates`" lazy="true" dynamic-update="true">
<id name="Id" column="RebateId" type="int">
<generator class="native" />
</id>
<property type="string" name="Name">
<column name="Name" not-null="true" sql-type="varchar(255)" />
</property>
<property type="int" not-null="true" name="ManufacturerId" column="`ManufacturerId`" />
<property type="string" name="ManufacturerLogoUrl">
<column name="ManufacturerLogoUrl" not-null="true" sql-type="varchar(255)" />
</property>
<property type="string" name="DocumentUrl">
<column name="ManufacturerLogoUrl" not-null="true" sql-type="varchar(255)" />
</property>
<property type="string" name="Description">
<column name="ManufacturerLogoUrl" not-null="true" sql-type="varchar(255)" />
</property>
<property type="string" name="ProductCodes">
<column name="ManufacturerLogoUrl" not-null="true" sql-type="varchar(255)" />
</property>
<property type="string" name="LineCode">
<column name="ManufacturerLogoUrl" not-null="true" sql-type="char(3)" />
</property>
<property type="CommerceBuilder.DomainModel.LocalDateTime, CommerceBuilder" not-null="true" name="ExpirationDate" column="ExpirationDate" />
<property type="CommerceBuilder.DomainModel.LocalDateTime, CommerceBuilder" not-null="true" name="CreatedDate" column="CreatedDate" />
<property type="CommerceBuilder.DomainModel.LocalDateTime, CommerceBuilder" name="DisplayDate" column="DisplayDate" />
</class>
</hibernate-mapping>
and here is Rebate class.
Code: Select all
namespace MEDCOData
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using CommerceBuilder.DomainModel;
using System.ComponentModel;
using NHibernate.Criterion;
using NHibernate;
using NHibernate.Mapping.Attributes;
using CommerceBuilder.Products;
using CommerceBuilder.Common;
public partial class Rebate : Entity
{
public override int Id { get; set; }
public virtual string Name { get; set; }
public virtual int ManufacturerId { get; set; }
public virtual string ManufacturerLogoUrl { get; set; }
public virtual string DocumentUrl { get; set; }
public virtual string Description { get; set; }
public virtual string ProductCodes { get; set; }
public virtual string LineCode { get; set; }
public virtual DateTime ExpirationDate { get; set; }
public virtual DateTime CreatedDate { get; set; }
public virtual DateTime DisplayDate { get; set; }
[System.Obsolete("Use the Id property instead.")]
public virtual int RebateId
{
get { return this.Id; }
set { this.Id = value; }
}
public Rebate()
{
}
public Rebate(string name, int manufacturerId, string manufacturerLogoUrl, string documentUrl, string description, string productCodes, string lineCode, DateTime expirationDate, DateTime createdDate, DateTime? displayDate)
{
this.Name = name;
this.ManufacturerId = manufacturerId;
this.ManufacturerLogoUrl = manufacturerLogoUrl;
this.DocumentUrl = documentUrl;
this.Description = description;
this.ProductCodes = productCodes;
this.LineCode = lineCode;
this.ExpirationDate = expirationDate;
this.CreatedDate = createdDate;
this.DisplayDate = (DateTime)displayDate;
}
}
public partial class RebateDataSource : DataSource<Rebate, IRepository<Rebate>>
{
}
}
Using Judy's suggestion I added the RebateDataSource code and it does indeed make the methods show up in Visual Studio. But when I run CountAll or LoadAll I get 0 records even though there are several records in the database. Here are those calls:
Code: Select all
int totalRows = RebateDataSource.CountAll();
IList<Rebate> _rebates = RebateDataSource.LoadAll();
I created a function that uses the CreateSQLQuery and it properly inserts into the database. It returns the ID of the new record.
Code: Select all
public static int InsertRebate(Rebate _rebate)
{
return NHibernateHelper.CreateSQLQuery("INSERT INTO MEDCO_Rebates (name, manufacturerId, manufacturerLogoUrl, documentUrl, description, productCodes, lineCode, expirationDate, createdDate, displayDate) VALUES (:name, :manufacturerId, :manufacturerLogoUrl, :documentUrl, :description, :productCodes, :lineCode, :expirationDate, :createdDate, :displayDate); SELECT CAST(@@IDENTITY AS INT) AS 'Identity';")
.SetParameter("name", _rebate.Name)
.SetParameter("manufacturerId", _rebate.ManufacturerId)
.SetParameter("manufacturerLogoUrl", _rebate.ManufacturerLogoUrl)
.SetParameter("documentUrl", _rebate.DocumentUrl)
.SetParameter("description", _rebate.Description)
.SetParameter("productCodes", _rebate.ProductCodes)
.SetParameter("lineCode", _rebate.LineCode)
.SetParameter("expirationDate", _rebate.ExpirationDate)
.SetParameter("createdDate", _rebate.CreatedDate)
.SetParameter("displayDate", _rebate.DisplayDate)
.List<int>()
.FirstOrDefault();
}
I created another function to see if I could return the database records through a SQL query. Here's the code:
Code: Select all
public static IList<Rebate> SelectAll()
{
return NHibernateHelper.CreateSQLQuery("select rebateid, name, manufacturerId, manufacturerLogoUrl, documentUrl, description, productCodes, lineCode, expirationDate, createdDate, displayDate from MEDCO_Rebates")
.AddEntity(typeof(Rebate))
.List<Rebate>();
}
and it returned this error:
Code: Select all
MappingException was unhandled by user code. No persister for: MEDCOData.Rebate
Bottom line is that it doesn't look like NHibernate has all it needs to work with my new database table.
I also tried creating a separate project that would create a new assembly with my mapping file and class. It worked just as well/poorly as the above code. I could recreate instances of the rebate but not invoke the Save. I could run the CountAll and LoadAll methods but they returned no results. I could run the same InsertRebate method and it also worked there. And I still got the "no persister" error at the same places as the first set of code.
Any other thoughts? Seems a shame to drop back to basic SQL statements and negates some of the usefulness of having NHibernate.
Thanks again for any help you might be able to provide!