在SharpArchitecture中使用Npgsql时,是否有人可以帮助我。我使用了Postgresql 8.4,Npgsql 2.0.11,SharpArchitecture 2.0.0.0和Visual Studio 2010。
我的示例项目名为"成功"。
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(在任务项目中,我创建了接口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:projectsNpgsql2srcNpgsqlNpgsqlConnection.cs:line 970
at Npgsql.NpgsqlConnection.GetSchema(String collectionName) in C:projectsNpgsql2srcNpgsqlNpgsqlConnection.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:
请帮帮我!我觉得由于缺乏最后的努力,我无法成功。我很沮丧!
你看过这个吗?
将"hbm2ddl.keywords
","none
"添加到您的Nhibernate.config
中,看看是否可以解决您的问题。