Как сделать ФК Обязательным при использовании конвенций? - PullRequest
0 голосов
/ 21 октября 2018

Предположим, что у меня есть такие сущности.

public class Student
{
    public int StudentId { get; set; }
    public string StudentName { get; set; }
}

public class Grade
{
    public int GradeId { get; set; }
    public string GradeName { get; set; }
    public string Section { get; set; }

    public ICollection<Student> Students { get; set; } 
}

Платформа сущностей сгенерирует для меня таблицу FK в таблице Student, но она будет обнуляться.Я хочу, чтобы это было необходимо, а также я хочу использовать это соглашение, а не другие.Является ли это возможным?Если это невозможно, вы рекомендуете какую-либо другую технику?

1 Ответ

0 голосов
/ 21 октября 2018

Посмотрите на приведенные ниже примеры

public class Student
{
    public int Id { get; set; }
    public string Name { get; set; }

    public int GradeId { get; set; }
    public Grade Grade { get; set; }
}

public class Grade
{

    public int GradeId { get; set; }
    public string GradeName { get; set; }

    public ICollection<Student> Student { get; set; }
}

В приведенном выше примере объект Student включает свойство внешнего ключа GradeId со своим ссылочным свойством Grade.Это создаст связь «один ко многим» со столбцом внешнего ключа NotNull в таблице «Студенты», как показано ниже. больше здесь

Полный образец

 public partial class Product
{
    public int Id { get; set; }
    public int? UserId { get; set; }   // set nullable FK

    public User User { get; set; }
}
public partial class Ticket
{
    public int Id { get; set; }
    public int UserId { get; set; }  // set non nullable FK
    public string Title { get; set; }

    public User User { get; set; }
}
public partial class User
{
    public User()
    {
        Products = new HashSet<Product>();
        Tickets = new HashSet<Ticket>();
    }

    public int Id { get; set; }
    public string Name { get; set; }

    public ICollection<Product> Products { get; set; }
    public ICollection<Ticket> Tickets { get; set; }
}

 public partial class AngulartestContext : DbContext
{
    public AngulartestContext()
    {
    }

    public AngulartestContext(DbContextOptions<AngulartestContext> options)
        : base(options)
    {
    }

    public virtual DbSet<Product> Products { get; set; }
    public virtual DbSet<Ticket> Tickets { get; set; }
    public virtual DbSet<User> Users { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        if (!optionsBuilder.IsConfigured)
        {
  #warning To protect potentially sensitive information in your connection string, you should move it out of source code. See http://go.microsoft.com/fwlink/?LinkId=723263 for guidance on storing connection strings.
            optionsBuilder.UseSqlServer("Data Source=.;Initial Catalog=db;Persist Security Info=True;User ID=sa;Password=123456");
        }
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Product>(entity =>
        {
            entity.Property(e => e.Id).ValueGeneratedNever();

            entity.HasOne(d => d.User)
                .WithMany(p => p.Products)
                .HasForeignKey(d => d.UserId)
                .HasConstraintName("FK_Products_Users");
        });

        modelBuilder.Entity<Ticket>(entity =>
        {
            entity.Property(e => e.Id).ValueGeneratedNever();

            entity.Property(e => e.Title).HasMaxLength(50);

            entity.HasOne(d => d.User)
                .WithMany(p => p.Tickets)
                .HasForeignKey(d => d.UserId)
                .OnDelete(DeleteBehavior.ClientSetNull)
                .HasConstraintName("FK_Tickets_Users");
        });

        modelBuilder.Entity<User>(entity =>
        {
            entity.Property(e => e.Id).ValueGeneratedNever();

            entity.Property(e => e.Name).HasMaxLength(50);
        });

        OnModelCreatingPartial(modelBuilder);
    }

    partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...