gpt4 book ai didi

nhibernate - 在 SharpArchitecture 中使用 Npgsql 时出现的问题

转载 作者:行者123 更新时间:2023-11-29 13:36:16 24 4
gpt4 key购买 nike

在 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)我的数据库域文件如下,一个表名为student(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 项目中,我创建了接口(interface) 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) 我创建了 View ,到目前为止一切正常,但在最后一步,项目显示如下错误。但是把项目改成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:

请帮帮我!我觉得自己没有最后的努力就功亏一篑。我很沮丧!

最佳答案

你看过this了吗? ?

将“hbm2ddl.keywords”、“none”添加到您的 Nhibernate.config,看看是否能解决您的问题。

关于nhibernate - 在 SharpArchitecture 中使用 Npgsql 时出现的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10407212/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com