0

저는 Nhibernate에서 작업 중이며 매핑 (CustomerMap 및 CustomerRoleMap)과 함께 Customer 및 CusotmerRole이라는 두 개의 클래스 이름을 사용합니다. 이제이 두 테이블을 As Customer_CustomerRole_Mapping이라는 데이터베이스에있는 세 번째 테이블과 매핑하려고합니다. 테이블 Customer_CustomerRole_Mapping에는 데이터베이스에 Customer_Id라는 두 개의 열이 있습니다 (기본 키뿐만 아니라 Customer 테이블의 ForeignKey와 CustomerRole_Id 또한 CustomerKey와 ForeignKey가있는 CustomerRole_Id입니다.) 그래서 고객과 Customer_CustomerRole_Mapping을 어떻게 매핑 할 수 있는지 알고 싶습니다 . 그리고 CustomerRole 또한 나는이 또 다른지도를 만들 필요가 있음을 요청해야 또는 나는이 기존의 클래스를 매핑 할 수 있습니다 감사합니다 사전에세 번째 테이블이있는 두 개의 테이블 매핑 Fluent Nhibernate

코드 :..

Customer.Cs :

public class Customer : BaseEntity 
{ 
    private ICollection<ExternalAuthenticationRecord> _externalAuthenticationRecords; 
    private ICollection<CustomerRole> _customerRoles; 
    private ICollection<ShoppingCartItem> _shoppingCartItems; 
    private ICollection<RewardPointsHistory> _rewardPointsHistory; 
    private ICollection<ReturnRequest> _returnRequests; 
    private ICollection<Address> _addresses; 

    /// <summary> 
    /// Ctor 
    /// </summary> 
    public Customer() 
    { 
     this.CustomerGuid = Guid.NewGuid(); 
     this.PasswordFormat = PasswordFormat.Clear; 
    } 

    /// <summary> 
    /// Gets or sets the customer Guid 
    /// </summary> 
    public virtual Guid CustomerGuid { get; set; } 

    /// <summary> 
    /// Gets or sets the username 
    /// </summary> 
    public virtual string Username { get; set; } 
    /// <summary> 
    /// Gets or sets the email 
    /// </summary> 
    public virtual string Email { get; set; } 
    /// <summary> 
    /// Gets or sets the password 
    /// </summary> 
    public virtual string Password { get; set; } 

    /// <summary> 
    /// Gets or sets the password format 
    /// </summary> 
    public virtual int PasswordFormatId { get; set; } 
    /// <summary> 
    /// Gets or sets the password format 
    /// </summary> 
    public virtual PasswordFormat PasswordFormat 
    { 
     get { return (PasswordFormat)PasswordFormatId; } 
     set { this.PasswordFormatId = (int)value; } 
    } 
    /// <summary> 
    /// Gets or sets the password salt 
    /// </summary> 
    public virtual string PasswordSalt { get; set; } 

    /// <summary> 
    /// Gets or sets the admin comment 
    /// </summary> 
    public virtual string AdminComment { get; set; } 

    /// <summary> 
    /// Gets or sets a value indicating whether the customer is tax exempt 
    /// </summary> 
    public virtual bool IsTaxExempt { get; set; } 

    /// <summary> 
    /// Gets or sets the affiliate identifier 
    /// </summary> 
    public virtual int AffiliateId { get; set; } 

    /// <summary> 
    /// Gets or sets the vendor identifier with which this customer is associated (maganer) 
    /// </summary> 
    public virtual int VendorId { get; set; } 

    /// <summary> 
    /// Gets or sets a value indicating whether the customer is active 
    /// </summary> 
    public virtual bool Active { get; set; } 

    /// <summary> 
    /// Gets or sets a value indicating whether the customer has been deleted 
    /// </summary> 
    public virtual bool Deleted { get; set; } 

    /// <summary> 
    /// Gets or sets a value indicating whether the customer account is system 
    /// </summary> 
    public virtual bool IsSystemAccount { get; set; } 

    /// <summary> 
    /// Gets or sets the customer system name 
    /// </summary> 
    public virtual string SystemName { get; set; } 

    /// <summary> 
    /// Gets or sets the last IP address 
    /// </summary> 
    public virtual string LastIpAddress { get; set; } 

    /// <summary> 
    /// Gets or sets the date and time of entity creation 
    /// </summary> 
    public virtual DateTime CreatedOnUtc { get; set; } 

    /// <summary> 
    /// Gets or sets the date and time of last login 
    /// </summary> 
    public virtual DateTime? LastLoginDateUtc { get; set; } 

    /// <summary> 
    /// Gets or sets the date and time of last activity 
    /// </summary> 
    public virtual DateTime LastActivityDateUtc { get; set; } 

    #region Navigation properties 

    /// <summary> 
    /// Gets or sets customer generated content 
    /// </summary> 
    public virtual ICollection<ExternalAuthenticationRecord> ExternalAuthenticationRecords 
    { 
     get { return _externalAuthenticationRecords ?? (_externalAuthenticationRecords = new List<ExternalAuthenticationRecord>()); } 
     protected set { _externalAuthenticationRecords = value; } 
    } 

    /// <summary> 
    /// Gets or sets the customer roles 
    /// </summary> 
    public virtual ICollection<CustomerRole> CustomerRoles 
    { 
     get { return _customerRoles ?? (_customerRoles = new List<CustomerRole>()); } 
     protected set { _customerRoles = value; } 
    } 

    /// <summary> 
    /// Gets or sets shopping cart items 
    /// </summary> 
    public virtual ICollection<ShoppingCartItem> ShoppingCartItems 
    { 
     get { return _shoppingCartItems ?? (_shoppingCartItems = new List<ShoppingCartItem>()); } 
     protected set { _shoppingCartItems = value; } 
    } 

    /// <summary> 
    /// Gets or sets reward points history 
    /// </summary> 
    public virtual ICollection<RewardPointsHistory> RewardPointsHistory 
    { 
     get { return _rewardPointsHistory ?? (_rewardPointsHistory = new List<RewardPointsHistory>()); } 
     protected set { _rewardPointsHistory = value; } 
    } 

    /// <summary> 
    /// Gets or sets return request of this customer 
    /// </summary> 
    public virtual ICollection<ReturnRequest> ReturnRequests 
    { 
     get { return _returnRequests ?? (_returnRequests = new List<ReturnRequest>()); } 
     protected set { _returnRequests = value; } 
    } 

    /// <summary> 
    /// Default billing address 
    /// </summary> 
    public virtual Address BillingAddress { get; set; } 

    /// <summary> 
    /// Default shipping address 
    /// </summary> 
    public virtual Address ShippingAddress { get; set; } 

    /// <summary> 
    /// Gets or sets customer addresses 
    /// </summary> 
    public virtual ICollection<Address> Addresses 
    { 
     get { return _addresses ?? (_addresses = new List<Address>()); } 
     protected set { _addresses = value; } 
    } 

    #endregion 
} 

CustomerRole.cs :

public partial class CustomerRole : BaseEntity 
    { 
     private ICollection<PermissionRecord> _permissionRecords; 
     /// <summary> 
     /// Gets or sets the customer role name 
     /// </summary> 
     public virtual string Name { get; set; } 

     /// <summary> 
     /// Gets or sets a value indicating whether the customer role is marked as free shiping 
     /// </summary> 
     public virtual bool FreeShipping { get; set; } 

     /// <summary> 
     /// Gets or sets a value indicating whether the customer role is marked as tax exempt 
     /// </summary> 
     public virtual bool TaxExempt { get; set; } 

     /// <summary> 
     /// Gets or sets a value indicating whether the customer role is active 
     /// </summary> 
     public virtual bool Active { get; set; } 

     /// <summary> 
     /// Gets or sets a value indicating whether the customer role is system 
     /// </summary> 
     public virtual bool IsSystemRole { get; set; } 

     /// <summary> 
     /// Gets or sets the customer role system name 
     /// </summary> 
     public virtual string SystemName { get; set; } 

     /// <summary> 
     /// Gets or sets a product identifier that is required by this customer role. 
     /// A customer is added to this customer role once a specified product is purchased. 
     /// </summary> 
     public virtual int PurchasedWithProductId { get; set; } 

     /// <summary> 
     /// Gets or sets the permission records 
     /// </summary> 
     public virtual ICollection<PermissionRecord> PermissionRecords 
     { 
      get { return _permissionRecords ?? (_permissionRecords = new List<PermissionRecord>()); } 
      protected set { _permissionRecords = value; } 
     } 
    } 

CustomerMap.cs :

public class CustomerMap : ClassMap<Customer> 
    { 
     public CustomerMap() 
     { 
      Table("Customer"); 
      LazyLoad(); 
      Id(x => x.Id).GeneratedBy.Identity().Column("Id");     
      Map(x => x.CustomerGuid).Column("CustomerGuid").Not.Nullable(); 
      Map(x => x.Username).Column("Username").Length(1000); 
      Map(x => x.Email).Column("Email").Length(1000); 
      Map(x => x.Password).Column("Password"); 
      Map(x => x.PasswordFormatId).Column("PasswordFormatId").Not.Nullable().Precision(10); 
      Map(x => x.PasswordSalt).Column("PasswordSalt"); 
      Map(x => x.AdminComment).Column("AdminComment"); 
      Map(x => x.IsTaxExempt).Column("IsTaxExempt").Not.Nullable(); 
      Map(x => x.AffiliateId).Column("AffiliateId").Not.Nullable(); 
      Map(x => x.VendorId).Column("VendorId").Not.Nullable(); 
      Map(x => x.Active).Column("Active").Not.Nullable(); 
      Map(x => x.Deleted).Column("Deleted").Not.Nullable(); 
      Map(x => x.IsSystemAccount).Column("IsSystemAccount").Not.Nullable(); 
      Map(x => x.SystemName); 
      Map(x => x.LastIpAddress); 
      Map(x => x.CreatedOnUtc).Column("CreatedOnUtc").Not.Nullable(); 
      Map(x => x.LastLoginDateUtc).Column("LastLoginDateUtc"); 
      Map(x => x.LastActivityDateUtc).Column("LastActivityDateUtc"); 
      References(x => x.BillingAddress).Column("BillingAddress_Id"); 
      References(x => x.ShippingAddress).Column("ShippingAddress_Id"); 

     } 


    } 

CustomerRoleMap.cs :

public class CustomerRoleMap : ClassMap<CustomerRole> 
    { 
     public CustomerRoleMap() 
     { 
      Table("CustomerRole"); 
      LazyLoad(); 
      Id(x => x.Id).GeneratedBy.Identity().Column("Id"); 
      Map(x => x.Name).Column("Name").Not.Nullable().Length(255); 
      Map(x => x.FreeShipping).Column("FreeShipping").Not.Nullable(); 
      Map(x => x.TaxExempt).Column("TaxExempt").Not.Nullable(); 
      Map(x => x.Active).Column("Active").Not.Nullable(); 
      Map(x => x.IsSystemRole).Column("IsSystemRole").Not.Nullable(); 
      Map(x => x.SystemName).Column("SystemName").Length(255); 
      Map(x => x.PurchasedWithProductId).Column("PurchasedWithProductId").Not.Nullable().Precision(10); 

      HasMany<CustomerRole>(x => x.Id).Table("Customer_CustomerRole_Mapping").KeyColumn("CustomerRole_Id"); 
     } 
    } 

답변

0

만 ". Customer_CustomerRole_Mapping"두 FK의 테이블이있는 경우, 당신은 ~ 별도로 필요하지 않습니다 이 테이블에 대한 매핑.

"Customer_CustomerRole_Mapping." 자체 SurrogateKey 또는 두 개의 FK 이외의 다른 컬럼을 가지고 있다면 ... 매핑 (그리고이 매핑을위한 poco/pojo 클래스)이 필요합니다.

고객 매핑

 /* the below is how to do it without a surogatekey on the link table */ 
     HasManyToMany<CustRoleNHEntity>(x => x.CustRoles) 
     .Table("Customer_CustomerRole_Mapping") 
      .ParentKeyColumns.Add("AbcCustomerUUID", p => p.UniqueKey("Emp_CustRole_Unique").Index("IX_ABC123")) 
      .ChildKeyColumns.Add("AbcCustRoleUUID", p => p.UniqueKey("Emp_CustRole_Unique")) 
      .Cascade.None() 
      ; 

CustRole 매핑

 /* the below is how to do it without a surogatekey on the link table */ 
     HasManyToMany<CustomerNHEntity>(x => x.MyCustomers) /* I think this is missing on the POCO */ 
     .Table("Customer_CustomerRole_Mapping") 
      .ParentKeyColumns.Add("AbcCustRoleUUID", p => p.UniqueKey("Emp_CustRole_Unique").Index("IX_ABC123")) 
      .ChildKeyColumns.Add("AbcCustomerUUID", p => p.UniqueKey("Emp_CustRole_Unique")) 
      .Cascade.None() 
      ; 

당신은 내가 CustRole 매핑에서 "MyCustomers"를 부르는 CustomerRole의 POCO/POJO에 고객의 ICollection이 필요합니다.

+0

답장을 보내 주셔서 감사합니다. 우리가 가지고있는 고유 한 핵심 가치를 설명해 주시겠습니까? 또한 우리 모델에 따라 취해야하는 인덱스 속성입니다. 두 매핑 (CustomerMap 및 CustomerRoleMap)에서 다음과 같은 점에 대해 알려주지 않습니다. 1) p => p.UniqueKey ("Emp_CustRole_Unique") 2) 색인 ("IX_ABC123"). –

+0

도와 주실 수 있나요? 그래서 나는이 일을 더 할 수있다. –