OverrideAll nHibernate Fluent无效

时间:2015-10-18 12:19:36

标签: nhibernate fluent-nhibernate

我正在尝试使用nHibernate设置我的第一个项目。我使用Fluent语法来执行此操作。这是我的Contacts.cs文件:

using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Xml.Serialization;

namespace MVCTest.Repository.Entities
{
    public partial class Contact : IEntity
    {
        #region .ctor
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
        public Contact()
        {
            this.PhoneNumbers = new HashSet<PhoneNumber>();
        }

        #endregion

        #region Database Columns

        [JsonProperty(PropertyName = "id")]
        public virtual int Id { get; set; }

        [JsonProperty(PropertyName = "firstName")]
        public virtual string FirstName { get; set; }

        [JsonProperty(PropertyName = "lastName")]
        public virtual string LastName { get; set; }

        [JsonProperty(PropertyName = "address")]
        public virtual string Address { get; set; }

        [JsonProperty(PropertyName = "city")]
        public virtual string City { get; set; }

        [JsonProperty(PropertyName = "state")]
        public virtual string State { get; set; }

        [JsonProperty(PropertyName = "zip")]
        public virtual string Zip { get; set; }

        #endregion

        #region Foreign Keys

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        [JsonIgnore]
        [XmlIgnore]
        public virtual ICollection<PhoneNumber> PhoneNumbers { get; set; }

        #endregion

        #region Non-Db Properties
        public string IgnoreTestField { get; set; }

        [JsonProperty(PropertyName = "homePhone")]
        [NotMapped]
        public string HomePhone { get; set; }

        [JsonProperty(PropertyName = "workPhone")]
        [NotMapped]
        public string WorkPhone { get; set; }

        [JsonProperty(PropertyName = "cellPhone")]
        [NotMapped]
        public string CellPhone { get; set; }

        #endregion

        #region Helper Methods
        public Contact PhoneCollectionToFlatObject()
        {
            return new Contact();
        }

        public List<PhoneNumber> FlatObjectToPhoneCollection()
        {
            return new List<PhoneNumber>();
        }
        private PhoneNumber CreatePhoneNumber(string phoneNumber, int type)
        {
            return new PhoneNumber()
            {
                ContactId = this.Id,
                PhoneTypeId = type,
                Number = phoneNumber
            };
        }

        #endregion
    }
}

如您所见,我有3个属性,我想忽略HomePhone,CellPhone和WorkPhone。我添加了一个IgnoreTestField属性来测试它。

     static ISessionFactory ConfigureSql()
            {
                var sessionFactory = Fluently.Configure()
                    .Database(MsSqlConfiguration.MsSql2008
                      .ConnectionString(conString => conString.FromConnectionStringWithKey("TrainingEntities"))
                      .ShowSql())
                      .Mappings(x => x.AutoMappings.Add(AutoMap.AssemblyOf<Contact>(new AutomappingConfiguration())
                      .OverrideAll(p =>
                      {
                          //p.SkipProperty(typeof(NotMapped));
                          p.IgnoreProperties(iProp => iProp.Name.Equals("IgnoreTestField"));
                      })))

                  //.OverrideAll(p => p.IgnoreProperties(ShouldIgnoreMember))))
                  //.UseOverridesFromAssemblyOf<ContactOverrides>()))

                  .ExposeConfiguration(config => new SchemaUpdate(config).Execute(false, true))
                  .BuildSessionFactory();

                //.Mappings(m =>
                //          m.FluentMappings
                //              .AddFromAssemblyOf<Program>())
                //.ExposeConfiguration(cfg => new SchemaExport(cfg)
                //                                .Create(true, true))
                //.BuildSessionFactory();

                return sessionFactory;
            }

虽然我无法忽视该财产。我已经尝试了各种方法,比如创建一个扩展方法来跳过NotMapped属性的所有属性,如下所示,但这甚至都不起作用

    public static IPropertyIgnorer SkipProperty(this IPropertyIgnorer p, Type propertyType)
            {
                var test = p.IgnoreProperties(x => x.MemberInfo.GetCustomAttributes(propertyType, false).Length > 0);
                return test;
            }

还尝试使用.UseOverridesFromAssemblyOf()和以下代码:

    public void Override(AutoMapping<Contact> mapping)
    {
        mapping.Map(p => p.HomePhone);
        mapping.IgnoreProperty(p => p.IgnoreTestField);
        mapping.IgnoreProperty(p => p.HomePhone);
        mapping.IgnoreProperty(p => p.CellPhone);
        mapping.IgnoreProperty(p => p.WorkPhone);
        //mapping(p => p.PhoneCollectionToFlatObject);

    }

你能说出我做错了吗?

另外,我想忽略方法PhoneCollectionToFlatObject,FlatObjectToPhoneCollection和CreatePhoneNumber。我怎么能这样做?

我正在使用nHibernate版本:4.0.0.4000,NHibernate.Validator版本:1.3.2.4000,FluentNHibernate版本:2.0.3.0与.Net Framework 4.5.2

0 个答案:

没有答案