0

내 머리에서 불에 난 회로의 뚜렷한 냄새가 있습니다. 내 무지를 용서하십시오.S # arp에서 일대일 매핑

저는 S # arp Architecture에서 일대일 관계를 설정하려고합니다. (물론 Automapper로 해보십시오).

나는 내가 다음과 같은 오류

NHibernate.ADOException을 얻을 MappingIntegrationTests의 단위 테스트 CanConfirmDatabaseMatchesMappings를 실행하면

public class User : Entity 
{ 
    public virtual Profile Profile { get; set; } 
    public virtual Basket Basket { get; set; } 
    public virtual IList<Order> Orders { get; set; } 
    public virtual IList<Role> Roles { get; set; } 
    ... 
} 

public class Basket : Entity 
{ 
    public virtual User User { get; set; } 
    ... 
} 

public class Profile : Entity 
{ 
    public virtual User User { get; set; } 
    ... 
} 

그리고 내 DB 스키마가

CREATE TABLE [dbo].[Users](
    [Id] [int] IDENTITY(1,1) NOT NULL, 
    ... 

CREATE TABLE [dbo].[Profiles](
    [Id] [int] IDENTITY(1,1) NOT NULL, 
    [UserFk] [int] NULL, 
    ... 

CREATE TABLE [dbo].[Baskets](
    [Id] [int] IDENTITY(1,1) NOT NULL, 
    [UserFk] [int] NULL, 
    ... 

입니다 있습니다 : 수 아니 쿼리를 실행하십시오 ... System.D ata.SqlClient.SqlException : 'ProfileFk'열 이름이 유효하지 않습니다. 'BasketFk'열 이름이 잘못되었습니다.

하고 실행하려고는 SQL은

SELECT TOP 0 
    this_.Id AS Id6_1_ , 
    .. 
    user2_.ProfileFk AS ProfileFk9_0_ , 
    user2_.BasketFk AS BasketFk9_0_ 
FROM 
    Profiles this_ 
    LEFT OUTER JOIN Users user2_ 
     ON this_.UserFk = user2_.Id 

그래서는 사용자 테이블에 ProfileFk 및 BasketFk 현장을 찾고있다. 나는 고객 오버라이드 매핑을 설정하지 않았고 S #에서 기본 규칙 설정을 따랐습니다.

IList Orders 및 Roles에 대한 두 개의 다른 매핑이 잘 매핑 된 것 같습니다. 그래서 나는 일대일 관계를 설정하기 위해 뭔가를 놓쳤다 고 생각합니다.

무엇이 누락 되었습니까?

답변

0

알 수 있습니다. 이것은 실제로 Fluent NHibernate 문법으로 해결하기위한 NHibernate 문제이지만 S #와 관련이 있습니다.

배경 읽기 : NHibernate MappingFluent NHibernate HasOne

당신이 할 것은 사용자에 대해 매핑을 무시하고 두 .HasOne 매핑을 제공합니다. 그런 다음 프로필 및 바구니 클래스의 사용자에 대한 유일한 참조 설정 : 보조 노트로

public class UserMap : IAutoMappingOverride<User> 
    { 
     #region Implementation of IAutoMappingOverride<User> 
     /// <summary> 
     /// Alter the automapping for this type 
     /// </summary> 
     /// <param name="mapping">Automapping</param> 
     public void Override(AutoMapping<User> mapping) 
     { 
      mapping.HasOne(u => u.Profile); 
      mapping.HasOne(u => u.Basket); 
     } 
     #endregion 
    } 

public class ProfileMap : IAutoMappingOverride<Profile> 
    { 
     #region Implementation of IAutoMappingOverride<Profile> 
     /// <summary> 
     /// Alter the automapping for this type 
     /// </summary> 
     /// <param name="mapping">Automapping</param> 
     public void Override(AutoMapping<Profile> mapping) 
     { 
      mapping.References(p => p.User).Unique().Column("UserFk"); 
     } 
     #endregion 
    } 

public class BasketMap : IAutoMappingOverride<Basket> 
    { 
     #region Implementation of IAutoMappingOverride<Basket> 
     /// <summary> 
     /// Alter the automapping for this type 
     /// </summary> 
     /// <param name="mapping">Automapping</param> 
     public void Override(AutoMapping<Basket> mapping) 
     { 
      mapping.References(b => b.User).Unique().Column("UserFk"); 
     } 
     #endregion 
    } 

,이 글을 쓰는 시점을, NHibernate에 3 만 출시되었습니다. 내가 방금 구입 한 NHibernate 3.0 Cookbook이라고하는 훌륭한 책이 있으며 S #으로 작업 할 때 매우 유용합니다.

+0

... 그리고 나는 아마도 당신이 .HasOne 매핑을 필요로하지 않는다고 느끼고 있습니다. (실제로 이것들은 다 대일 관계이기 때문입니다) – autonomatt

관련 문제