Hello,
I searched in the forum and even finding some related (some about mappingexception and some about assembly) I do not found any one about this two together. So, if there is any "famous" topic about this please tell me.
My solutions is organized in the fallowing way:
Ordering.sln
---Ordering.Console (console app)
------Program.cs
---Ordering.Data (library)
------Common
---------Address.cs (and others)
------Mapping
---------Address.hbm.xml (and othersConfigurei todos os XML como Build Action Embedded Resource e Copy to
Output Directory Copy Always )
I configured all my hbm.XML as Embedded Resource in the Build Action and Copy Always in the Copy to Output Directory property.
Program code:
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate;
using NHibernate.Cfg;
using Ordering.Data;
using System.IO;
namespace Ordering.Console
{
public class Program
{
static void Main(string[] args)
{
Configuration cfg = new Configuration();
cfg.Properties.Add(NHibernate.Cfg.Environment.ConnectionProvider, typeof(NHibernate.Connection.DriverConnectionProvider).AssemblyQualifiedNam e);
cfg.Properties.Add(NHibernate.Cfg.Environment.Dialect, typeof(NHibernate.Dialect.MsSql2008Dialect).AssemblyQualifiedName);
cfg.Properties.Add(NHibernate.Cfg.Environment.ConnectionDriver, typeof(NHibernate.Driver.SqlClientDriver).AssemblyQualifiedName);
cfg.Properties.Add(NHibernate.Cfg.Environment.ConnectionString, "Server= (local)\
\SQLExpress;Database=Ordering;Trusted_Connection=true;");
cfg.Properties.Add(NHibernate.Cfg.Environment.ProxyFactoryFactoryClass, typeof(NHibernate.ByteCode.Castle.ProxyFactoryFactory).AssemblyQualifiedNam e);
cfg.AddAssembly(typeof(Ordering.Data.Address).AssemblyQualifiedName); //the exception is throw here.
}
}
}
Exception:
Could not add assembly Ordering.Data.Address, Ordering.Data,
Version=1.0.0.0, Culture=neutral, PublicKeyToken=null.
Inner Exception:
System.IO.FileLoadException
{"Could not load file or assembly 'Ordering.Data.Address\\,
Ordering.Data\\, Version\\=1.0.0.0\\, Culture\\=neutral\\,
PublicKeyToken\\=null' or one of its dependencies. The given assembly
name or codebase was invalid. (Exception from HRESULT:
0x80131047)":"Ordering.Data.Address\\, Ordering.Data\\, Version\
\=1.0.0.0\\, Culture\\=neutral\\, PublicKeyToken\\=null"}.
Address.cs
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Ordering.Data
{
public class Address
{
public Address() { }
public Address(int _id, string _address1, string _address2,
string _city, string _state, string _zip)
: this()
{
this.Id = _id; this.Address1 = _address1; this.Address2 =
_address2; this.City = _city; this.State = _state; this.Zip = _zip;
}
//Fileds
public virtual int Id { get; set; }
public virtual string Address1 { get; set; }
public virtual string Address2 { get; set; }
public virtual string City { get; set; }
public virtual string State { get; set; }
public virtual string Zip { get; set; }
//Foreing key
public Contact Contact { get; set; }
public IList<OrderHeader> BillToOrderHeaders { get; set; }
public IList<OrderHeader> ShipToOrderHeaders { get; set; }
}
}
Address.hbm.xml
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
namespace="Ordering.Data" assembly="Ordering.Data">
<class name="Address" table="Address">
<id name="Id" type="Int32" unsaved-value="null">
<column name="Id"/>
<generator class="hilo"/>
</id>
<property name="Address1" type="String">
<column name="Address1" length="255" sql-type="varchar" not-
null="true"/>
</property>
<property name="Address2" type="String">
<column name="Address2" length="255" sql-type="varchar" not-
null="true"/>
</property>
<property name="City" type="String">
<column name="City" length="255" sql-type="varchar" not-
null="true"/>
</property>
<property name="State" type="String">
<column name="State" length="2" sql-type="varchar" not-
null="true"/>
</property>
<property name="Zip" type="String">
<column name="Zip" length="12" sql-type="varchar" not-
null="true"/>
</property>
<many-to-one name="Contact" class="Ordering.Data.Contact,
Ordering.Data">
<column name="Contact_Id" length="4" sql-type="int" not-
null="true"/>
</many-to-one>
<bag name="BillToOrderHeaders" inverse="true" cascade="all-delete-
orphan">
<key column="BillToAddress_Id"/>
<one-to-many class="Ordering.Data.Common.OrderHeader,
Ordering.Data"/>
</bag>
<bag name="ShipToOrderHeaders" inverse="true" cascade="all-delete-
orphan">
<key column="ShipToAddress_Id"/>
<one-to-many class="Ordering.Data.Common.OrderHeader,
Ordering.Data"/>
</bag>
</class>
</hibernate-mapping>
Both projects are .NET 3.5 framework.
Thanks for any help,
Pedro Dusso