Validating DataAnnotations with Validator class

I found the answer here: http://forums.silverlight.net/forums/p/149264/377212.aspx MVC recognizes the MetaDataType attribute, but other projects do not. Before validating, you need to manually register the metadata class: TypeDescriptor.AddProviderTransparent( new AssociatedMetadataTypeTypeDescriptionProvider(typeof(Persona), typeof(Persona_Validation)), typeof(Persona)); ValidationContext context = new ValidationContext(p, null, null); List<ValidationResult> results = new List<ValidationResult>(); bool valid = Validator.TryValidateObject(p, context, results, true);

What is .Net Framework 4 extended?

Got this from Bing. Seems Microsoft has removed some features from the core framework and added it to a separate optional(?) framework component. To quote from MSDN (http://msdn.microsoft.com/en-us/library/cc656912.aspx) The .NET Framework 4 Client Profile does not include the following features. You must install the .NET Framework 4 to use these features in your application: * …

Read more

404 – A public action method X was not found on controller Y (ActionInvoker.InvokeAction returns false)

The problem is that you’re specifying both the HttpGet and HttpPost attributes. If you leave both of them off, the action accepts both POST and GET requests. My guess is that the HttpGet and HttpPost attributes don’t signal to MVC to allow the corresponding request type, but to deny the opposite type. So by including …

Read more

Performance concern: StringCollection vs List

I would personally prefer to use List<string>: No need to remember one specific type just for strings It implements the generic IEnumerable<T> rather than just IEnumerable, and thus supports LINQ It’s supported in SilverLight It’s more idiomatic for most developers (IMO) I would be really surprised to find StringCollection to be significantly faster than List<string> …

Read more

When KeyNotFoundException is thrown, how do I see which key wasn’t found?

Custom exception: class WellknownKeyNotFoundException : KeyNotFoundException { public WellknownKeyNotFoundException(object key, string message) : this(key, message, null) { } public WellknownKeyNotFoundException(object key, string message, Exception innerException) : base(message, innerException) { this.Key = key; } public object Key { get; private set; } } Handy extension method: public TValue GetValue<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key) { …

Read more

Mixed mode assembly in .NET 4

The best would probably be to recompile your class library for .NET 4.0 in Visual Studio 2010 (ie. opening up the project, converting it, and changing the target framework.) If you can’t, or won’t, do that, then you can try adding the following to your app.config file for your .NET 4.0 application: <startup useLegacyV2RuntimeActivationPolicy=”true”> <supportedRuntime …

Read more

Entity framework Include command – Left or inner join?

I know this is an old question, but if anyone else lands here using EF Code First as I am, my issue was in the fluent mappings: protected override void OnModelCreating(DbModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); modelBuilder.Entity<Parent>() .HasOptional(a => a.Child) /* LEFT OUTER JOIN */ .WithMany() .HasForeignKey(a => a.ChildId); } is translated as a LEFT OUTER JOIN …

Read more