[英]Asp.net mvc 4 foreign key in models
I have these tables: 我有这些表:
public class udb:DbContext
{
public DbSet<klasa> Keuni { get; set; }
public DbSet<student> Seuni { get; set; }
}
public class student
{
[Key]
public int studentid { get; set; }
public string emristudent { get; set; }
public int nota { get; set; }
[ForeignKey("klasaid"), Column(Order = 0)]
public int klasaid { get; set; }
}}
public class klasa
{ [Key]
public int klasaid { get; set; }
public string emriklases { get; set; }
public string vendodhja { get; set; }
public virtual ICollection<student> students { get; set; }
}
The problem is that I want the klasaid to be a foreign key in students so when the user adds a new student the klasaid should exist in klasa model.I use Asp.net mvc 4.Can sb help me? 问题是我希望klasaid成为学生的外键,所以当用户添加新学生时,klasaid应该存在于klasa模型中。我使用Asp.net mvc 4.Can sb可以帮助我吗?
It should look like this: 它应该如下所示:
public class student
{
[Key]
public int studentid { get; set; }
public string emristudent { get; set; }
public int nota { get; set; }
public int klasaid {get;set;}
[ForeignKey("klasaid")]
public klasa Klasa{ get; set; }
}
You can use various database initialization strategies as follows: 您可以使用以下各种数据库初始化策略:
public class udb:DbContext
{
public udb()
{
Database.SetInitializer<udb>(new CreateDatabaseIfNotExists<udb>());
}
public DbSet<klasa> Keuni { get; set; }
public DbSet<student> Seuni { get; set; }
}
One suggestion- follow proper naming conventions . 一个建议 - 遵循正确的命名约定 。 Please, have a look into this link for more details.
请查看此链接以获取更多详细信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.