Làm cách nào để thêm khóa ngoại vào lớp mô hình?

Thuộc tính ForeignKey được sử dụng để định cấu hình khóa ngoại trong mối quan hệ giữa hai thực thể trong EF 6 và EF Core. Nó ghi đè các quy ước mặc định. Theo quy ước mặc định, EF tạo một thuộc tính làm thuộc tính khóa ngoại khi tên của thuộc tính khớp với thuộc tính khóa chính của thực thể liên quan

Chữ ký khóa ngoại.

using System.ComponentModel.DataAnnotations.Schema;

public class Student
{
    public int StudentID { get; set; }
    public string StudentName { get; set; }
        
    [ForeignKey["Standard"]]
    public int StandardRefId { get; set; }
    public Standard Standard { get; set; }
}

public class Standard
{
    public int StandardId { get; set; }
    public string StandardName { get; set; }
    
    public ICollection Students { get; set; }
}
3

  • Tên. Tên của thuộc tính điều hướng được liên kết hoặc tên của [các] khóa ngoại được liên kết

Xem xét ví dụ sau về mối quan hệ một-nhiều giữa các thực thể

using System.ComponentModel.DataAnnotations.Schema;

public class Student
{
    public int StudentID { get; set; }
    public string StudentName { get; set; }
        
    //Foreign key for Standard
    public int StandardId { get; set; }
    public Standard Standard { get; set; }
}

public class Standard
{
    public int StandardId { get; set; }
    public string StandardName { get; set; }
    
    public ICollection Students { get; set; }
}

Ví dụ trên mô tả mối quan hệ một-nhiều giữa các thực thể

using System.ComponentModel.DataAnnotations.Schema;

public class Student
{
    public int StudentID { get; set; }
    public string StudentName { get; set; }
        
    [ForeignKey["Standard"]]
    public int StandardRefId { get; set; }
    public Standard Standard { get; set; }
}

public class Standard
{
    public int StandardId { get; set; }
    public string StandardName { get; set; }
    
    public ICollection Students { get; set; }
}
4 và
using System.ComponentModel.DataAnnotations.Schema;

public class Student
{
    public int StudentID { get; set; }
    public string StudentName { get; set; }
        
    [ForeignKey["Standard"]]
    public int StandardRefId { get; set; }
    public Standard Standard { get; set; }
}

public class Standard
{
    public int StandardId { get; set; }
    public string StandardName { get; set; }
    
    public ICollection Students { get; set; }
}
5. Để biểu diễn mối quan hệ này, lớp
using System.ComponentModel.DataAnnotations.Schema;

public class Student
{
    public int StudentID { get; set; }
    public string StudentName { get; set; }
        
    [ForeignKey["Standard"]]
    public int StandardRefId { get; set; }
    public Standard Standard { get; set; }
}

public class Standard
{
    public int StandardId { get; set; }
    public string StandardName { get; set; }
    
    public ICollection Students { get; set; }
}
4 bao gồm thuộc tính
using System.ComponentModel.DataAnnotations.Schema;

public class Student
{
    public int StudentID { get; set; }
    public string StudentName { get; set; }
        
    [ForeignKey["Standard"]]
    public int StandardRefId { get; set; }
    public Standard Standard { get; set; }
}

public class Standard
{
    public int StandardId { get; set; }
    public string StandardName { get; set; }
    
    public ICollection Students { get; set; }
}
7 với thuộc tính tham chiếu
using System.ComponentModel.DataAnnotations.Schema;

public class Student
{
    public int StudentID { get; set; }
    public string StudentName { get; set; }
        
    [ForeignKey["Standard"]]
    public int StandardRefId { get; set; }
    public Standard Standard { get; set; }
}

public class Standard
{
    public int StandardId { get; set; }
    public string StandardName { get; set; }
    
    public ICollection Students { get; set; }
}
5 và lớp thực thể
using System.ComponentModel.DataAnnotations.Schema;

public class Student
{
    public int StudentID { get; set; }
    public string StudentName { get; set; }
        
    [ForeignKey["Standard"]]
    public int StandardRefId { get; set; }
    public Standard Standard { get; set; }
}

public class Standard
{
    public int StandardId { get; set; }
    public string StandardName { get; set; }
    
    public ICollection Students { get; set; }
}
5 bao gồm thuộc tính điều hướng bộ sưu tập
using System.ComponentModel.DataAnnotations.Schema;

public class Student
{
    public int StudentID { get; set; }
    public string StudentName { get; set; }
        
    [ForeignKey["Standard"]]
    public int StandardRefId { get; set; }
    public Standard Standard { get; set; }
}

public class Standard
{
    public int StandardId { get; set; }
    public string StandardName { get; set; }
    
    public ICollection Students { get; set; }
}
1. Tên thuộc tính
using System.ComponentModel.DataAnnotations.Schema;

public class Student
{
    public int StudentID { get; set; }
    public string StudentName { get; set; }
        
    [ForeignKey["Standard"]]
    public int StandardRefId { get; set; }
    public Standard Standard { get; set; }
}

public class Standard
{
    public int StandardId { get; set; }
    public string StandardName { get; set; }
    
    public ICollection Students { get; set; }
}
7 trong thực thể
using System.ComponentModel.DataAnnotations.Schema;

public class Student
{
    public int StudentID { get; set; }
    public string StudentName { get; set; }
        
    [ForeignKey["Standard"]]
    public int StandardRefId { get; set; }
    public Standard Standard { get; set; }
}

public class Standard
{
    public int StandardId { get; set; }
    public string StandardName { get; set; }
    
    public ICollection Students { get; set; }
}
4 khớp với thuộc tính khóa chính của thực thể
using System.ComponentModel.DataAnnotations.Schema;

public class Student
{
    public int StudentID { get; set; }
    public string StudentName { get; set; }
        
    [ForeignKey["Standard"]]
    public int StandardRefId { get; set; }
    public Standard Standard { get; set; }
}

public class Standard
{
    public int StandardId { get; set; }
    public string StandardName { get; set; }
    
    public ICollection Students { get; set; }
}
5, do đó,
using System.ComponentModel.DataAnnotations.Schema;

public class Student
{
    public int StudentID { get; set; }
    public string StudentName { get; set; }
        
    [ForeignKey["Standard"]]
    public int StandardRefId { get; set; }
    public Standard Standard { get; set; }
}

public class Standard
{
    public int StandardId { get; set; }
    public string StandardName { get; set; }
    
    public ICollection Students { get; set; }
}
7 trong thực thể
using System.ComponentModel.DataAnnotations.Schema;

public class Student
{
    public int StudentID { get; set; }
    public string StudentName { get; set; }
        
    [ForeignKey["Standard"]]
    public int StandardRefId { get; set; }
    public Standard Standard { get; set; }
}

public class Standard
{
    public int StandardId { get; set; }
    public string StandardName { get; set; }
    
    public ICollection Students { get; set; }
}
4 sẽ tự động trở thành thuộc tính khóa ngoại và cột tương ứng trong bảng db cũng sẽ là cột khóa ngoại, như minh họa bên dưới

Thuộc tính

using System.ComponentModel.DataAnnotations.Schema;

public class Student
{
    public int StudentID { get; set; }
    public string StudentName { get; set; }
        
    [ForeignKey["Standard"]]
    public int StandardRefId { get; set; }
    public Standard Standard { get; set; }
}

public class Standard
{
    public int StandardId { get; set; }
    public string StandardName { get; set; }
    
    public ICollection Students { get; set; }
}
7 ghi đè quy ước mặc định cho khóa ngoại Nó cho phép chúng ta chỉ định thuộc tính khóa ngoại trong thực thể phụ thuộc có tên không khớp với thuộc tính khóa chính của thực thể chính

Thuộc tính

using System.ComponentModel.DataAnnotations.Schema;

public class Student
{
    public int StudentID { get; set; }
    public string StudentName { get; set; }
        
    [ForeignKey["Standard"]]
    public int StandardRefId { get; set; }
    public Standard Standard { get; set; }
}

public class Standard
{
    public int StandardId { get; set; }
    public string StandardName { get; set; }
    
    public ICollection Students { get; set; }
}
8 có thể được áp dụng theo ba cách

  1. using System.ComponentModel.DataAnnotations.Schema;
    
    public class Student
    {
        public int StudentID { get; set; }
        public string StudentName { get; set; }
            
        [ForeignKey["Standard"]]
        public int StandardRefId { get; set; }
        public Standard Standard { get; set; }
    }
    
    public class Standard
    {
        public int StandardId { get; set; }
        public string StandardName { get; set; }
        
        public ICollection Students { get; set; }
    }
    
    9 về thuộc tính khóa ngoại vô hướng trong thực thể phụ thuộc
  2. using System.ComponentModel.DataAnnotations.Schema;
    
    public class Student
    {
        public int StudentID { get; set; }
        public string StudentName { get; set; }
            
        public int StandardRefId { get; set; }
        
        [ForeignKey["StandardRefId"]]
        public Standard Standard { get; set; }
    }
    
    public class Standard
    {
        public int StandardId { get; set; }
        public string StandardName { get; set; }
        
        public ICollection Students { get; set; }
    }
    
    0 trên thuộc tính điều hướng tham chiếu có liên quan trong thực thể phụ thuộc
  3. using System.ComponentModel.DataAnnotations.Schema;
    
    public class Student
    {
        public int StudentID { get; set; }
        public string StudentName { get; set; }
            
        public int StandardRefId { get; set; }
        
        [ForeignKey["StandardRefId"]]
        public Standard Standard { get; set; }
    }
    
    public class Standard
    {
        public int StandardId { get; set; }
        public string StandardName { get; set; }
        
        public ICollection Students { get; set; }
    }
    
    0 trên thuộc tính điều hướng trong thực thể chính

[ForeignKey] trên thuộc tính khóa ngoại trong thực thể phụ thuộc

using System.ComponentModel.DataAnnotations.Schema;

public class Student
{
    public int StudentID { get; set; }
    public string StudentName { get; set; }
        
    [ForeignKey["Standard"]]
    public int StandardRefId { get; set; }
    public Standard Standard { get; set; }
}

public class Standard
{
    public int StandardId { get; set; }
    public string StandardName { get; set; }
    
    public ICollection Students { get; set; }
}
7 trên thuộc tính khóa ngoại trong thực thể phụ thuộc và tên thuộc tính điều hướng liên quan có thể được chỉ định làm tham số như minh họa bên dưới

using System.ComponentModel.DataAnnotations.Schema;

public class Student
{
    public int StudentID { get; set; }
    public string StudentName { get; set; }
        
    [ForeignKey["Standard"]]
    public int StandardRefId { get; set; }
    public Standard Standard { get; set; }
}

public class Standard
{
    public int StandardId { get; set; }
    public string StandardName { get; set; }
    
    public ICollection Students { get; set; }
}

Trong ví dụ trên, thuộc tính

using System.ComponentModel.DataAnnotations.Schema;

public class Student
{
    public int StudentID { get; set; }
    public string StudentName { get; set; }
        
    [ForeignKey["Standard"]]
    public int StandardRefId { get; set; }
    public Standard Standard { get; set; }
}

public class Standard
{
    public int StandardId { get; set; }
    public string StandardName { get; set; }
    
    public ICollection Students { get; set; }
}
7 được áp dụng trên thuộc tính điều hướng
using System.ComponentModel.DataAnnotations.Schema;

public class Student
{
    public int StudentID { get; set; }
    public string StudentName { get; set; }
        
    [ForeignKey["Standard"]]
    public int StandardRefId { get; set; }
    public Standard Standard { get; set; }
}

public class Standard
{
    public int StandardId { get; set; }
    public string StandardName { get; set; }
    
    public ICollection Students { get; set; }
}
1 trong thực thể chính
using System.ComponentModel.DataAnnotations.Schema;

public class Student
{
    public int StudentID { get; set; }
    public string StudentName { get; set; }
        
    [ForeignKey["Standard"]]
    public int StandardRefId { get; set; }
    public Standard Standard { get; set; }
}

public class Standard
{
    public int StandardId { get; set; }
    public string StandardName { get; set; }
    
    public ICollection Students { get; set; }
}
5. Điều này sẽ tạo một cột khóa ngoại
using System.ComponentModel.DataAnnotations.Schema;

public class Student
{
    public int StudentID { get; set; }
    public string StudentName { get; set; }
        
    public int StandardRefId { get; set; }
    
    [ForeignKey["StandardRefId"]]
    public Standard Standard { get; set; }
}

public class Standard
{
    public int StandardId { get; set; }
    public string StandardName { get; set; }
    
    public ICollection Students { get; set; }
}
4 trong bảng
using System.ComponentModel.DataAnnotations.Schema;

public class Student
{
    public int StudentID { get; set; }
    public string StudentName { get; set; }
        
    [ForeignKey["Standard"]]
    public int StandardRefId { get; set; }
    public Standard Standard { get; set; }
}

public class Standard
{
    public int StandardId { get; set; }
    public string StandardName { get; set; }
    
    public ICollection Students { get; set; }
}
1 trong cơ sở dữ liệu

Khóa ngoại trong mô hình hóa dữ liệu là gì?

Khóa ngoại là cột hoặc tập hợp các cột trong bảng có giá trị tương ứng với giá trị của khóa chính trong bảng khác . Để thêm một hàng có giá trị khóa ngoại đã cho, phải tồn tại một hàng trong bảng liên quan có cùng giá trị khóa chính.

Làm cách nào để thêm tham chiếu khóa ngoại vào danh tính ASP NET MVC 5?

Tại lớp Địa chỉ của bạn. địa chỉ lớp công khai { chuỗi công khai MyAddress { get; . cs] chuỗi công khai UserId { get;
Your ApplicationUser Class: public virtual ICollection Addresses { get; set; }.

Làm cách nào để xác định khóa ngoại trong Spring Boot?

Sử dụng khóa ngoại. Hãy tạo một lớp Người dùng với tên bảng là người dùng. .
Mối quan hệ một đối một bằng cách sử dụng Khóa chính được chia sẻ. Trong chiến lược 1-1 này, thay vì tạo một cột mới address_id , chúng ta sẽ đánh dấu cột khóa chính user_id của bảng địa chỉ làm khóa ngoại cho bảng người dùng. .
Sử dụng Bảng tham gia trong JPA

Chủ Đề