1

asp.net mvc 프로그램에서 첫 번째 마이그레이션 코드를 사용하고 있습니다.초기 마이그레이션에 엔티티 추가

프로젝트에서 제공되는 기본 인증 및 역할을 사용하고 있습니다. 내가 마이그레이션을 사용할 때

지금은 자동으로 여기에 편집 wqish 특정 테이블의 excample 인 테이블 등

모두를 생성하는 마이그레이션 클래스를 생성합니다.

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

이제이 표에 설명 필드를 추가하고 싶습니다. 방금 데이터베이스에 추가 한 다음에는 코드를 처음 마이그레이션하는 것이 쉽습니다.

1> Entity 프레임 워크는 초기 마이그레이션을 위해 모든 명령을 어디서 얻습니까? 프로젝트에 생성 된 테이블을 지정하는 모델을 볼 수있는 모델이 없기 때문입니다.

2> 생성 된 원본 테이블을 수정하거나 편집하려면 어떻게해야합니까? 초기 마이그레이션 폴더 편집을 시도했지만 작동하지 않습니다?

(내 생각으로) 롤과 사용자 모델이 프레임 워크에 저장되어있는 것이지 테이블의 구조를 가져 오는 곳이 아닌가? 그렇다면 더 많은 속성을 추가하기 위해 기본 모델을 확장 할 수 있습니까?

// You can add profile data for the user by adding more properties to your ApplicationUser class, please visit http://go.microsoft.com/fwlink/?LinkID=317594 to learn more. 
public class ApplicationUser : IdentityUser 
{ 
    [Required] 
    [MaxLength(50)] 
    [Display(Name = "Email Address")] 
    public string email { get; set; } 
} 

그게 내가 기본 사용자에게 전자 메일 주소 필드를 추가하는 방법 : 난 당신이 ApplicationUser 위해 그것을 할 수있어 왜냐하면, 난 여기의 예, 그래서 이전에 완료했다. 나는 역할과 함께 이것을 할 수 없을까?

답변

1

ApplicationUser에 완벽하게 맞고 다른 항목과 마찬가지로 계속 진행하십시오. 사용자 테이블에 설명을 추가하려는 경우. ApplicationUser 모델에 추가하기 만하면됩니다. 외래 키 및 가상 속성에 신경 쓰지 마십시오.

public class ApplicationUser : IdentityUser 
    { 
     [Required] 
     public string FirstName { get; set; } 
     [Required] 
     public string LastName { get; set; } 
     public string GroupName { get; set; } 
     [Required] 
     public string Email { get; set; } 
     [Required] 
     [StringLength(15)] 
     public string Phone { get; set; } 
     public string Remark { get; set; } 
     public DateTime? BirthDate { get; set; } 
     public DateTime ValidFrom { get; set; } 
     public DateTime ValidUntil { get; set; } 

     // Foreign keys 
     [ForeignKey("Bank")] 
     public string AccountNumber { get; set; } 
     [ForeignKey("Address")] 
     public int? AddressId { get; set; } 

     public virtual Bank Bank { get; set; } 
     public virtual Address Address { get; set; } 
     public virtual ICollection<Request> Requests { get; set; } 
    } 

역할 클래스를 편집하는 경우, 당신은 당신의 클래스에 IdentityRole을 상속 속성을 추가해야합니다 :

public class ApplicationRole : IdentityRole 
{ 
    public string Description { get; set; } 
} 

업데이트-데이터베이스를 사용할 때 실행됩니다 새로운 마이그레이션 클래스를 생성 프레임 워크 명령.

public class ApplicationDbContext : IdentityDbContext<ApplicationUser> 
{ 
    public ApplicationDbContext() 
     : base("DefaultConnection") 
    { 
    } 

    ... 
    public DbSet<Product> Products { get; set; } 
    new public DbSet<ApplicationRole> Roles { get; set; } 
} 
+0

I을 : 당신은 (새로운 잊지 마세요) 다음과 같이 수행 ApplicationDbContext의 역할을 덮어해야

public class IdentityManager { public bool RoleExists(string name) { var rm = new RoleManager<ApplicationRole>(new RoleStore<ApplicationRole>(new ApplicationDbContext())); return rm.RoleExists(name); } public bool CreateRole(string name) { var rm = new RoleManager<ApplicationRole>(new RoleStore<ApplicationRole>(new ApplicationDbContext())); var idResult = rm.Create(new ApplicationRole(name)); return idResult.Succeeded; } } 

:

당신은 당신의 identityManager (여기에 사용 ApplicationRole)을 변경해야 'Roles' 테이블에 설명을 추가하고 싶습니다. 내가 어떻게 그럴 수 있니? 연장 할 수업은 무엇입니까? – Zapnologica

+0

게시물에 추가되었습니다. –

+0

문제가 있습니다. 나는 네가 제안한대로 해왔다. 하지만 이제는 데이터베이스'db.Roles'에 접근 할 때 역할은'ApllicationRole'이 아닌'IntentyRole' 유형입니다. 이것을 어떻게 바꿀 수 있습니까? NB – Zapnologica