I am engaged in an ETL project using IronPython and loading into a SQL Server database represented by an EDM 4.1 model provided to me. While I can generally load and save the data without trouble, I’m plagued by string length overruns in the incoming data. This throws an exception in the final .SaveChanges() call to the EDM. I receive no information in the exception about what entity, entity class, or property threw the error.
I would like to modify my code so that it checks the max length of the column for the database’s metadata at the time of assignment. It appears that this metadata is not available through the entity class but rather using the MetadataWorkspace of the EDM context object.
In order to call .GetItems() on the MetadataWorkspace, I need to provide an enumeration value from System.Data.Metadata.Edm.Dataspace:
.GetItems(System.Data.Metadata.Edm.Dataspace.CSpace)
Unfortunately, I don’t seem to be able to reference System.Data.Metadata in any way. I cannot get beyond System.Data:
>>> import clr
>>> clr.AddReference("System.Data")
>>> import System.Data
>>> clr.AddReference("System.Data.Metadata")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IOError: System.IO.IOException: Could not add reference to assembly System.Data.Metadata
at IronPython.Runtime.ClrModule.AddReference(CodeContext context, String name)
Does anyone have experience in introspecting EDM metadata from IronPython?
To be able to access MetadataWorkspace you need to add reference to System.Data.Entity.dll (if you don’t have one) and then drop down to ObjectContext like this:
where ctx is your DbContext derived class.
Also in EF 4.1 there is a Validation feature implemented. I am not sure if this what throws the exception because it should give you all the details – including property name and a reference to the entity where the validation error occured. If the exception is thrown by the database it would mean that Validation is turned off. If you turned it on it would check facets and throw an execption if they are validated. Here are the links to blog posts on Validation:
http://blogs.msdn.com/b/adonet/archive/2011/05/27/ef-4-1-validation.aspx
http://blogs.msdn.com/b/adonet/archive/2010/12/15/ef-feature-ctp5-validation.aspx