2012-05-02 4 views
1

SharpArchitecture에서 Npgsql을 사용할 때 도움이되는 사람이 있습니까? 좌절했습니다. PostgreSQL 8.4, Npgsql 2.0.11, SharpArchitecture 2.0.0.0 및 Visual Studio 2010을 사용했습니다.SharpArchitecture에서 Npgsql을 사용할 때의 문제

"success"라는 샘플 프로젝트가 있습니다.

<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) 다음으로 내 데이터베이스 도메인 파일, 테이블 이름의 학생 :

1) 나는 모든 프로젝트에 Npgsql 드라이버를 참조하고, 그 다음으로 내 NHibernate.config을 config (설정), 나는 아무 문제가없는 느낌 다음으로

//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) 위해, 나는 기본 MyEntity1.cs, 수정 AutoPersistenceModelGenerator.cs 삭제가 아닌 ID 열 테이블을 사용하는 : (SNO, SNAME, 세이지), SNO는 PK와 입력 된 문자열입니다

... 
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 프로젝트에서 "sno"로 레코드를 채우기 위해 인터페이스 IStudentRepository.cs와 StudentRepository.cs를 수정하여 NHibernateRepository를 수정했습니다.

//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; 
    } 
} 
} 

MVC 프로젝트에서 StudentController.cs 대신 NHibernateRepository의 사용 StudentRepository, 다음과 같이 몇 가지 핵심 코드 작성 5) : 내가보기를 생성하고 지금까지 전부

... 
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: 

저를 도와주세요! 최종 노력이 부족하여 성공에 미치지 못했습니다. 나는 좌절했다!

답변

1

this을 보았습니까?

Nhibernate.config에 "hbm2ddl.keywords", "none"을 추가하고 문제가 해결되는지 확인하십시오.

관련 문제