EF Code First在它应该具有的时候没有创建外键

时间:2013-03-01 20:10:39

标签: c# asp.net entity-framework ef-code-first foreign-keys

目前我正在关注作者创建这些模型类的Programming Entity Framework: Code First

namespace Model
{
    public class Destination
    {
        public int DestinationID { get; set; }
        public string Name { get; set; }
        public string Country { get; set; }
        public string Description { get; set; }
        public byte[] Photo { get; set; }
        public List<Lodging> Lodgings { get; set; }
    }

    public class Lodging
    {
        public int LodgingID { get; set; }
        public string Name { get; set; }
        public string Owner { get; set; }
        public bool IsResort { get; set; }
        public Destination Destination { get; set; }
    }
}

并显示自动生成的数据库在Destination_DestinationID表中有一个名为Lodgings外键。但问题是在我的情况下,它是作为普通的 int 而不是外键创建的。

我正在使用EF5,我猜这本书使用的是EF4.1,这是造成这种差异的原因吗?

1 个答案:

答案 0 :(得分:0)

我复制了代码,我使用EF Code First 5,生成的Destination_DestinationId确实是外键。问题不在于代码优先,但是Visual Studio Server资源管理器没有向您展示,我更喜欢使用SQL Server Management Studio进行数据库相关任务,它会向您显示该列实际上是带有灰色键的外键图标。