Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Friday, July 31, 2009 5:15 AM
I have created a Multi Tier website with Data Contexts created using SQLMetal and modified to include a default constructor in the Data Layer and the Website trying to register the routes through global.asax.cs
When trying to run the site I get the error:
Unable to cast object of type 'System.Reflection.RtFieldInfo' to type 'System.Reflection.PropertyInfo'
on
model.RegisterContext(typeof(My_DataContext), new ContextConfiguration() { ScaffoldAllTables = true });
If I copy the datacontext in to the web project the site builds and runs
Can anyone help as we are wanting to have two systems using the same data layer
Thanks in advance
All replies (9)
Friday, July 31, 2009 6:30 AM âś…Answered
Unable to cast object of type 'System.Reflection.RtFieldInfo' to type 'System.Reflection.PropertyInfo'
it looks to me like you have some field that should realy be a property. I think the issue may be to do with where you havbe added metadata.
Friday, July 31, 2009 9:53 AM
Excellent.
We had an additional int to hold an exception number in case of error. Changed from
public int errornumber;
to
public int errornumber {get; set;}
And hey presto!!!
Thanks
Friday, August 7, 2009 9:03 AM
[*1]
...Unable to cast object of type 'System.Reflection.RtFieldInfo' to type 'System.Reflection.PropertyInfo'...
it looks to me like you have some field that should realy be a property. I think the issue may be to do with where you havbe added metadata.
Please help.
I am also getting this error.
After reading your message above, I checked and I have not added any metadata-- that is, I am trying to setup Dynamic Data out-of-the-box, by adding it to an existing VS.NET Web Site.
I have been following the "How To Add Dynamic Data To An Existing Web Site" instructions here...
http://blogs.msdn.com/scothu/archive/2008/06/23/how-to-add-dynamic-data-to-an-existing-web-site.aspx
...and here...
http://msdn.microsoft.com/en-us/library/cc837197.aspx
...and everything compiles fine but when I load the Dynamic Data page I get that error.
One thing to note is that I have my Linq-To-Sql model in a separate custom class library project, which is a project added as part of the Solution that contains both the Web Site and the custom class library-- and the Web Site has a project-based reference pointing to the class library and the RegisterRoutes looks like this...
model.RegisterContext(typeof(Team.Cle.BusinessLayer.BusinessEntities.DataClassesContext), new ContextConfiguration() { ScaffoldAllTables = true });
...and that compiles properly, etc.
Another thing to note is that I am using "LINQ to SQL Entity Base - Disconnected LINQ", found here...
http://www.codeplex.com/LINQ2SQLEB
...which provides a custom base class for all Linq To Sql entities in the model-- so, I am think this may be the issue.
Also, I am getting the dreaded error ...
[*2]
throw new InvalidOperationException("There are no accessible tables. Make sure that at least one data model is registered in Global.asax and scaffolding is enabled or implement custom pages.");
...error but it seems to occilate between throwing [*1] and [*2]...
...so I am stuck and looking for help.
What do you think?
Please advise.
Thank you.
-- Mark Kamoski
Friday, August 7, 2009 9:35 AM
I've not used the "LINQ to SQL Entity Base - Disconnected LINQ", but if this allows you to use inheritance then that could cause you some issues ad DD v1 does not support it v2 will as seen in Preview 4 refresh on codeplex
Friday, August 7, 2009 11:25 AM
mkamoski, are you using a custom linq to sql model? (as opossed to a dbml generated one)
if so, check that you don't have any fields marked with a [Column] attribute. Even though that is perfectly ok for L2S, dynamic data expects only properties to be marked as columns.
Also, I looked at that base class you are using, it is doing a lot of reflection, are you sure that expection is not comming from there?
Saturday, August 8, 2009 5:56 PM
Good point Brian, I didn't think of that as it usually crops up in partial class files [:$]
Tuesday, August 11, 2009 2:26 PM
Regarding this...
I've not used the "LINQ to SQL Entity Base - Disconnected LINQ", but if this allows you to use inheritance then that could cause you some issues ad DD v1 does not support it...
Maybe that is the issue.
The "LINQEntityBase" requires a one-time manual modification of the DBML file's XML, which adds an XML property "EntityBase" to the XML node "Database", as follows...
Before the modification...
<Database Name="Clearances" EntityNamespace="Team.Clear.BusinessLayer.BusinessEntities" ContextNamespace="Team.Clear.BusinessLayer.BusinessEntities" Serialization="Unidirectional" xmlns="http://schemas.microsoft.com/linqtosql/dbml/2007">
After the modification...
<Database Name="Clearances" EntityNamespace="Team.Clear.BusinessLayer.BusinessEntities" ContextNamespace="Team.Clear.BusinessLayer.BusinessEntities" Serialization="Unidirectional" EntityBase="Team.Clear.BusinessLayer.BusinessEntities.LINQEntityBase" xmlns="http://schemas.microsoft.com/linqtosql/dbml/2007">
Do you think that is the culprit?
Please advise.
Thank you.
-- Mark Kamoski
Tuesday, August 11, 2009 2:42 PM
mkamoski, are you using a custom linq to sql model? (as opossed to a dbml generated one)
if so, check that you don't have any fields marked with a [Column] attribute. Even though that is perfectly ok for L2S, dynamic data expects only properties to be marked as columns.
Also, I looked at that base class you are using, it is doing a lot of reflection, are you sure that expection is not comming from there?
Regarding the "custom linq to sql model"...
...well, the DBML file it IS generated in the VS.NET IDE-- that is, I do not write it by hand. However, there are 2 changes that I make the DBML file. As I noted in my post above, there is a one-time, manual modification that I made to XML to make sure every entity uses a given base class but after generating my model using the IDE that attribute does not get dropped and the rest of the DBML's XML does get updated as expected. Also, I am using it, http://www.huagati.com/dbmltools/ , AKA Huagati, which provides round-trip sync between the L2s model and the database (and it has great renaming support)-- but, it just tweaks the file, AFAIK.
Regading "fields marked as [Column]" versus "properties marked as [Column]"...
...I am not sure what you mean by the distinction between a "field" and a "property". I am simply letting the IDE map each database-table plain-vanilla, that is, basically, each database-column gets mapped in an L2s entity wherein the entity has each database-column exposed as a property on that L2s entity. So, as such I have a lot of the following in "DataClasses.designer.cs"...
[
Column(Storage="_Subtotal", DbType="money")]
public System.Nullable<decimal> Subtotal
...Where "Subtotal" is the name of a database-column and the name of the exposed property in the L2s entity. The code gen in the IDE does that for every table, I do not manually add any fields/properties to the model. I do have a custom, hand-written partial class for every L2s entity, in which I write code in the OnValidate() Extensibility Method, etc-- but I do not add any properties in those classes. So, is that an issue?
Regarding the Reflection in the base class...
...I am not sure about that, but the offending line, as far as the error thrown is concerned, is the the RegisterContext call, as noted here...
Unable to cast object of type 'System.Reflection.RtFieldInfo' to type 'System.Reflection.PropertyInfo'.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.InvalidCastException: Unable to cast object of type 'System.Reflection.RtFieldInfo' to type 'System.Reflection.PropertyInfo'.
Source Error:
Line 41: // model.RegisterContext(typeof(YourDataContextType), new ContextConfiguration() { ScaffoldAllTables = false });
Line 42:
Line 43: model.RegisterContext(typeof(Team.Cle.BusinessLayer.BusinessEntities.DataClassesContext), new ContextConfiguration() { ScaffoldAllTables = true });
...so maybe that is bubbling up from somewhere? Is that what you are suggesting? From the be base class maybe?
If you have any other help on the matter, then please let me know.
Thank you VERY much.
-- Mark Kamoski
Tuesday, August 11, 2009 2:47 PM
It could be we usually get this error like Brian Pritchard says when using Partial classes and there is a field marked as a column which could happen if you are using something that represents the columns with fields instead.