با سلام
لطفا یکی بگه چرا زمان Add-Migration با این خطا رو برو میشم
The property 'UserId' cannot be configured as a navigation property. The property must be a valid entity type and the property should have a non-abstract getter and setter. For collection properties the type must implement ICollection<T> where T is a valid entity type.
کد مربود به content
using System; using System.ComponentModel.DataAnnotations; using Languages.Resources; using System.ComponentModel.DataAnnotations.Schema; using System.Collections.Generic; using Domain.ViewModels; using System.Web.Mvc; namespace Domain.Domain { public abstract class Content { public Content() { this.LastModifyDate = DateTime.Now; } #region public [Required] [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int Id { get; set; } [StringLength(1000, ErrorMessageResourceName = "Between", ErrorMessageResourceType = typeof(Error), MinimumLength = 1)] [Required(ErrorMessageResourceName = "IsRequired", ErrorMessageResourceType = typeof(Error))] [Display(Name = "Title", ResourceType = typeof(DomainLabel))] [MaxLength(1000)] public string Title { get; set; } . . . [ForeignKey("User")] public int UserId { get; set; } public virtual User User { get; set; } #endregion } }
کد مربوط به جدول User
using Languages.Resources; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; namespace Domain.Domain { public class User { [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int Id { get; set; } [Required(ErrorMessageResourceName = "IsRequired", ErrorMessageResourceType = typeof(Error))] [Display(Name = "Alias", ResourceType = typeof(DomainLabel))] [MaxLength(100)] public string Alias { get; set; } [Display(Name = "Avatar", ResourceType = typeof(DomainLabel))] public string Avatar { get; set; } [Required(ErrorMessageResourceName = "IsRequired", ErrorMessageResourceType = typeof(Error))] [Display(Name = "Email", ResourceType = typeof(DomainLabel))] [MaxLength(200)] [DataType(DataType.EmailAddress, ErrorMessageResourceName = "IsNotValid", ErrorMessageResourceType = typeof(Error))] public string Email { get; set; } public string HashPassword { get; set; } [ForeignKey("Role")] public int RoleId { get; set; } public virtual Role Role { get; set; } } }
این لینک رو ببینید جواب اول
ممنونم استاد این لینک رو دیدم ولی جواب نداد
تا اونجا که من متوجه شدم مشکل در رابطه های مدل ها است
فیلد UserID رو بررسی کنید
فیلد های دیگه ای هم توی جدول هستن که دقیقا مثل همین نوشتم و از اونها خطا نمیگیره ولی از UserId خطا میگیره
الان این کد رو هم نوشتم ولی بازم کار نمیکنه
mb.Entity<Content>() .HasRequired<User>(s => s.User) .WithMany(s => s.Content).HasForeignKey(f => f.UserId);
هیچ کاربری تا کنون از این پست تشکر نکرده است
با ما تماس بگیرید تا در این مسیر همراهتان باشیم :)