我觉得EF4.1的ForeignKey字段批注很有用啊

Topics: User Forum
Nov 23, 2011 at 2:01 PM
namespace System.ComponentModel.DataAnnotations
{
    // 摘要:
    //     Denotes a property used as a foreign key in a relationship.  The annotation
    //     may be placed on the foreign key property and specify the associated navigation
    //     property name, or placed on a navigation property and specify the associated
    //     foreign key name.
    [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)]
    public class ForeignKeyAttribute : Attribute
    {
        // 摘要:
        //     Initializes a new instance of the System.ComponentModel.DataAnnotations.ForeignKeyAttribute
        //     class.
        //
        // 参数:
        //   name:
        //     If placed on a foreign key property, the name of the associated navigation
        //     property.  If placed on a navigation property, the name of the associated
        //     foreign key(s).  If a navigation property has multiple foreign keys, a comma
        //     separated list should be supplied.
        public ForeignKeyAttribute(string name);

        // 摘要:
        //     If placed on a foreign key property, the name of the associated navigation
        //     property.  If placed on a navigation property, the name of the associated
        //     foreign key(s).
        public string Name { get; }
    }
}
尤其在使用mvc3时,view自动填充到对象时,,是不能,也没必要直接绑定外键所在类上吧????这样我们访问外键时也不用访问对象。当然这个外键的同步,,却是个产生的新问题。。一方改变时,框架自动改变另一方,这是个不错的方案?
Nov 25, 2011 at 8:48 AM

现在的data框架是不能直接修改外键的吧,??就是说外键只能读不能写入??有时候我们只需要一个id,我个人认为没必要将整个对象再实例化

Coordinator
Dec 3, 2011 at 1:54 PM

如果是一个小项目,将整个对象实例化的影响并不会被感受到。

如果是一个大项目,而且已经确认是对象实例化造成了性能瓶颈,则应该直接将外键改为long类型,而不依靠内置的关联处理模式。