-
Notifications
You must be signed in to change notification settings - Fork 134
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Failing to find Key attribute on the property if the property name in the model not equal to "Id" #32
Comments
can you provide a pull request or a failing unit test? Eric Hexter blog | http://Hex.LosTechies.com On Tue, Dec 11, 2012 at 4:32 AM, srnux [email protected] wrote:
|
I will try to provide the unit test as soon I get to my development machine. Thanks, 2012/12/22 Eric Hexter [email protected]
|
Please find the unit tests inside a test project here: https://github.com/srnux/MVCTwitterBootstrap Luka |
awesome.. i will add a project and unit tests in the for this extension Eric Hexter blog | http://Hex.LosTechies.com On Mon, Dec 24, 2012 at 5:15 AM, srnux [email protected] wrote:
|
I have another resolution (more like a quickfix) for this bug: change ... info.PropertyType.AttributeExists ... to info.AttributeExists ... on lines 66 and 70. The problem is that info.PropertyType points to the type of the property (ie String) so it can't have the KeyAttribute. but info.AttributeExists asks about the attributes of the actual property. |
@xkobram 's post should fix the issue. |
Just to confirm I had the problem with a key attribute on a string, xkobram's solution fixed it. |
Im having the same problem described but in muy situtation we are using fluent api so we do not add the key data annotation in the class or the ID to the key in the object. What can i do ? I would appreciate any help. |
You can change that method to find your id field by your convention. On Saturday, July 20, 2013, eljeanc wrote:
Eric Hexter blog | http://Hex.LosTechies.com |
any idea on how to achieve this i have been thinking how to make it work without using the data annotations, this because our project will not use it directly in de dtos. |
If you name your properties Id or key or some other predictable way you On Saturday, July 20, 2013, eljeanc wrote:
Eric Hexter blog | http://Hex.LosTechies.com |
Model pseudo-code:
[Key]
public long AnythingDifferentFromId { get; set; }
Breaks at line 49, ViewHelperExtensions.cs :
return model.GetType().GetProperty(model.IdentifierPropertyName()).GetValue(model,new object[0]);
with exception:
Object reference not set to an instance of an object.
Cause, line ~60:
if (type.GetProperties().Any(info => info.PropertyType.AttributeExists<System.ComponentModel.DataAnnotations.KeyAttribute>()))
does not find the Key
Possible solution, replace the if inside IdentifierPropertyName with:
if (type.GetProperties().Any(info => info.GetCustomAttributes(typeof(System.ComponentModel.DataAnnotations.KeyAttribute), true)!=null))
{
return
type.GetProperties().First(info => info.GetCustomAttributes(typeof(System.ComponentModel.DataAnnotations.KeyAttribute), true)!=null).Name;
}
The text was updated successfully, but these errors were encountered: