2016-11-01 24 views
0

我有有許多角色nPoco V3 - 多對多不工作

public class User 
{ 
    public int Id {get;set;} 
    public string Name {get;set;} 
    public List<Role> Roles {get;set;} 
} 

public class Roles 
{ 
    public int Id {get;set;} 
    public string Key{get;set;} 
} 

public class UserRoles 
{ 
    public int UserId {get;set;} 
    public int RoleId {get;set;} 
} 

什麼,我儘量做到越來越及其所有角色的用戶在一個查詢,但到目前爲止,我失敗了用戶。 映射我使用一個自定義Conventionbased映射器(我可以提供的代碼,但它是相當大的)

我試圖FetchOneToMany,我試圖抓取這裏所描述

https://github.com/schotime/NPoco/wiki/One-to-Many-Query-Helpers https://github.com/schotime/NPoco/wiki/Version-3

但角色總是空。 角色和用戶本身的正確映射,我也嘗試指定像

For<User>().Columns(x => 
     { 
      x.Many(c => c.Roles); 
      x.Column(c => c.Roles).ComplexMapping(); 
     }, true); 

的關係同樣也沒有幫助,角色是空的。

我不知道我在想什麼。 任何想法?

回答

0

ComplexMapping和關係映射(1對n,n對n)是兩個不同的事情。

ComplexMapping用於映射通常駐留在存在一對一關係的同一表中的數據的嵌套對象。對於這樣的事情:

public class Client 
{ 
    public int Id { get; set; } 
    public string FirstName { get; set; } 
    public string LastName { get; set; } 
    public Address Address { get; set; } 

    public Client() 
    { 
     Address = new Address(); 
    } 
} 

public class Address 
{ 
    public string Street { get; set; } 
    public string City { get; set; } 
    public string PostalCode { get; set; } 
    public string Telephone { get; set; } 
    public string Country{ get; set; } 
} 

如果您使用的是基於常規映射器覆蓋時會是這個樣子:

For<Client>().Columns(x => 
{ 
    x.Column(y => y.Address).ComplexMapping(); 
}); 

有一點需要注意使用基於約定映射器時;您必須啓用ComplexMapping在掃描儀用下面的代碼:

scanner.Columns.ComplexPropertiesWhere(y => ColumnInfo.FromMemberInfo(y).ComplexMapping); 

否則ComplexMapping()調用您的重寫將完全忽略。

一個一對多的映射是這樣的工作(見NPoco on Github更多):

For<One>() 
    .TableName("Ones") 
    .PrimaryKey(x => x.OneId) 
    .Columns(x => 
    { 
     x.Column(y => y.OneId); 
     x.Column(y => y.Name); 
     x.Many(y => y.Items).WithName("OneId").Reference(y => y.OneId); 
    }, true); 

For<Many>() 
    .TableName("Manys") 
    .PrimaryKey(x => x.ManyId) 
    .Columns(x => 
    { 
     x.Column(y => y.ManyId); 
     x.Column(y => y.Value); 
     x.Column(y => y.Currency); 
     x.Column(y => y.OneId); 
     x.Column(y => y.One).WithName("OneId").Reference(y => y.OneId, ReferenceType.OneToOne); 
    }, true);