我是 ASP.NET 和 EF 的新手,但有 Ruby MVC 经验。我正在开发一个具有一对多关系的复杂应用程序,因此我创建了一个较小的项目,我可以使用它来测试 CodeFirst 生成,还有什么比使用吉他项目测试更有趣!你们所有的音乐家都知道一对多的关系,因为一位吉他手拥有几把吉他和放大器。这段代码的工作原理是在我播种时创建数据库和表 - 只是想要一些关于如何做得更好以及任何可能的陷阱的建议?
谢谢
namespace GuitarCollector.Models
{
public class Guitarist
{
public Guitarist()
{
Guitars = new List<Guitar>();
Amplifiers = new List<Amplifier>();
}
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public List<Guitar> Guitars { get; set; }
public List<Amplifier> Amplifiers { get; set; }
}
public class Guitar
{
//Primary Key
public int Id { get; set; }
//Foreign Key
public int GuitaristId { get; set; }
public int YearOfManufacture { get; set; }
public string Make { get; set; }
public string Model { get; set; }
public string Finish { get; set; }
public string SerialNumber { get; set; }
public double AppraisedValue { get; set; }
Guitarist Guitarist { get; set; }
}
public class Amplifier
{
//Primary Key
public int Id { get; set; }
//Foriegn Key
public int GuitaristId { get; set; }
public int YearOfManufacture { get; set; }
public int Wattage { get; set; }
public string Make { get; set; }
public string Model { get; set; }
public string SerialNumber { get; set; }
public double AppraisedValue { get; set; }
public Guitarist Guitarist { get; set; }
}
}
命名空间 GuitarCollector.DAL { 公共类 GuitaristContext : DbContext {
public DbSet<Guitarist> Guitarists { get; set; }
public DbSet<Guitar> Guitars { get; set; }
public DbSet<Amplifier> Amplifiers { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
}
}
}