具有2个不同变量的内部连接表

时间:2016-04-05 20:45:36

标签: c# entity-framework linq

这是一个带有EF6身份Web应用程序的MVC5

您可以在我的cshtml页面中看到的管理员列是IssuedTo。 Issueto与我的AspNetUsers中的id相关联。

我试图显示IssueTo指向的名称,所以如果它1,它应该显示Andy Domagas而不是1。

enter image description here enter image description here

我尝试通过为IssuedTo而不是User创建虚拟ApplicationUser属性来做我为User所做的事情。我是通过添加[ForeignKey("IssuedTo")] public virtual ApplicationUser assignedAdmin{ get; set; }然后在我的使用@Html.DisplayFor(modelItem => item.IssuedUser.LastName)的视图中完成此操作但我得到引入FOREIGN KEY约束可能会导致循环或多个级联路径

IdentityModel.cs(ApplicationUser)(添加了TicketsIssuedTo集合)

namespace RecreationalServicesTicketingSystem.Models
{
    public class ApplicationUserLogin : IdentityUserLogin<int> { }
    public class ApplicationUserClaim : IdentityUserClaim<int> { }
    public class ApplicationUserRole : IdentityUserRole<int> { }

    public class ApplicationRole : IdentityRole<int, ApplicationUserRole>, IRole<int>
    {
        public string Description { get; set; }

        public ApplicationRole() : base() { }
        public ApplicationRole(string name)
            : this()
        {
            this.Name = name;
        }

        public ApplicationRole(string name, string description)
            : this(name)
        {
            this.Description = description;
        }
    }

    public class ApplicationUser : IdentityUser<int, ApplicationUserLogin, ApplicationUserRole, ApplicationUserClaim>, IUser<int>
    {
        public async Task<ClaimsIdentity>
            GenerateUserIdentityAsync(UserManager<ApplicationUser, int> manager)
        {
            var userIdentity = await manager
                .CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
            return userIdentity;
        }

        public bool IsAdministrator { get; set; }
        [StringLength(50, MinimumLength = 1)]

        public string LastName { get; set; }
        [StringLength(50, MinimumLength = 1, ErrorMessage = "First name cannot be longer than 50 characters.")]

        [Column("FirstName")]
        public string FirstMidName { get; set; }

        public string FullName
        {
            get { return FirstMidName + " " + LastName; }
        }
        [DataType(DataType.Date)]
        [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
        public DateTime EnrollmentDate { get; set; }
        public int DepartmentID { get; set; }
        [ForeignKey("DepartmentID")]
        public virtual Department Department { get; set; }
        public int DepotID { get; set; }
        [ForeignKey("DepotID")]
        public virtual Depot Depot { get; set; }
        public virtual ICollection<Ticket> Tickets { get; set; }

        public virtual ICollection<ApplicationUser> TicketsIssuedTo { get; set; }




    }


    public class ApplicationDbContext
        : IdentityDbContext<ApplicationUser, ApplicationRole, int,
        ApplicationUserLogin, ApplicationUserRole, ApplicationUserClaim>
    {
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
            modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();

            modelBuilder.Entity<Ticket>()
            .HasRequired(t => t.IssuedUser)
            .WithMany(u => u.TicketsIssuedTo)
            .HasForeignKey(t => t.IssueID)
            .WillCascadeOnDelete(false);
        }
        public ApplicationDbContext()
            : base("DefaultConnection")
        {
        }

        static ApplicationDbContext()
        {
            Database.SetInitializer<ApplicationDbContext>(new ApplicationDbInitializer());
        }


        public static ApplicationDbContext Create()
        {
            return new ApplicationDbContext();
        }


        public DbSet<Ticket> Tickets { get; set; }
        public DbSet<Category> Categories { get; set; }
        public DbSet<Department> Departments { get; set; }
        public DbSet<Depot> Depots { get; set; }


    }



    public class ApplicationUserStore :
    UserStore<ApplicationUser, ApplicationRole, int,
    ApplicationUserLogin, ApplicationUserRole, ApplicationUserClaim>, IUserStore<ApplicationUser, int>, IDisposable
    {
        public ApplicationUserStore()
            : this(new IdentityDbContext())
        {
            base.DisposeContext = true;
        }

        public ApplicationUserStore(DbContext context)
            : base(context)
        {
        }
    }


    public class ApplicationRoleStore
    : RoleStore<ApplicationRole, int, ApplicationUserRole>,
    IQueryableRoleStore<ApplicationRole, int>,
    IRoleStore<ApplicationRole, int>, IDisposable
    {
        public ApplicationRoleStore()
            : base(new IdentityDbContext())
        {
            base.DisposeContext = true;
        }

        public ApplicationRoleStore(DbContext context)
            : base(context)
        {
        }
    }

}

Ticket.cs(已添加IssuedID和IssuedUser)

public class Ticket
{
    public int? TicketID { get; set; }
    [Required(ErrorMessage = "Please enter the description")]
    public string Issue { get; set; }
    [Display(Name = "Administrator")]
    [Required(ErrorMessage = "Please select the Administrator")]
    public int IssuedTo { get; set; }
    public int Author { get; set; }

    [DisplayFormat(NullDisplayText = "No Priority")]
    public Priority Priority { get; set; }
    [Required]
    [ForeignKey("CategoryID")]
    public virtual Category Category { get; set; }
    public int CategoryID { get; set; }

    public int UserID { get; set; }  
    [ForeignKey("UserID")]
    public virtual ApplicationUser User { get; set; }

    public int IssueID { get; set; }
    [ForeignKey("IssueID")]
    public virtual ApplicationUser IssuedUser { get; set; }

}

这给我带来了太多麻烦来解决这个问题所以我的问题是还有其他选择吗?

我可以在TicketController Index方法中使用Linq语句吗? SELECT FirstName LastName等INNER JOIN User Table.id with Ticket Table.Administrator(IssuedTo)??

在Gert建议使用FluentAPI之后更新了代码 IdentityModel.cs(内部OnModelCreating方法)

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<Ticket>()
    .HasRequired(t => t.IssuedUser)
    .WithMany(u => u.TicketsIssuedTo)
    .HasForeignKey(t => t.IssueID)
    .WillCascadeOnDelete(false);
 }

错误

  

无法将lambda表达式转换为预期的委托类型,因为   块中的某些返回类型不可隐式转换   到委托返回类型RecreationalServicesTicketingSystem

TicketsIssuedTo错误

  

无法隐式转换System.Collections.Generic.ICollection类型    RecreationalServicesTicketingSystem.Models.ApplicationUser 到System.Collections.Generic.ICollection   的 RecreationalServicesTicketingSystem.Models.Ticket 即可。存在显式转换(您是否错过了演员?)

1 个答案:

答案 0 :(得分:1)

您只能通过流畅的映射解决此问题,因为您需要从一个外键中删除级联删除:

modelBuilder.Entity<Ticket>()
            .HasRequired(t => t.IssuedUser)
            .WithMany(u => u.TicketsIssuedTo)
            .HasForeignKey(t => t.IssueID)
            .WillCascadeOnDelete(false);

这是OnModelCreating子类中的DbContext覆盖。

但你已经......

public virtual ICollection<ApplicationUser> TicketsIssuedTo { get; set; }

......应该是:

public virtual ICollection<Ticket> TicketsIssuedTo { get; set; }
相关问题