Asp.net mvc POCOs上主键的MVC视图生成器提示

Asp.net mvc POCOs上主键的MVC视图生成器提示,asp.net-mvc,Asp.net Mvc,为我的模型生成强类型索引视图时,我始终会得到以下结果: <%= Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) %> 我正在使用一个POCO类用于我们的ORM。据我所知,在使用LINQ进行SQL时,视图代码将知道哪个字段是主键 是否有一种方法可以为属性(或类)添加一个属性,让视图生成器知道如果主键是ID属性?您可以直接使用,也可以在好友类上使用,或者通过关联的元数据提供程序使用。您可以使用,直接

为我的模型生成强类型索引视图时,我始终会得到以下结果:

<%= Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) %>

我正在使用一个POCO类用于我们的ORM。据我所知,在使用LINQ进行SQL时,视图代码将知道哪个字段是主键


是否有一种方法可以为属性(或类)添加一个属性,让视图生成器知道如果主键是ID属性?

您可以直接使用,也可以在好友类上使用,或者通过关联的元数据提供程序使用。

您可以使用,直接或在好友类上,或通过关联的元数据提供程序。

如果我理解正确的话

[Column(IsPrimaryKey=true)]
应该有用

建议基于功能

public static List<string> GetEntityKeyProperties(Type type)
{
    List<string> keyProperties = new List<string>();

    PropertyInfo[] properties = type.GetProperties();

    foreach (PropertyInfo pi in properties)
    {
        System.Object[] attributes = pi.GetCustomAttributes(true);

        foreach (object attribute in attributes)
        {
            if (attribute is EdmScalarPropertyAttribute)
            {
                if ((attribute as EdmScalarPropertyAttribute).EntityKeyProperty == true)
                {
                    keyProperties.Add(pi.Name);
                }
            } else if(attribute is ColumnAttribute) {
                if ((attribute as ColumnAttribute).IsPrimaryKey == true)
                {
                    keyProperties.Add(pi.Name);
                }
            }
        }
    }

    return keyProperties;
}
公共静态列表GetEntityKeyProperties(类型)
{
List keyProperties=new List();
PropertyInfo[]properties=type.GetProperties();
foreach(PropertyInfo pi in properties)
{
System.Object[]attributes=pi.GetCustomAttributes(true);
foreach(属性中的对象属性)
{
if(属性为EdmScalarPropertyAttribute)
{
if((属性为EdmScalarPropertyAttribute).EntityKeyProperty==true)
{
添加(pi.Name);
}
}else if(属性为ColumnAttribute){
if((属性为ColumnAttribute).IsPrimaryKey==true)
{
添加(pi.Name);
}
}
}
}
返回键属性;
}

放在MVC 1的List.tt模板中。

如果我理解正确的话

[Column(IsPrimaryKey=true)]
应该有用

建议基于功能

public static List<string> GetEntityKeyProperties(Type type)
{
    List<string> keyProperties = new List<string>();

    PropertyInfo[] properties = type.GetProperties();

    foreach (PropertyInfo pi in properties)
    {
        System.Object[] attributes = pi.GetCustomAttributes(true);

        foreach (object attribute in attributes)
        {
            if (attribute is EdmScalarPropertyAttribute)
            {
                if ((attribute as EdmScalarPropertyAttribute).EntityKeyProperty == true)
                {
                    keyProperties.Add(pi.Name);
                }
            } else if(attribute is ColumnAttribute) {
                if ((attribute as ColumnAttribute).IsPrimaryKey == true)
                {
                    keyProperties.Add(pi.Name);
                }
            }
        }
    }

    return keyProperties;
}
公共静态列表GetEntityKeyProperties(类型)
{
List keyProperties=new List();
PropertyInfo[]properties=type.GetProperties();
foreach(PropertyInfo pi in properties)
{
System.Object[]attributes=pi.GetCustomAttributes(true);
foreach(属性中的对象属性)
{
if(属性为EdmScalarPropertyAttribute)
{
if((属性为EdmScalarPropertyAttribute).EntityKeyProperty==true)
{
添加(pi.Name);
}
}else if(属性为ColumnAttribute){
if((属性为ColumnAttribute).IsPrimaryKey==true)
{
添加(pi.Name);
}
}
}
}
返回键属性;
}

放在MVC 1的List.tt模板中。

Awesome。这似乎是framework4的一个属性。有3.5美元的吗?我不知道。使用关联的元数据提供程序可能是可行的。我没看过,太棒了。这似乎是framework4的一个属性。有3.5美元的吗?我不知道。使用关联的元数据提供程序可能是可行的。我还没看过,就是这样。谢谢(也适用于MVC2)就是这样做的。谢谢(也适用于MVC2)