2016-04-05 14 views
1

ile Tabloları birleştirme. Sorgu, AspNetUser'lerde kimliğe bağlı.İç Bu benim cshtml sayfasından görebilirsiniz Yönetici sütun IssuedTo olan Ef6 Kimlik Web Uygulaması</p> <p>ile bir MVC5 olan 2 farklı değişkenler

ben için yaptıklarını yapıyor çalıştı Ne IssueTo o 1 yüzden eğer işaret bunun yerine 1.

enter image description here enter image description here

Andy Domagas göstermesi gerekir adını görüntülemek çalışıyorum Kullanıcı, User yerine IssuedTo için bir sanal ApplicationUser özelliği oluşturarak. Bunu [ForeignKey("IssuedTo")] public virtual ApplicationUser assignedAdmin{ get; set; } ekleyerek yaptım ve sonra benim View @Html.DisplayFor(modelItem => item.IssuedUser.LastName) kullanıyorum ama alınıyorum FOREIGN KEY kısıtlaması döngüleri veya birden çok kaskatlı yol neden olabilir.

IdentityModel.cs (ApplicationUser) (Katma TicketsIssuedTo Koleksiyon)

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 (Katma IssuedID ve 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; } 

} 

Bunu düzeltmek için bana bir baş ağrısı çok fazla veriyor Bu yüzden sorum var, bunun için başka seçenek var mı?

benim TicketController Endeksi yönteminde bir Linq deyimi kullanabilir ve SEÇ Ad Soyadı vb İÇ Bilet Table.Administrator (IssuedTo) ile kullanıcı Table.id KATILIN ??

Gert önerisi sonrasında Güncelleme kod u üzerinde FluentAPI IdentityModel.cs (İçinde OnModelCreating yöntemi)

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

Hata kullanmak

amaçlanan temsilci türüne Lambda ifade dönüştürülemez çünkü bloktaki dönüş türlerinden bazıları, dolaylı olarak döndürülebilir dönüş türü olan RecreationalServi'ye dönüştürülebilir değildir

örtülü System.Collections.Generic.ICollection RecreationalServicesTicketingSystem.Models tip System.Collections.Generic.ICollection RecreationalServicesTicketingSystem.Models.ApplicationUser dönüştürülemez TicketsIssuedTo üzerinde

Hata cesTicketingSystem .Ticket. Açık bir dönüşüm var (bir dökümün eksik mi?Bu DbContext alt sınıfta OnModelCreating geçersiz kılma olduğunu

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

: Eğer bir yabancı anahtarından silmek basamaklı kaldırmak gerekir çünkü)

cevap

1

Yalnızca, akıcı haritalama ile bunu çözebiliriz.

Ama var ...

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

... bu olmalıdır: Ben base.OnModelCreating (ModelBuilder) `var ApplicationDbContext` benim` kamu sınıfının içinde

public virtual ICollection<Ticket> TicketsIssuedTo { get; set; } 
+0

; ' ve 'modelBuilder.Conventions.Remove (); '(Basamaklı silme işlemini kaldırmaya çalıştığım için) Bunları da kaldırmalı mıyım? – TykiMikk

+1

Bu sözleşmeyi kaldırmazdım. Basamaklı silme büyük bir performans artışı sağlayabilir. Basamaklı silme sadece istenmediği yerde kaldırırdım. –

+0

ModelBuilder.Entity () 'un sonunda bir '.'' Ye ihtiyacınız olduğunu düşünmüyorum. Ayrıca birkaç hata alıyorum ve bu sorunu kendi başıma güncelledim. – TykiMikk

İlgili konular