我正在尝试添加迁移,但我一直在收到错误,我该如何解决这一挑战

时间:2016-09-13 08:32:17

标签: c# entity-framework

在我的models目录中,我有以下代码

public class Post
{
    // Each post will have an ID
    public string Id { get; set; }

    // Each Post must have a title
    [Required]
    [StringLength(50, ErrorMessage = "The {0} must be between {2} and {1} characters long.", MinimumLength = 5)]
    [Display(Name = "Title")]
    public string Title { get; set; }

    // Each Post must have a short description
    [Required]
    [StringLength(250, ErrorMessage = "The {0} must be between {2} and {1} characters long.", MinimumLength = 20)]
    [Display(Name = "Short Description")]
    public string ShortDescription { get; set; }

    // Each Post must have a body
    [Required]
    [StringLength(5000, ErrorMessage = "The {0} must be between {2} and {1} characters long.", MinimumLength = 500)]
    [Display(Name = "Body")]
    public string Body { get; set; }

    // Each Post must have meta description to use in the meta tag
    [Required]
    [StringLength(25, ErrorMessage = "The {0} must be between {2} and {1} characters long.", MinimumLength = 5)]
    [Display(Name = "Meta")]
    public string Meta { get; set; }

    // User friendly urls
    [Required]
    [Display(Name = "UrlSeo")]
    public string UrlSeo { get; set; }

    // A boolean to check if the post has been published
    public bool Published { get; set; }

    [DefaultValue(0)]
    // Count of how many likes the post has
    public int NetLikeCount { get; set; }

    //DateTime when it was created
    public DateTime PostedOn { get; set; }

    //DateTime when it was modified
    public DateTime? Modified { get; set; }

    //Properties which define relationships between tables
    public ICollection<Comment> Comments { get; set; }
    public ICollection<Reply> Replies { get; set; }
    public ICollection<PostCategory> PostCategories { get; set; }
    public ICollection<PostTag> PostTags { get; set; }
    public ICollection<PostVideo> PostVideos { get; set; }
    public ICollection<PostLike> PostLikes { get; set; }
}
public class Comment
{
    public string Id { get; set; }

    public string PostId { get; set; }

    public DateTime DateTime { get; set; }

    public string UserName { get; set; }

    [Required]
    [StringLength(1000, ErrorMessage = "The {0} must be between {2} and {1} characters long.", MinimumLength = 25)]
    [Display(Name = "Body")]
    public string Body { get; set; }

    [DefaultValue(0)]
    public int NetLikeCount { get; set; }

    [DefaultValue(false)]
    public bool Deleted { get; set; }

    public Post Post { get; set; }
    public ICollection<Reply> Replies { get; set; }
    public ICollection<CommentLike> CommentLikes { get; set; }
}
public class Reply
{
    public string Id { get; set; }

    public string PostId { get; set; }

    public string CommentId { get; set; }

    public string ParentReplyId { get; set; }

    public DateTime DateTime { get; set; }

    public string UserName { get; set; }

    [Required]
    [StringLength(1000, ErrorMessage = "The {0} must be between {2} and {1} characters long.", MinimumLength = 25)]
    [Display(Name = "Body")]
    public string Body { get; set; }

    public bool Deleted { get; set; }

    public Post Post { get; set; }
    public Comment Comment { get; set; }
    public ICollection<ReplyLike> ReplyLikes { get; set; }
}
public class PostCategory
{
    [Key]
    [Column(Order = 0)]
    public string PostId { get; set; }

    [Key]
    [Column(Order = 1)]
    public string CategoryId { get; set; }

    public bool Checked { get; set; }

    public Post Post { get; set; }

    public Category Category { get; set; }
}
public class PostTag
{
    [Key]
    [Column(Order = 0)]
    public string PostId { get; set; }

    [Key]
    [Column(Order = 1)]
    public string TagId { get; set; }

    public bool Checked { get; set; }

    public Post Post { get; set; }
    public Tag Tag { get; set; }
}
public class PostVideo
{
    public string Id { get; set; }

    [Required]
    [Display(Name = "VideoUrl")]
    [DataType(DataType.Url)]
    public string VideoUrl { get; set; }

    public string VideoThumbnail { get; set; }

    public string PostId { get; set; }

    public string VideoSiteName { get; set; }

    public Post Post { get; set; }
}
public class PostLike
{
    [Key]
    public string PostId { get; set; }

    public string UserName { get; set; }

    public bool Like { get; set; }

    public bool DisLike { get; set; }

    public Post Post { get; set; }
}
public class Category
{
    public string Id { get; set; }

    [Required]
    [Display(Name = "Name")]
    public string Name { get; set; }

    [Required]
    [Display(Name = "UrlSeo")]
    public string UrlSeo { get; set; }

    [Required]
    [StringLength(20, ErrorMessage = "The {0} must be between {2} and {1} characters long.", MinimumLength = 5)]
    [Display(Name = "Description")]
    public string Description { get; set; }

    public bool Checked { get; set; }

    public ICollection<PostCategory> PostCategories { get; set; }
}
public class CommentLike
{
    [Key]
    public string CommentId { get; set; }

    public string UserName { get; set; }

    public bool Like { get; set; }

    public bool DisLike { get; set; }

    public Comment Comment { get; set; }
}
public class ReplyLike
{
    [Key]
    public string ReplyId { get; set; }

    public string UserName { get; set; }

    public bool Like { get; set; }

    public bool DisLike { get; set; }

    public Reply Reply { get; set; }
}
public class Tag
{
    public string Id { get; set; }

    [Required]
    [Display(Name = "Name")]
    public string Name { get; set; }

    [Required]
    [StringLength(20, ErrorMessage = "The {0} must be between {2} and {1} characters long.", MinimumLength = 5)]
    [Display(Name = "UrlSeo")]
    public string UrlSeo { get; set; }

    public bool Checked { get; set; }

    public ICollection<PostTag> PostTags { get; set; }
}

所以这些是我试图添加迁移的数据模式

当我尝试添加此迁移时,它告诉我实体类型“Blog.Models.PostCategory”需要定义一个键。我该如何解决这个错误

1 个答案:

答案 0 :(得分:1)

这是为您的类自动生成的迁移类,并在我的解决方案中正常工作,试一试:

public partial class Blog : DbMigration
{
    public override void Up()
    {
        CreateTable(
            "dbo.PostCategories",
            c => new
                {
                    PostId = c.String(nullable: false, maxLength: 128),
                    CategoryId = c.String(nullable: false, maxLength: 128),
                    Checked = c.Boolean(nullable: false),
                })
            .PrimaryKey(t => new { t.PostId, t.CategoryId })
            .ForeignKey("dbo.Categories", t => t.CategoryId, cascadeDelete: true)
            .ForeignKey("dbo.Posts", t => t.PostId, cascadeDelete: true)
            .Index(t => t.PostId)
            .Index(t => t.CategoryId);

        CreateTable(
            "dbo.Categories",
            c => new
                {
                    Id = c.String(nullable: false, maxLength: 128),
                    Name = c.String(nullable: false),
                    UrlSeo = c.String(nullable: false),
                    Description = c.String(nullable: false, maxLength: 20),
                    Checked = c.Boolean(nullable: false),
                })
            .PrimaryKey(t => t.Id);

        CreateTable(
            "dbo.Posts",
            c => new
                {
                    Id = c.String(nullable: false, maxLength: 128),
                    Title = c.String(nullable: false, maxLength: 50),
                    ShortDescription = c.String(nullable: false, maxLength: 250),
                    Body = c.String(nullable: false),
                    Meta = c.String(nullable: false, maxLength: 25),
                    UrlSeo = c.String(nullable: false),
                    Published = c.Boolean(nullable: false),
                    NetLikeCount = c.Int(nullable: false),
                    PostedOn = c.DateTime(nullable: false),
                    Modified = c.DateTime(),
                })
            .PrimaryKey(t => t.Id);

        CreateTable(
            "dbo.Comments",
            c => new
                {
                    Id = c.String(nullable: false, maxLength: 128),
                    PostId = c.String(maxLength: 128),
                    DateTime = c.DateTime(nullable: false),
                    UserName = c.String(),
                    Body = c.String(nullable: false, maxLength: 1000),
                    NetLikeCount = c.Int(nullable: false),
                    Deleted = c.Boolean(nullable: false),
                })
            .PrimaryKey(t => t.Id)
            .ForeignKey("dbo.Posts", t => t.PostId)
            .Index(t => t.PostId);

        CreateTable(
            "dbo.CommentLikes",
            c => new
                {
                    CommentId = c.String(nullable: false, maxLength: 128),
                    UserName = c.String(),
                    Like = c.Boolean(nullable: false),
                    DisLike = c.Boolean(nullable: false),
                    Comment_Id = c.String(maxLength: 128),
                })
            .PrimaryKey(t => t.CommentId)
            .ForeignKey("dbo.Comments", t => t.Comment_Id)
            .Index(t => t.Comment_Id);

        CreateTable(
            "dbo.Replies",
            c => new
                {
                    Id = c.String(nullable: false, maxLength: 128),
                    PostId = c.String(maxLength: 128),
                    CommentId = c.String(maxLength: 128),
                    ParentReplyId = c.String(),
                    DateTime = c.DateTime(nullable: false),
                    UserName = c.String(),
                    Body = c.String(nullable: false, maxLength: 1000),
                    Deleted = c.Boolean(nullable: false),
                })
            .PrimaryKey(t => t.Id)
            .ForeignKey("dbo.Comments", t => t.CommentId)
            .ForeignKey("dbo.Posts", t => t.PostId)
            .Index(t => t.PostId)
            .Index(t => t.CommentId);

        CreateTable(
            "dbo.ReplyLikes",
            c => new
                {
                    ReplyId = c.String(nullable: false, maxLength: 128),
                    UserName = c.String(),
                    Like = c.Boolean(nullable: false),
                    DisLike = c.Boolean(nullable: false),
                    Reply_Id = c.String(maxLength: 128),
                })
            .PrimaryKey(t => t.ReplyId)
            .ForeignKey("dbo.Replies", t => t.Reply_Id)
            .Index(t => t.Reply_Id);

        CreateTable(
            "dbo.PostLikes",
            c => new
                {
                    PostId = c.String(nullable: false, maxLength: 128),
                    UserName = c.String(),
                    Like = c.Boolean(nullable: false),
                    DisLike = c.Boolean(nullable: false),
                    Post_Id = c.String(maxLength: 128),
                })
            .PrimaryKey(t => t.PostId)
            .ForeignKey("dbo.Posts", t => t.Post_Id)
            .Index(t => t.Post_Id);

        CreateTable(
            "dbo.PostTags",
            c => new
                {
                    PostId = c.String(nullable: false, maxLength: 128),
                    TagId = c.String(nullable: false, maxLength: 128),
                    Checked = c.Boolean(nullable: false),
                })
            .PrimaryKey(t => new { t.PostId, t.TagId })
            .ForeignKey("dbo.Posts", t => t.PostId, cascadeDelete: true)
            .ForeignKey("dbo.Tags", t => t.TagId, cascadeDelete: true)
            .Index(t => t.PostId)
            .Index(t => t.TagId);

        CreateTable(
            "dbo.Tags",
            c => new
                {
                    Id = c.String(nullable: false, maxLength: 128),
                    Name = c.String(nullable: false),
                    UrlSeo = c.String(nullable: false, maxLength: 20),
                    Checked = c.Boolean(nullable: false),
                })
            .PrimaryKey(t => t.Id);

        CreateTable(
            "dbo.PostVideos",
            c => new
                {
                    Id = c.String(nullable: false, maxLength: 128),
                    VideoUrl = c.String(nullable: false),
                    VideoThumbnail = c.String(),
                    PostId = c.String(maxLength: 128),
                    VideoSiteName = c.String(),
                })
            .PrimaryKey(t => t.Id)
            .ForeignKey("dbo.Posts", t => t.PostId)
            .Index(t => t.PostId);

    }

    public override void Down()
    {
        DropForeignKey("dbo.PostVideos", "PostId", "dbo.Posts");
        DropForeignKey("dbo.PostTags", "TagId", "dbo.Tags");
        DropForeignKey("dbo.PostTags", "PostId", "dbo.Posts");
        DropForeignKey("dbo.PostLikes", "Post_Id", "dbo.Posts");
        DropForeignKey("dbo.PostCategories", "PostId", "dbo.Posts");
        DropForeignKey("dbo.ReplyLikes", "Reply_Id", "dbo.Replies");
        DropForeignKey("dbo.Replies", "PostId", "dbo.Posts");
        DropForeignKey("dbo.Replies", "CommentId", "dbo.Comments");
        DropForeignKey("dbo.Comments", "PostId", "dbo.Posts");
        DropForeignKey("dbo.CommentLikes", "Comment_Id", "dbo.Comments");
        DropForeignKey("dbo.PostCategories", "CategoryId", "dbo.Categories");
        DropIndex("dbo.PostVideos", new[] { "PostId" });
        DropIndex("dbo.PostTags", new[] { "TagId" });
        DropIndex("dbo.PostTags", new[] { "PostId" });
        DropIndex("dbo.PostLikes", new[] { "Post_Id" });
        DropIndex("dbo.ReplyLikes", new[] { "Reply_Id" });
        DropIndex("dbo.Replies", new[] { "CommentId" });
        DropIndex("dbo.Replies", new[] { "PostId" });
        DropIndex("dbo.CommentLikes", new[] { "Comment_Id" });
        DropIndex("dbo.Comments", new[] { "PostId" });
        DropIndex("dbo.PostCategories", new[] { "CategoryId" });
        DropIndex("dbo.PostCategories", new[] { "PostId" });
        DropTable("dbo.PostVideos");
        DropTable("dbo.Tags");
        DropTable("dbo.PostTags");
        DropTable("dbo.PostLikes");
        DropTable("dbo.ReplyLikes");
        DropTable("dbo.Replies");
        DropTable("dbo.CommentLikes");
        DropTable("dbo.Comments");
        DropTable("dbo.Posts");
        DropTable("dbo.Categories");
        DropTable("dbo.PostCategories");
    }
}