Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8952987
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T13:59:45+00:00 2026-06-15T13:59:45+00:00

virI’m trying to create a lookup table in my ASP.NET MVC4 application with Entity

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T13:59:47+00:00Added an answer on June 15, 2026 at 1:59 pm

    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

    this.HasMany(i => i.Softwares)
                .WithMany(c => c.Locations)
                .Map(mc =>
                {
                    mc.MapLeftKey("SoftwareId");
                    mc.MapRightKey("LocationId");
                    mc.ToTable("SoftwareLocations");
                });
    

    I got that snippet from this blog post

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.