Reputation: 19
Is there some guy can help me when using Npgsql in SharpArchitecture.I'm frustrated. I used Postgresql 8.4, Npgsql 2.0.11, SharpArchitecture 2.0.0.0, and visual studio 2010.
My sample project named "success".
1)I refer Npgsql driver in every project, and config my NHibernate.config as followed, I felt there is no problem:
<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
<property name="connection.driver_class">NHibernate.Driver.NpgsqlDriver</property>
<property name="dialect">NHibernate.Dialect.PostgreSQLDialect</property>
<property name="connection.connection_string">
Server=localhost;Database=***;Encoding=UNICODE;User ID=***;Password=***;
</property>
2)My database domain files as followed, a table name student(sno, sname, sage), sno is PK and typed string :
//student.cs
using System.Collections.Generic;
using SharpArch.Domain.DomainModel;
namespace success.Domain {
public class student : Entity
{
public student() { }
public virtual string sno { get; set; }
public virtual System.Nullable<int> sage { get; set; }
public virtual string sname { get; set; }
}
}
//studentMap.cs
using FluentNHibernate.Automapping.Alterations;
namespace success.Domain {
public class studentMap : IAutoMappingOverride<student>
{
public void Override(FluentNHibernate.Automapping.AutoMapping<student> mapping)
{
mapping. Table("student");
mapping.LazyLoad();
mapping.Id(x => x.sno).GeneratedBy.Assigned().Column("sno");
//I don't used Id but sno as the PK, and sno is typed string.
mapping.Map(x => x.sage).Column("sage");
mapping.Map(x => x.sname).Column("sname");
}
}
}
3)In order to using non Id column table, I delete the default MyEntity1.cs, and modified AutoPersistenceModelGenerator.cs as followed:
...
public AutoPersistenceModel Generate()
{
var mappings = AutoMap.AssemblyOf<studentMap>(new AutomappingConfiguration())
.UseOverridesFromAssemblyOf<studentMap>() //alter AutoMapping Assembly
.OverrideAll(map => { map.IgnoreProperty("Id"); }) // ignore id property, and the program recognize the "sno" as the PK, test OK
...
4)In Tasks project, I create interface IStudentRepository.cs and class StudentRepository.cs, modifed NHibernateRepository, in order to fatch record by "sno".
//IStudentRepository.cs
using success.Domain;
using SharpArch.NHibernate.Contracts.Repositories;
namespace success.Tasks.IRepository
{
public interface IStudentRepository:INHibernateRepository<student>
{
student GetStudentFromSno(string sno);
}
}
//StudentRepository.cs
using SharpArch.NHibernate;
using success.Domain;
using success.Tasks.IRepository;
using NHibernate;
using NHibernate.Criterion;
namespace success.Tasks.Repository
{
public class StudentRepository:NHibernateRepository<student>,IStudentRepository
{
public student GetStudentFromSno(string sno) //define a new method to fatch record by sno
{
ICriteria criteria = Session.CreateCriteria<student>()
.Add(Expression.Eq("sno", sno));
return criteria.UniqueResult() as student;
}
}
}
5)In MVC project, create StudentController.cs and using StudentRepository instead of NHibernateRepository, some key codes as followed:
...
public ActionResult Index()
{
var students = this.studentRepository.GetAll();
return View(students);
}
private readonly StudentRepository studentRepository;
public StudentsController(StudentRepository studentRepository)
{
this.studentRepository = studentRepository;
}
[Transaction]
[HttpGet]
public ActionResult CreateOrUpdate(string sno)
{
student s = studentRepository.GetStudentFromSno(sno);
return View(s);
}
[Transaction]
[ValidateAntiForgeryToken]
[HttpPost]
public ActionResult CreateOrUpdate(student s)
{
if (ModelState.IsValid && s.IsValid())
{
studentRepository.SaveOrUpdate(s);
return this.RedirectToAction("Index");
}
return View(s);
}
[Transaction]
[ValidateAntiForgeryToken]
[HttpPost]
public ActionResult Delete(string sno)
{
var s = studentRepository.GetStudentFromSno(sno);
if (s == null)
return HttpNotFound();
studentRepository.Delete(s);
return this.RedirectToAction("Index");
}
...
6) I created view and that's all right until now, but at the last step, the project show error as followed. However, there is no problem when change the project to SQL Server 2005 platform. The error appeared at Global.asax.cs:
...
private void InitialiseNHibernateSessions()
{
NHibernateSession.ConfigurationCache = new NHibernateConfigurationFileCache();
//the follow line codes make error when using Npgsql, but no error when using SQL Server 2005
NHibernateSession.Init(
this.webSessionStorage,
new[] { Server.MapPath("~/bin/success.Infrastructure.dll") },
new AutoPersistenceModelGenerator().Generate(),
Server.MapPath("~/NHibernate.config"));
}
...
the error details as followed:
System.NotSupportedException was unhandled by user code
Message=Specified method is not supported.
Source=Npgsql
StackTrace:
at Npgsql.NpgsqlConnection.GetSchema(String collectionName, String[] restrictions) in C:\projects\Npgsql2\src\Npgsql\NpgsqlConnection.cs:line 970
at Npgsql.NpgsqlConnection.GetSchema(String collectionName) in C:\projects\Npgsql2\src\Npgsql\NpgsqlConnection.cs:line 946
at NHibernate.Dialect.Schema.AbstractDataBaseSchema.GetReservedWords()
at NHibernate.Tool.hbm2ddl.SchemaMetadataUpdater.GetReservedWords(Dialect dialect, IConnectionHelper connectionHelper)
at NHibernate.Tool.hbm2ddl.SchemaMetadataUpdater.Update(ISessionFactory sessionFactory)
at NHibernate.Impl.SessionFactoryImpl..ctor(Configuration cfg, IMapping mapping, Settings settings, EventListeners listeners)
InnerException:
please help me! I felt to fall short of success for lack of a final effort. I'm frustrated!
Upvotes: 1
Views: 1275