2014-11-07 27 views
0

我看到如何使用映射文件爲我想要的任何nhibernate映射類指定自定義IUserType。我可以告訴nhibernate將System.Data.DbType完全交換爲自定義IUserType嗎?

但是,我不想每次都輸入它。有沒有辦法來覆蓋這裏看到的標準映射表? enter image description here

我有JørnSchou-Rode的IUserType實現,用於在MariaDB中的二進制文件(16)中存儲guid。我想要的只是輸入一行或兩行代碼,以便在看到System.Guid將其轉換爲Schou-Rode製作的自定義「BinaryGuidType」時告訴Nhibernate。可以做到嗎?

+0

我真的不需要這個答案了,我只是回去使用int標識符爲大多數事情現在。如果MariaDB有更好的支持,我會使用Guid。 – 2014-11-07 19:33:29

回答

1

如果你使用流利的NHibernate,你可以很容易地使用約定做到這一點。下面是我如何映射所有字符串爲varchar,而不是爲nvarchar:

public class PropertyConvention : IPropertyConvention 
{ 
    public void Apply(IPropertyInstance instance) 
    { 
     SetStringsAsAnsiStringByDefault(instance); 
    } 

    private void SetStringsAsAnsiStringByDefault(IPropertyInstance instance) 
    { 
     if (instance.Property.PropertyType == typeof(string)) 
     { 
      instance.CustomType("AnsiString"); 
     } 
     else if (instance.Property.PropertyType == typeof(char)) 
     { 
      instance.CustomType("AnsiChar"); 
     } 
    } 
} 

相信的NHibernate的後續版本已經內置了公約的支持,但文檔似乎是稀疏。這裏有一篇文章讓你開始,雖然:http://weblogs.asp.net/ricardoperes/nhibernate-conventions

+0

美麗,我不知道這些。 – 2014-11-10 07:45:23

相關問題