나는이 ApplicationUser
ASP.NET 정체성 프레임 워크에 의해 만들어진 테이블과 또 다른 테이블 고객을 사용하려는 one-to-one relation
. PK
의 ApplicationUser
테이블은 Id
형식 문자열. 그리고 Customer
테이블에는 FK
문자열 유형이 있습니다. 나는 테이블 의 Id
유형 을 변경할 수 없습니다 ApplicationUser
. 그러나 고객 테이블에는 int 유형의 ID (자동 번호) 열이 있어야합니다. 이건 괜찮아.
그리고이 Customer
테이블은 테이블 one-to-many
과 관계가 Contact
있습니다.
모두 Customer
와 Contact
테이블은 고유 ID 열이 Id
들. Contact
표는이 CustomerId
열을. AspNetUser
테이블 이 없으면 코드는 먼저 one-to-many
이들 id
및 CustomerId
열을 사용하여 관계를 생성 합니다. 때문에 AspNetuser
표가 one-to-one
와의 관계를 Customer
,이 사용하지 않습니다 CustomerId
. 그러나 코드는 먼저이 관계 (Customer_UserId)
를 구축하기 위해 자체 문자열 키 를 생성합니다 one-to-many
.
어떻게 사용합니까 Customer
관련하는 테이블 열을 Contact
테이블에게 CustomerId
EF6 코드 첫 번째 방법을 사용하여 관계를 매핑하는 데 열을?
다음은 완전한 코드입니다.
ApplicationUser
public class ApplicationUser : IdentityUser
{
//Navigation properties
public Customer Customer { get; set; }
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
{
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
return userIdentity;
}
}
고객
public class Customer
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Index(IsUnique = true)]
public int Id { get; set; }
[Key, ForeignKey("ApplicationUser")]
public string UserId { get; set; }
public string BusinessName { get; set; }
//Navigation properties
[ForeignKey("UserId")]
public ApplicationUser ApplicationUser { get; set; }
public virtual ICollection<Contact> Contacts { get; set; }
}
접촉
public class Contact
{
public int Id { get; set; }
public int CustomerId { get; set; }
public string LastName { get; set; }
public string FirstName { get; set; }
public string OfficeLocation { get; set; }
//Navigation properties
[Required]
public Customer Customer { get; set; }
}
ApplicationDbContext
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext(): base("DefaultConnection", throwIfV1Schema: false){}
static ApplicationDbContext()
{
// Set the database intializer which is run once during application start
// This seeds the database with admin user credentials and admin role
Database.SetInitializer<ApplicationDbContext>(new ApplicationDbInitializer());
}
public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Customer>()
.HasRequired(u => u.ApplicationUser).WithRequiredDependent(c => c.Customer);
}
public System.Data.Entity.DbSet<WebServer.Models.Customer> Customers { get; set; }
public System.Data.Entity.DbSet<WebServer.Models.Contact> Contacts { get; set; }
}
나는 그것을 알아. ApplicationUser
및 Customer
테이블은 one-to one
두 테이블이 동일합니다 PK
( String Id
in ApplicationUser
및 String UserId
in Customer
). 논리적으로 우리는이 두 테이블 PK
을 유형이있는 하나의 테이블로 생각할 수 있습니다 string
. Contact
테이블의 자식 Customer
테이블. 따라서 one-to-many
간의 관계 Customer
와 Contact
. 따라서 Contact
테이블에는 Foreign Key
문자열 유형 이 있어야 합니다. 따라서 CustomerId
모델의은 string
대신 int
. 또한 코드 우선이 필드를 사용하도록 지시하려면 [ForeignKey("CustomerId")]
속성에 public Customer Customer { get; set; }
속성 을 추가해야 합니다.
따라서 새 연락처 모델은 다음과 같아야합니다.
public class Contact
{
public int Id { get; set; }
public string CustomerId { get; set; }
public string LastName { get; set; }
public string FirstName { get; set; }
public string OfficeLocation { get; set; }
//Navigation properties
[ForeignKey("CustomerId")]
[Required]
public Customer Customer { get; set; }
}
추신 : 원래 응용 프로그램에서는이 테이블 계층 구조에 다른 수준이 있습니다. 연락처는 많은 주소를 가질 수 있으므로 Address
테이블은 s 와 함께 FK
유형 int
을 사용 하여 다른 관계 를 형성 할 수 있습니다 .Contact
PK
one-to-many
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다