virI’m trying to create a lookup table in my ASP.NET MVC4 application with Entity Framework Code First. It is for our locations and there should be two entries. I need to have some sort of ID associated with them so there is a LocationID stored in my Software table. However, when I create them there is an entry to created for each row in the Software Table.
Here is my Software class:
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; }
}
Here is my Locations class:
public class Location
{
public int Id { get; set; }
[Required]
[StringLength(20)]
public string LocationName { get; set; }
public virtual Software Software { get; set; }
}
Here is my Global.asax call to a seed method:
Database.SetInitializer(new SampleData());
using (var context = new Context())
{
context.Database.Initialize(true);
}
Here is my context:
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;
}
}
And here is my seeding:
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();
}
Because I am creating a new list for things like Locations (I need to fix the other things like SoftwareType and Publisher, but let’s focus on Locations), it is creating a new row in my Locations table. How do I restructure my classes, so that I have two entries in my Locations table and then IDs in my Software table pointing to one of those two entries? Please bear in mind I am an Entity Framework newbie, so please try to be explicit. Thanks.
I think you want a many to many relationship between Software and Locations. To do that, you’ll need to create a join table (also called a linking table). I believe you want to do this in your OnModelCreating override
I got that snippet from this blog post