درود و سپاس مهندس حاجی شریفی گرامی
از وقتی که در انجمن کدی که نوشتید و توضیحاتی را که دادید دیدم دارم تمرین میکنم، ولی کمی گیج شدم
کدهای کلاس هارو براتون میزارم ببینید کجای کارم مشکل داره
کلاس UserRole public class UserRole
{
[Key]
public int UserID { get; set; }
public Nullable<int> RoleID { get; set; }
public virtual Role Role { get; set; }
public virtual User User { get; set; }
}
کلاس User public class User
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int UserID { get; set; }
[Required]
[MaxLength(50)]
public string UserName { get; set; }
[MaxLength(50)]
public string Password { get; set; }
[MaxLength(50)]
public string FirstName { get; set; }
[MaxLength(50)]
public string LastName { get; set; }
[MaxLength(50)]
public string Email { get; set; }
[MaxLength(50)]
public string Mobile { get; set; }
public Nullable<byte> SecurityQuestion { get; set; }
[MaxLength(50)]
public string AnswerQuestion { get; set; }
public Nullable<bool> IsActive { get; set; }
public virtual UserRole UserRole { get; set; }
}
کلاس Role public class Role
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int RoleID { get; set; }
[MaxLength(50)]
public string RoleName { get; set; }
public virtual ICollection<UserRole> UserRoles { get; set; }
public virtual ICollection<RolePermission> RolePermissions { get; set; }
}
کلاس RolePermission public class RolePermission
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int RolePermissionID { get; set; }
public Nullable<int> RoleID { get; set; }
public Nullable<int> PermissionID { get; set; }
public virtual Role Role { get; set; }
public virtual Permission Permission { get; set; }
}
کلاس Permission public class Permission
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int PermissionID { get; set; }
[MaxLength(50)]
public string PermissioName { get; set; }
public Nullable<int> Parent { get; set; }
public virtual ICollection<RolePermission> RolePermissions { get; set; }
}
من میخوام رابطه هام شبیه به این دیاگرام بشه
از Update-Database -Force برای آپدیت دیتابیسم که استفاده میکنم این خطا رو میده - البته خطاهایی دیگه هم داده ولی الان این رو نشون میده
Unable to determine the principal end of an association between the types 'CafeNetManagement.Models.User' and 'CafeNetManagement.Models.Tanzimat.UserRole'. The principal end of this association must be explicitly configured using either the relationship fluent API or data annotations.
مهندس این رو یه توضیحی میدید؟ چطور باید تنظیمش کنیم؟ و اگر مثلا بیش از 100 جدول داشتیم و روابط بین آن ها مختلف بود چطور باید تنظیمش کنیم؟
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
//modelBuilder.Entity<Govahiha>()
// .HasRequired(g => g.Contact)
// .WithMany(c => c.Govahihas)
// .HasForeignKey(f => f.ContactId)
// .WillCascadeOnDelete(true);
//modelBuilder.Entity<User>().HasOptional(x => x.Accesslevel)
//.WithRequired(x => x.User)
//.WillCascadeOnDelete(true);
}
البته اگر بشه در کنار آموزش، اون دیتابیس رو هم تبدیل کنید تا عملی تر متوجه بشم ممنون میشم (ببخشید واقعا
)