简体   繁体   English

首先在代码中在ASP.NET MVC中的表中添加外键

[英]Add Foreign Key in Table in ASP.NET MVC in code first

I want to have a one to many relationship in between instructor and course. 我想在讲师和课程之间建立一对多的关系。 How can I achieve it? 我该如何实现? Below is what I have as of now. 以下是我截至目前的情况。

public class Instructor
{
    //[Key]
    public int id { get; set; }
    public string name { get; set; }
    public string address { get; set; }
    public string email { get; set; }
}
public class Course
{
    //[Key]
    public int id { get; set; }
    [DisplayName("Course")]
    [Required(ErrorMessage = "CSExxx")]
    public string progId { get; set; }
    public string name { get; set; }
    public string semester { get; set; }
    public string description { get; set; }
    public virtual Instructor instructor { get; set; }
}

You need to add a virtual collection of Course to Instructor 您需要将课程的虚拟集合添加到教师中

public class Instructor
{
    //[Key]
    public int id { get; set; }
    public string name { get; set; }
    public string address { get; set; }
    public string email { get; set; }

    public virtual ICollection<Course> Courses { get; set; }
}
public class Course
{
    //[Key]
    public int id { get; set; }
    [DisplayName("Course")]
    [Required(ErrorMessage = "CSExxx")]
    public string progId { get; set; }
    public string name { get; set; }
    public string semester { get; set; }
    public string description { get; set; }
    public virtual Instructor instructor { get; set; }
}

In OnModelCreating in your DataContext 在OnModel中创建您的DataContext

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<Instructor>().HasMany(i => i.Courses).WithRequired(c => c.Instructor).HasForeignKey(c => c.instructor).WillCascadeOnDelete(true);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Code First在ASP.NET MVC 5 Identity 2.0中为AspNetUser表(Id列)在Customer表(CreatedBy列)中添加外键 - How to add a Foreign key in Customer table (CreatedBy column) for AspNetUser table (Id column) in ASP.NET MVC 5 Identity 2.0 using Code First 使用实体框架的代码优先方法在ASP.NET MVC应用程序中缺少外键关系 - Foreign key relationship missing in ASP.NET MVC app using code-first approach with Entity Framework ASP.NET MVC中一条记录的多个外键关系,代码优先 - Multiple foreign key relationships for one record in asp.net mvc, code-first 如何在代码优先的ASP.NET MVC 5应用程序中以一对多关系正确设置外键约束 - How to correctly set foreign key constraint in one to many relationship in code first ASP.NET MVC 5 application asp.net MVC-代码第一个外键可让我输入任何内容 - asp.net MVC - code first foreign key lets me input anything 首先在ASP.net MVC实体框架代码中创建外键关系 - Create Foreign Key Relationship in ASP.net MVC Entity Framework Code First ASP.NET MVC 3 EF代码优先-外键问题 - ASP.NET MVC 3 EF Code First - Problems with FOREIGN KEYS 如何在ASP.NET MVC4中添加外键数据 - How to add foreign key data in asp.net mvc4 Asp.net MVC导出表到Excel | 外键显示 - Asp.net MVC Export Table to Excel | Foreign Key Display ASP.NET MVC 5 ApplicationUser作为外键 - ASP.NET MVC 5 ApplicationUser as foreign key
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM