多重性在 Role Code First 中无效
Multiplicity is not valid in Role Code First
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);
}
下一页>
包管理器控制台
PM> Add-Migration
cmdlet Add-Migration at command pipeline position 1
Supply values for the following parameters:
Name: MvcDbContext
错误:
One or more validation errors were detected during model generation:
Borrowers_Book_Source: : Multiplicity is not valid in Role
'Borrowers_Book_Source' in relationship 'Borrowers_Book'. Because the
Dependent Role refers to the key properties, the upper bound of the
multiplicity of the Dependent Role must be '1'.
Please help me?
问题来自 Visual Studio。
祝你好运。
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);
}
下一页> 包管理器控制台
PM> Add-Migration
cmdlet Add-Migration at command pipeline position 1
Supply values for the following parameters:
Name: MvcDbContext
错误:
One or more validation errors were detected during model generation:
Borrowers_Book_Source: : Multiplicity is not valid in Role 'Borrowers_Book_Source' in relationship 'Borrowers_Book'. Because the Dependent Role refers to the key properties, the upper bound of the multiplicity of the Dependent Role must be '1'. Please help me?
问题来自 Visual Studio。 祝你好运。