В моем каталоге моделей у меня есть следующий кодЯ пытаюсь добавить миграции, но я получаю сообщение об ошибке, как я могу решить эту проблему
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
' требуется ключ. Как я могу решить эту ошибку
Я попробовал ваш код и он работал отлично, может вы также показываете классы Category и Post? – Ziyad
@ Ziyad Я не уверен, что вы сможете это увидеть. public class Категория { public string Id {get; задавать; } [Обязательно] [Показать (с именем = "Имя")] public string Имя {get; задавать; } [Обязательно] [Показать (сгенерировано = "UrlSeo")] public string UrlSeo {get; задавать; } [Обязательно] [Показать (с именем = "Описание")] public string Описание {get; задавать; } public bool Проверено {get; задавать; } общественный ICollection PostCategories {get; задавать; } } –
Я до сих пор не могу найти, почему вы видите эту ошибку, просто обновите вопрос и добавьте свой класс миграции, а также – Ziyad