无法使用EF6在一个表中创建2个外键

时间:2016-07-29 06:22:04

标签: asp.net-mvc sql-server-2008-r2 entity-framework-6

我正在使用VS 2013,SQL Server 2008 R2。我刚开始学习ASP.NET MVC& EF

我创建了两个类:

public class Album
{
    public int Id { get; set; }
    //ApplicationUser from Asp.net Identity Model
    public ApplicationUser Artist; //Who Part
    public DateTime DateTime { get; set; } //When Part
    public string Venue { get; set; } //Where Part
    public Genre Genre { get; set; } //Type Part
}

public class Genre
{
    public byte Id { get; set; }
    public string Name { get; set; }
}

当我创建add-migration CreateAlbum时,我应该获得两个外键,但我只得到一个,即来自GenreGenre_Id)。

如何解决这个问题?我想将ApplicationUserArtist_Id)即AspnetUser链接到我的Album

1 个答案:

答案 0 :(得分:1)

您忘了设置获取并设置。在您的类中为Application用户添加get和set。

public class Album
{
    public int Id { get; set; }
    //ApplicationUser from Asp.net Identity Model
    public ApplicationUser Artist { get; set; }; //Who Part
    public DateTime DateTime { get; set; } //When Part
    public string Venue { get; set; } //Where Part
    public Genre Genre { get; set; } //Type Part
}

public class Genre
{
    public byte Id { get; set; }
    public string Name { get; set; }
}

现在运行迁移。它会起作用。

相关问题