virI ASP.NET MVC4 アプリケーションで Entity Framework Code First を使用してルックアップ テーブルを作成しようとしています。それは私たちの場所のためのもので、2 つのエントリがあるはずです。それらに何らかの ID を関連付ける必要があるため、Software テーブルに LocationID が格納されます。ただし、それらを作成すると、ソフトウェア テーブルの各行にエントリが作成されます。
ここに私のソフトウェアクラスがあります:
public class Software
{
public int Id { get; set; }
public virtual List<SoftwareType> SoftwareTypes { get; set; }
public virtual List<Location> Locations { get; set; }
public virtual List<SoftwarePublisher> Publishers { get; set; }
[Required]
[StringLength(128)]
public string Title { get; set; }
[Required]
[StringLength(10)]
public string Version { get; set; }
[Required]
[StringLength(128)]
public string SerialNumber { get; set; }
[Required]
[StringLength(3)]
public string Platform { get; set; }
[StringLength(1000)]
public string Notes { get; set; }
[Required]
[StringLength(15)]
public string PurchaseDate { get; set; }
public bool Suite { get; set; }
public string SubscriptionEndDate { get; set; }
//[Required]
//[StringLength(3)]
public int SeatCount { get; set; }
}
これが私の Locations クラスです:
public class Location
{
public int Id { get; set; }
[Required]
[StringLength(20)]
public string LocationName { get; set; }
public virtual Software Software { get; set; }
}
シード メソッドへの Global.asax 呼び出しは次のとおりです。
Database.SetInitializer(new SampleData());
using (var context = new Context())
{
context.Database.Initialize(true);
}
これが私のコンテキストです:
public class Context : DbContext
{
public DbSet<Software> Software { get; set; }
public DbSet<Location> Locations { get; set; }
public DbSet<SoftwarePublisher> SoftwarePublishers { get; set; }
public DbSet<SoftwareType> SoftwareTypes { get; set; }
public Context()
{
Configuration.ProxyCreationEnabled = false;
}
}
そして、ここに私のシードがあります:
public class SampleData : CreateDatabaseIfNotExists<Context>
{
protected override void Seed(Context context)
{
new List<Software> {
new Software {
Title = "Adobe Creative Suite",
Version = "CS6",
SerialNumber = "1234634543",
Platform = "Mac",
Notes = "Macs rock!",
PurchaseDate = "2012-12-04",
Suite = true,
SubscriptionEndDate = null,
SeatCount = 0,
SoftwareTypes = new List<SoftwareType>
{ new SoftwareType { Type="Suite" }},
Locations = new List<Location>
{ new Location { LocationName = "Paradise" }},
Publishers = new List<SoftwarePublisher>
{ new SoftwarePublisher { Publisher = "Adobe" }}},
...other similar rows...
}.ForEach(s => context.Software.Add(s));
base.Seed(context);
context.SaveChanges();
}
Locations などの新しいリストを作成しているため (SoftwareType や Publisher などの他の項目を修正する必要がありますが、Locations に注目しましょう)、Locations テーブルに新しい行を作成しています。Locations テーブルに 2 つのエントリがあり、Software テーブルの ID がそれらの 2 つのエントリのいずれかを指すように、クラスを再構築するにはどうすればよいですか? 私は Entity Framework の初心者であることを心に留めておいてください。ありがとう。