Multiplicity在Role Code First中无效

时间:2017-12-16 12:51:19

标签: asp.net asp.net-mvc-4 ef-code-first migration ef-migrations

Books.cs

public class Books
    {
        public int BookId { get; set; }
        public int AuthorId { get; set; }
        public int CategoryId { get; set; }
        public int PublishinHouseId { get; set; }
        public string IsbnNo { get; set; }
        public string BookName { get; set; }
        public int NumberOfPages { get; set; }
        public string Location { get; set; }
        public virtual Authors Author { get; set; }
        public virtual  Categories Category { get; set; }
        public virtual PublishingHouses PublishingHouse { get; set; }

        public virtual ICollection<Borrowers> Borrowers { get; set; }
    }

Students.cs

public class Students
    {
        public int StudentId { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public int GenderId { get; set; }
        public  int DepartmentId { get; set; }
        public DateTime DateOfBirth { get; set; }
        public string Email { get; set; }
        public string Phone { get; set; }
        public string Address { get; set; }
        public virtual Genders Gender { get; set; }
        public  virtual Departments Department { get; set; }

        public virtual ICollection<Borrowers> Borrowers { get; set; }
    }

Borrowers.cs

public class Borrowers
    {
        public int BorrowerId { get; set; }
        public int BookId { get; set; }
        public int StudentId { get; set; }
        public DateTime DateGiven { get; set; }
        public DateTime DateReceived { get; set; }
        public virtual Books Book { get; set; }
        public  virtual Students Student { get;set ;}
    }

BorrowerMap.cs

public BorrowerMap()
        {
            //Primary Key
            this.HasKey(t => t.BorrowerId);
            //Property
            this.Property(t => t.BorrowerId).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
            this.Property(t => t.DateGiven).IsRequired().HasColumnType("Date");
            this.Property(t => t.DateReceived).IsRequired().HasColumnType("Date");          
            this.ToTable("Borrowers");
            this.Property(t => t.BorrowerId).HasColumnName("BorrowerId");
            this.Property(t => t.BookId).HasColumnName("BookId");
            this.Property(t => t.StudentId).HasColumnName("StudentId");
            this.Property(t => t.DateGiven).HasColumnName("DateGiven");
            this.Property(t => t.DateReceived).HasColumnName("DateReceived");            
            this.HasRequired(p => p.Book).WithMany(p => p.Borrowers).HasForeignKey(f => f.BorrowerId);
            this.HasRequired(p => p.Student).WithMany(p => p.Borrowers).HasForeignKey(f => f.StudentId);
        }

下一步&GT; 软件包管理器控制台

PM> Add-Migration
cmdlet Add-Migration at command pipeline position 1
Supply values for the following parameters:
Name: MvcDbContext

错误:

  

在模型生成期间检测到一个或多个验证错误:

     

Borrowers_Book_Source ::多重性在角色中无效   &#39; Borrowers_Book_Source&#39;在关系&#39; Borrowers_Book&#39;。因为   依赖角色是指关键属性,即上界   依赖角色的多样性必须是&#39; 1&#39;。   请帮帮我?

1 个答案:

答案 0 :(得分:0)

问题来自Visual Studio。 祝你好运。