if (attribute.MaximumLength != default(int)) instance.Length(attribute.MaximumLength); else instance.Length(4001); //force MS-SQL to varchar(MAX)
I have a separate reference convention where I also set the Foreign Key name: public class ReferenceConvention : IReferenceConvention { public void Apply(FNH.Conventions.Instances.IManyToOneInstance instance) { if (instance.Property.MemberInfo.IsDefined(typeof(RequiredAttribute), false)) instance.Not.Nullable(); instance.ForeignKey(string.Format("FK_{0}_{1}", instance.EntityType.Name, instance.Name)); } } On Tue, May 21, 2013 at 10:35 AM, Riderman Sousa < riderma...@bindsolution.com> wrote: > Very nice, did not know the property AttributePropertyConvention. Why you > not used in ColumnNullConvention? The same will apply to reference types? > > Another question, in ColumnLengthConvention the default is 4001 (this way > understands that the SQL type is varchar (max)). So even for those > properties that do not have the attribute StringLengthAttribute set, the > value should be 4001. How can I do this? > > > > My mapping is like this: > > m.AutoMappings.Add( > AutoMap.AssemblyOf<Entidade>(mapConfig) > .UseOverridesFromAssemblyOf<AudityConvention>() > .Conventions.AddFromAssemblyOf<EnumConvention>() > ); > > > Thanks. > > > On Tue, May 21, 2013 at 10:57 AM, Carl Bussema <carl.buss...@gmail.com>wrote: > >> using System.ComponentModel.DataAnnotations; >> using FluentNHibernate.Conventions; >> using FluentNHibernate.Conventions.Instances; >> >> namespace YourApp.Infrastructure.Conventions >> { >> public class ColumnNullConvention : IPropertyConvention >> { >> public void Apply(IPropertyInstance instance) >> { >> if >> (instance.Property.MemberInfo.IsDefined(typeof(RequiredAttribute), false)) >> instance.Not.Nullable(); >> } >> >> } >> } >> >> ---- >> >> using System.ComponentModel.DataAnnotations; >> using FluentNHibernate.Conventions; >> using FluentNHibernate.Conventions.Instances; >> >> namespace YourApp.Infrastructure.Conventions >> { >> public class ColumnLengthConvention : >> AttributePropertyConvention<StringLengthAttribute> >> { >> protected override void Apply(StringLengthAttribute attribute, >> IPropertyInstance instance) >> { >> // override the default column length >> if (attribute.MaximumLength != default(int)) >> instance.Length(attribute.MaximumLength); >> } >> } >> } >> >> >> ---------------- >> >> Make sure when you generate your mappings, you're actually adding all >> your conventions, something like >> >> mappings.Conventions.Setup(GetConventions()); >> >> private static Action<IConventionFinder> GetConventions() >> { >> return c => >> { >> c.AddFromAssemblyOf<ColumnNullConvention>(); >> }; >> } >> >> On Tue, May 21, 2013 at 9:46 AM, Riderman Sousa < >> riderma...@bindsolution.com> wrote: >> >>> Using DataAnnotations, I set my string properties with the length using >>> StringLength >>> attribute >>> >>> [StringLength(10)] >>> public virtual string Identificacao { get; set; } >>> >>> For this convection is applied to my database, I created a >>> StringLengthConventions see: http://chopapp.com/#jlu7vz5v >>> >>> Now I need to configure the properties as NotNull then follow the same >>> steps: >>> >>> 1. I added the attribute [Required] on properties. >>> 2. Create a RequiredConvention attribute, see: >>> http://chopapp.com/#4milknwk >>> >>> *Error* >>> >>> As you may have noticed, the conventions retrieve only the first >>> attribute. What generates an error in the generated schema. >>> The schema will follow the convention only the first attribute! >>> >>> *Question* >>> >>> The first step to fix the error is to create a test that fails. The >>> question is, how to create a test for conventions? >>> >>> Thanks, >>> >>> Riderman de Sousa Barbosa <http://about.me/ridermansb> >>> >>> Web Developer | MCPD Certify >>> >>> Skype.: 4042-6002 | Cel.: (31) 8681-1986 >>> bindsolution.com >>> >>> Microsoft Parner Network >>> >>> -- >>> You received this message because you are subscribed to the Google >>> Groups "Fluent NHibernate" group. >>> To unsubscribe from this group and stop receiving emails from it, send >>> an email to fluent-nhibernate+unsubscr...@googlegroups.com. >>> To post to this group, send email to fluent-nhibernate@googlegroups.com. >>> Visit this group at >>> http://groups.google.com/group/fluent-nhibernate?hl=en-US. >>> For more options, visit https://groups.google.com/groups/opt_out. >>> >>> >>> >> >> -- >> You received this message because you are subscribed to the Google Groups >> "Fluent NHibernate" group. >> To unsubscribe from this group and stop receiving emails from it, send an >> email to fluent-nhibernate+unsubscr...@googlegroups.com. >> To post to this group, send email to fluent-nhibernate@googlegroups.com. >> Visit this group at >> http://groups.google.com/group/fluent-nhibernate?hl=en-US. >> For more options, visit https://groups.google.com/groups/opt_out. >> >> >> > > -- > You received this message because you are subscribed to the Google Groups > "Fluent NHibernate" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to fluent-nhibernate+unsubscr...@googlegroups.com. > To post to this group, send email to fluent-nhibernate@googlegroups.com. > Visit this group at > http://groups.google.com/group/fluent-nhibernate?hl=en-US. > For more options, visit https://groups.google.com/groups/opt_out. > > > -- You received this message because you are subscribed to the Google Groups "Fluent NHibernate" group. To unsubscribe from this group and stop receiving emails from it, send an email to fluent-nhibernate+unsubscr...@googlegroups.com. To post to this group, send email to fluent-nhibernate@googlegroups.com. Visit this group at http://groups.google.com/group/fluent-nhibernate?hl=en-US. For more options, visit https://groups.google.com/groups/opt_out.