I'm having trouble using NHibernate Fluent Automapping. He did a great job in a test project. But now..
Test method [PROJECTNAME].SubscriptionTest.SubscriptionConstructorTest threw exception: NHibernate.MappingException: No persister for: [PROJECTLIB].SubscriptionManagerRP
Class (again, the same exception occurs with a much simpler test class - so there shouldn’t be a problem here):
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "2.0.50727.4927")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://docs.oasis-open.org/wsn/b-2")]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "http://docs.oasis-open.org/wsn/b-2", IsNullable = false)]
[System.Runtime.Serialization.DataContractAttribute(Name = "SubscriptionManagerRP", Namespace = "http://docs.oasis-open.org/wsn/b-2")]
public class SubscriptionManagerRP
{
private string id;
public string Id
{
get
{
return id;
}
set
{
id = value;
}
}
public Boolean Save()
{
DatabaseAccess access = new DatabaseAccess();
var sessionFactory = access.getSessionFactory();
using (var session = sessionFactory.OpenSession())
{
using (var transaction = session.BeginTransaction())
{
SaveTextMess(this.ToString());
session.Save(this);
transaction.Commit();
return true;
}
}
return false;
}
private void SaveTextMess(String output)
{
TextWriter tw = new StreamWriter("C:\\Temp\\CespSubscriptionManagerRPMessage.txt");
tw.WriteLine(output);
tw.Close();
}
[EditorBrowsable(EditorBrowsableState.Never)]
private EndpointReferenceType consumerReferenceField;
[EditorBrowsable(EditorBrowsableState.Never)]
private FilterType filterField;
[EditorBrowsable(EditorBrowsableState.Never)]
private SubscriptionPolicyType subscriptionPolicyField;
[EditorBrowsable(EditorBrowsableState.Never)]
private System.DateTime creationTimeField;
[EditorBrowsable(EditorBrowsableState.Never)]
private bool creationTimeFieldSpecified;
private static System.Xml.Serialization.XmlSerializer serializer;
public SubscriptionManagerRP()
{
this.subscriptionPolicyField = new SubscriptionPolicyType();
this.filterField = new FilterType();
this.consumerReferenceField = new EndpointReferenceType();
}
[System.Runtime.Serialization.DataMemberAttribute()]
public EndpointReferenceType ConsumerReference
{
get
{
return this.consumerReferenceField;
}
set
{
this.consumerReferenceField = value;
}
}
[System.Runtime.Serialization.DataMemberAttribute()]
public FilterType Filter
{
get
{
return this.filterField;
}
set
{
this.filterField = value;
}
}
[System.Runtime.Serialization.DataMemberAttribute()]
public SubscriptionPolicyType SubscriptionPolicy
{
get
{
return this.subscriptionPolicyField;
}
set
{
this.subscriptionPolicyField = value;
}
}
[System.Runtime.Serialization.DataMemberAttribute()]
public System.DateTime CreationTime
{
get
{
return this.creationTimeField;
}
set
{
this.creationTimeField = value;
}
}
[System.Xml.Serialization.XmlIgnoreAttribute()]
[System.Runtime.Serialization.DataMemberAttribute()]
public bool CreationTimeSpecified
{
get
{
return this.creationTimeFieldSpecified;
}
set
{
this.creationTimeFieldSpecified = value;
}
}
private static System.Xml.Serialization.XmlSerializer Serializer
{
get
{
if ((serializer == null))
{
serializer = new System.Xml.Serialization.XmlSerializer(typeof(SubscriptionManagerRP));
}
return serializer;
}
}
#region Serialize/Deserialize
public virtual string WriteObject()
{
System.IO.StreamReader streamReader = null;
System.IO.MemoryStream memoryStream = null;
try
{
memoryStream = new System.IO.MemoryStream();
Serializer.Serialize(memoryStream, this);
memoryStream.Seek(0, System.IO.SeekOrigin.Begin);
streamReader = new System.IO.StreamReader(memoryStream);
return streamReader.ReadToEnd();
}
finally
{
if ((streamReader != null))
{
streamReader.Dispose();
}
if ((memoryStream != null))
{
memoryStream.Dispose();
}
}
}
public static bool ReadObject(string xml, out SubscriptionManagerRP obj, out System.Exception exception)
{
exception = null;
obj = default(SubscriptionManagerRP);
try
{
obj = ReadObject(xml);
return true;
}
catch (System.Exception ex)
{
exception = ex;
return false;
}
}
public static bool ReadObject(string xml, out SubscriptionManagerRP obj)
{
System.Exception exception = null;
return ReadObject(xml, out obj, out exception);
}
public static SubscriptionManagerRP ReadObject(string xml)
{
System.IO.StringReader stringReader = null;
try
{
stringReader = new System.IO.StringReader(xml);
return ((SubscriptionManagerRP)(Serializer.Deserialize(System.Xml.XmlReader.Create(stringReader))));
}
finally
{
if ((stringReader != null))
{
stringReader.Dispose();
}
}
}
public virtual bool SaveToFile(string fileName, out System.Exception exception)
{
exception = null;
try
{
SaveToFile(fileName);
return true;
}
catch (System.Exception e)
{
exception = e;
return false;
}
}
public virtual void SaveToFile(string fileName)
{
System.IO.StreamWriter streamWriter = null;
try
{
string xmlString = WriteObject();
System.IO.FileInfo xmlFile = new System.IO.FileInfo(fileName);
streamWriter = xmlFile.CreateText();
streamWriter.WriteLine(xmlString);
streamWriter.Close();
}
finally
{
if ((streamWriter != null))
{
streamWriter.Dispose();
}
}
}
public static bool LoadFromFile(string fileName, out SubscriptionManagerRP obj, out System.Exception exception)
{
exception = null;
obj = default(SubscriptionManagerRP);
try
{
obj = LoadFromFile(fileName);
return true;
}
catch (System.Exception ex)
{
exception = ex;
return false;
}
}
public static bool LoadFromFile(string fileName, out SubscriptionManagerRP obj)
{
System.Exception exception = null;
return LoadFromFile(fileName, out obj, out exception);
}
public static SubscriptionManagerRP LoadFromFile(string fileName)
{
System.IO.FileStream file = null;
System.IO.StreamReader sr = null;
try
{
file = new System.IO.FileStream(fileName, FileMode.Open, FileAccess.Read);
sr = new System.IO.StreamReader(file);
string xmlString = sr.ReadToEnd();
sr.Close();
file.Close();
return ReadObject(xmlString);
}
finally
{
if ((file != null))
{
file.Dispose();
}
if ((sr != null))
{
sr.Dispose();
}
}
}
#endregion
#region Clone method
public virtual SubscriptionManagerRP Clone()
{
return ((SubscriptionManagerRP)(this.MemberwiseClone()));
}
#endregion
}
The save method from the above class (looks similar in a simple test class that works in a test project):
public Boolean Save()
{
DatabaseAccess access = new DatabaseAccess();
var sessionFactory = access.getSessionFactory();
using (var session = sessionFactory.OpenSession())
{
using (var transaction = session.BeginTransaction())
{
SaveTextMess(this.ToString());
session.Save(this);
transaction.Commit();
return true;
}
}
return false;
}
Where I set the connection string and Factory session:
class SessionFactoryController
{
public SessionFactoryController()
{
}
public ISessionFactory GiveFactory()
{
return CreateSessionFactory();
}
private static void ReferByteCode(){
ProxyFactory fake = new ProxyFactory();
}
private static ISessionFactory CreateSessionFactory()
{
ReferByteCode();
var cfg = new FluentNhibernateConfiguration();
return Fluently.Configure()
.Database(
FluentNHibernate.Cfg.Db.MsSqlConfiguration.MsSql2005
.ConnectionString("[SERVER];Database=Pets;User ID=NHibernateTester;Password=[PASSWORD];Trusted_Connection=False;")
)
.Mappings(m =>
m.AutoMappings
.Add(AutoMap.AssemblyOf<SubscriptionManagerRP>(cfg))
)
.BuildSessionFactory();
}
}
Config:
class NotifyFluentNhibernateConfiguration : DefaultAutomappingConfiguration
{
public override bool ShouldMap(Type type)
{
return type.Namespace == "System.Xml.XmlAttribute";
}
}
The configuration is designed to handle an earlier matching exception for "System.Xml.XmlAttribute".
, ? , , , NHibernate Automapping ( ) ( , ). , "" .
.
, , .
(, , /) . , , , , , , -, .
, , . , .
, . , . , , . , , .
, ?