EF外键未创建

时间:2017-11-04 01:25:14

标签: c# .net entity-framework code-first ef-migrations

我正在使用EF代码第一种方法。一切都工作正常,直到我开始在我的课程中使用多个主键。这是一个例子:

public class BaseEntity
{
    [Key, Column(Order = 0)]
    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    public int Id { get; set; }
}

public class News : BaseEntity
{
    [Key, Column(Order = 1)]
    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    public int LanguageId { get; set; }

    public string Headline { get; set; }

    public string Description { get; set; }

    public DateTime Date { get; set; }

    public virtual ICollection<Content> Paragraphs { get; set; }

    public bool IsQuick { get; set; }

    public virtual Photo Photo { get; set; }
}

public class Photo : BaseEntity
{
    public string Description { get; set; }

    public DateTime? Date { get; set; }

    public byte[] Image { get; set; }
}

当我运行命令 Add-Migration 时, Photo_Id 外键未被创建。

这是运行命令的结果:

CreateTable(
"dbo.News",
c => new
    {
        Id = c.Int(nullable: false),
        LanguageId = c.Int(nullable: false),
        Headline = c.String(),
        Description = c.String(),
        Date = c.DateTime(nullable: false),
        IsQuick = c.Boolean(nullable: false),
    })
.PrimaryKey(t => new { t.Id, t.LanguageId })
.ForeignKey("dbo.Photos", t => t.Id, cascadeDelete: true)
.Index(t => t.Id);

CreateTable(
"dbo.Photos",
c => new
    {
        Id = c.Int(nullable: false),
        Description = c.String(),
        Date = c.DateTime(),
        Image = c.Binary(),
        Match_Id = c.Int(),
    })
.PrimaryKey(t => t.Id)
.ForeignKey("dbo.Matches", t => t.Match_Id)
.Index(t => t.Match_Id);

正如您所看到的,表创建中没有 Photo_Id 键。

我能做些什么的想法?

0 个答案:

没有答案