2016-12-24 6 views
1

ApplicationUser(IdentityUser)から継承したクラスで、削除、 クラスは次のとおりです。これは私のApplicationUserクラスはカスケードは、私はASP .NETアイデンティティにApplicationUserから継承されている2つのクラスを持っている

ある

public class ApplicationUser : IdentityUser 
{ 
    public string Name { get; set; } 

    public string Surname { get; set; } 

    public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager) 
    { 
     // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType 
     var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie); 
     // Add custom user claims here 
     return userIdentity; 
    } 
} 

それを継承し、二つのクラス:

public class User : ApplicationUser 
{ 
    public virtual ICollection<Repair> Repairs { get; set; } 

    public virtual ICollection<Vehicle> Vehicles { get; set; } 
} 

public class Repairer : ApplicationUser 
{ 
    public virtual ICollection<Repair> Repairs { get; set; } 
} 

私はコードファーストマイグレーションを実行すると、両方のクラスが同じテーブル内whicですhは適切なDiscriminatorと役割を持つAspNetUsersテーブルです。問題は、私がシステムからいくつかのユーザーを削除すると、すべての車両と修理がデータベースから削除されてもらいたいのですが、データベースではディスクリミネータの隣に適切な区別がないので、データベース内の適切なテーブル(Vehicle and Repair)を参照する外部キー制約でカスケード削除を行うので、代わりに例外が発生するか、dbの外部キーがnullに設定されます。

カスケード削除を達成する方法ですか、それ以外はすべてうまく動作しているため、モデルを変更する必要があります。

ありがとうございます!

答えて

0

ここで同様の問題を解決しようとしているので、これまでに学んだことを私は貢献します。私の場合は、特定の(エンティティ)のプロパティを含む二つの継承クラスでベースApplicationUserあります

public class ApplicationUser : IdentityUser { 

} 

public class Server : ApplicationUser { 

} 

public class HumanUser : ApplicationUser { 

    [Required] 
    public virtual GlobalInventory GlobalInventory { get; set; } 
} 

public class GlobalInventory { 

    [Key, ForeignKey("User")] 
    public string UserId { get; set; } 

    [Required] 
    public virtual HumanUser User { get; set; } 
} 

を私もApplicationDbContextに次の流体APIのコードを追加しました:

protected override void OnModelCreating(DbModelBuilder modelBuilder) 
    { 
     modelBuilder.Entity<HumanUser>().HasOptional(x => x.GlobalInventory).WithRequired(x => x.User).WillCascadeOnDelete(true); 
     base.OnModelCreating(modelBuilder); 
    } 

ここでは、移行部分ですそのコード・ファーストはGlobalInventoryのためにチャック・アウトする。 HumanUserへの外部キー接続に「cascadeDelete:true」フラグがないことに注目してください。それは私が親を削除すると、エラーが発生することを意味します:

 CreateTable(
      "dbo.GlobalInventories", 
      c => new 
       { 
        UserId = c.String(nullable: false, maxLength: 128), 
       }) 
      .PrimaryKey(t => t.UserId) 
      .ForeignKey("dbo.Users", t => t.UserId) 
      .Index(t => t.UserId); 

しかし - 私はそうのように、代わりに基本ApplicationUserクラスのGlobalInventoryプロパティを加えた場合..

public class ApplicationUser : IdentityUser { 

    [Required] 
    public virtual GlobalInventory GlobalInventory { get; set; } 
} 

public class HumanUser : ApplicationUser { 

    [Required] 
    public virtual GlobalInventory GlobalInventory { get; set; } 
} 


public class GlobalInventory { 

    [Key, ForeignKey("User")] 
    public string UserId { get; set; } 

    [Required] 
    public virtual ApplicationUser User { get; set; } 
} 

と変更しますそれに応じてモデルの建物は:

protected override void OnModelCreating(DbModelBuilder modelBuilder) 
    { 
     modelBuilder.Entity<ApplicationUser>().HasOptional(x => x.GlobalInventory).WithRequired(x => x.User).WillCascadeOnDelete(true); 
     base.OnModelCreating(modelBuilder); 
    } 

その後GlobalInventoryための移行コードは、右に見える、と私はGlobalInventoryも削除されることを知っているユーザーを削除することができます。

 CreateTable(
      "dbo.GlobalInventories", 
      c => new 
       { 
        UserId = c.String(nullable: false, maxLength: 128), 
       }) 
      .PrimaryKey(t => t.UserId) 
      .ForeignKey("dbo.AspNetUsers", t => t.UserId, cascadeDelete: true) 
      .Index(t => t.UserId); 

(ApplicationUserは抽象クラスとして定義しているとき、これはまた、正常に動作します)

私も、基本クラスに固有のエンティティプロパティを添付する必要はありませんので、私は現在、こだわっているところ、これがありますで。これは、継承クラスに適用するとFluid APIカスケード・オン・デリート指定が無視されるようになりますが、ベースクラスに適用された場合は無視されます。

関連する問題