Quantcast
Channel: Windows Forms Data Controls and Databinding forum
Viewing all articles
Browse latest Browse all 2535

Code generation for property 'DataSource' failed.

$
0
0

Greetings all,

I'm building a WinForms app using VS 2015 Update 2 with EF6 database first edmx and buddy classes for IValidateableObject and MetadataType data annotations. It's a three tier app with forms, data model, and business logic in separate projects and namespaces.

This has been working for a couple of weeks on this form but I just started running into an error when trying to add in another bindingSource. I drag in the bindingSource and connect the DataSource to DataModel.Address and try to save the form and I get this error

"Code generation for property 'DataSource' failed. Error was: 'The associated metadata type for type 'System.RuntimeType' contains the following unknown properties or fields: AddressId, Line1, Line2.... "

The error lists the names of properties in the metadata class for the address entity but it seems to be attempting to assign it to "System.RuntimeType". Why would it be doing that? It's not limited to a single entity type, at this point any entity type that I try to use will result in the same error. The fields listed in the dialog are always those fields listed in the metadatatype class. 

Here are excerpt from the code: I modified the t4 template to generate this static constructor for registering the metadatatype


public partial class Address
{
// Added section to register the metadatatype so that WinForms validation
    // Done in static constructor to prevent repeated entries
static Address() { Type currentType = typeof(Address); object[] attributes = currentType.GetCustomAttributes(typeof(MetadataTypeAttribute), false); if (attributes.Length > 0) { //MetadataType attribute found! MetadataTypeAttribute metaDataAttribute = (MetadataTypeAttribute)attributes[0]; TypeDescriptor.AddProviderTransparent( new AssociatedMetadataTypeTypeDescriptionProvider( currentType, metaDataAttribute.MetadataClassType), currentType); } }

The file declaring the MetadataType:

namespace DataModel
{
	[MetadataType(typeof(Address_Metadata))]
	public partial class Address : BaseEntity, IValidatableObject
	{

		public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
		{
			var results = new List<ValidationResult>();

			if (Line1.Length == 0 && Line2.Length == 0)
				results.Add(new ValidationResult("Every address needs either Line1 or Line2"));

			return results;
		}
	}

	public sealed class Address_Metadata
	{
		[Required]
		public int AddressId { get; set; }

		[Required]
		[MaxLength(128, ErrorMessage = "Address Line max length is 128")]
		public string Line1 { get; set; }

		[MaxLength(128, ErrorMessage = "Address Line max length is 128")]
		public string Line2 { get; set; }

		[Required]
		[MaxLength(64, ErrorMessage = "Address City max length is 64")]
		public string City { get; set; }

		[Required]
		[MaxLength(10, ErrorMessage = "Address State/Province max length is 10")]
		public string State { get; set; }

		[Required]
		[MaxLength(10, ErrorMessage = "Address Zip/Postal code max length is 10")]
		public string Zip { get; set; }
	}
}

I've searched for this error and I can't find any that mention 'DataSource' as the item that fails to be generated. They always appear to be something missing from the class they're working with. Has anyone encountered this problem? Have a solution for it?

TIA,
Mike


Viewing all articles
Browse latest Browse all 2535

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>