مرجع تخصصی برنامه نویسان

انجمن تخصصی برنامه نویسان فارسی زبان

کاربر سایت

علی شهریاری

عضویت از 1393/05/30

خطا در Migration

  • یکشنبه 31 اردیبهشت 1396
  • 11:43
تشکر میکنم

با سلام 

لطفا یکی بگه چرا زمان 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; }

    }
}

پاسخ های این پرسش

تعداد پاسخ ها : 4 پاسخ
کاربر سایت

ایمان مدائنی

عضویت از 1392/01/20

  • یکشنبه 31 اردیبهشت 1396
  • 11:49

این لینک رو ببینید جواب اول

کاربر سایت

علی شهریاری

عضویت از 1393/05/30

  • یکشنبه 31 اردیبهشت 1396
  • 11:55

ممنونم استاد این لینک رو دیدم ولی جواب نداد

کاربر سایت

ایمان مدائنی

عضویت از 1392/01/20

  • یکشنبه 31 اردیبهشت 1396
  • 12:00

تا اونجا که من متوجه شدم مشکل در رابطه های مدل ها است

فیلد UserID رو بررسی کنید

کاربر سایت

علی شهریاری

عضویت از 1393/05/30

  • یکشنبه 31 اردیبهشت 1396
  • 12:11

فیلد های  دیگه ای هم توی جدول هستن که دقیقا مثل همین نوشتم و از اونها خطا نمیگیره ولی از UserId  خطا میگیره 

الان این کد رو هم نوشتم ولی بازم کار نمیکنه

 mb.Entity<Content>()
                  .HasRequired<User>(s => s.User)  
                  .WithMany(s => s.Content).HasForeignKey(f => f.UserId);

کاربرانی که از این پست تشکر کرده اند

هیچ کاربری تا کنون از این پست تشکر نکرده است

اگر نیاز به یک مشاور در زمینه طراحی سایت ، برنامه نویسی و بازاریابی الکترونیکی دارید

با ما تماس بگیرید تا در این مسیر همراهتان باشیم :)