VS2012 RC中无法识别ForeignKey

13

昨天得到了很多帮助后,我在asp.net4 beta中遇到了一个已知的错误 - 我升级到了VS2012 RC Express(4.5),现在VS在我的模型中报告了两个错误,这些错误之前是没问题的:

"The type or namespace name 'ForeignKeyAttribute' could not be found (are you missing a using directive or an assembly reference?)"

"The type or namespace name 'ForeignKey' could not be found (are you missing a using directive or an assembly reference?)"

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.Data.Entity.ModelConfiguration.Conventions;
using System.Data.Entity;

namespace MvcApplication6.Models
{
    public class tblRental
    {
        [Key()]
            public int rental_id { get; set; }
        public int room_id { get; set; }
        public DateTime check_in { get; set; }
        public DateTime check_out { get; set; }
        public decimal room_cost { get; set; }
        public long customer_ref { get; set; }
        [ForeignKey("customer_ref")]
        public virtual tblCustomerBooking Customer { get; set; }

    }

    public class tblCustomerBooking
    {
        [Key()]
        public long customer_id { get; set; }
        public string customer_name { get; set; }
        public string customer_email { get; set; }
        public virtual ICollection<tblRental> Rentals { get; set; }
    }

有人知道ForeignKey引用是否已更改吗?

谢谢任何帮助,

马克

1个回答

23

我刚刚发现我需要添加:

using System.ComponentModel.DataAnnotations.Schema;
我之前不需要移动它,所以我认为ForeignKey已经被移动到模式命名空间下。
希望这能帮助其他人,
谢谢,Mark

顺便提一下,您可以使用 Ctrl + .(点)快捷键来自动解决这些错误。 - Alexander
我自己大约一周前也遇到了这个问题...在新版本中他们移动了很多东西。 - Ben
我遇到了一个反向问题:升级到RC 2012并执行上述操作使我的属性工作。然后我不得不降级到.NET 4.0(Azure尚不支持.NET 4.5),现在我遇到了相同的ForeignKey和NotMapped属性未找到错误,尽管我似乎引用了正确的DLL(System.ComponentModel.DataAnnotations)。令人沮丧。 - jeremy
2
已解决:其他人可能会遇到这个问题:如果你在升级后被迫降级,请按照以下步骤操作:(a)从Nuget卸载EF (b)设置目标.NET框架 (c)重新安装EF (d)确保添加.Schema引用。 - jeremy
1
我花了很多时间查找哪些库和注释被保存了。看起来像是一个设计问题。 - John Shedletsky

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接