在SharpArchitecture中使用Npgsql时,是否有人可以帮助我。我很沮丧。我使用了Postgresql 8.4,Npgsql 2.0.11,SharpArchitecture 2.0.0.0和visual studio 2010。
我的示例项目名为“success”。
1)我在每个项目中引用Npgsql驱动程序,然后配置我的NHibernate.config,我觉得没有问题:
<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)我的数据库域文件如下,表名学生(sno,sname,sage),sno是PK和键入的字符串:
//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)为了使用非Id列表,我删除了默认的MyEntity1.cs,并修改了AutoPersistenceModelGenerator.cs,如下所示:
...
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)在Tasks项目中,我创建了接口IStudentRepository.cs和类StudentRepository.cs,修改了NHibernateRepository,以便通过“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)在MVC项目中,创建StudentController.cs并使用StudentRepository而不是NHibernateRepository,一些关键代码如下:
...
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)我创建了视图,直到现在都可以,但在最后一步,项目显示错误如下。但是,将项目更改为SQL Server 2005平台时没有问题。 错误出现在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"));
}
...
错误详情如下:
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:
请帮帮我!由于缺乏最后的努力,我感到失败了。我很沮丧!