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 8675011
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T19:50:45+00:00 2026-06-12T19:50:45+00:00

I have 2 classes which hold data for restaurants. Status.cs public class Status {

  • 0

I have 2 classes which hold data for restaurants.

Status.cs

public class Status
{

    [Required]
    public int StatusId { get; set; }

    [Required]
    [DisplayName("Status")]
    public string Name { get; set; }
}

Restaurant.cs

public class Restaurant
{
    public int RestaurantId { get; set; }

    [Required]
    public string Name { get; set; }

    [Required]
    [EmailAddress]
    public string Email { get; set; }

    [Required]
    public string Telephone { get; set; }

    [Required]
    public int StatusId { get; set; }



    // NAVIGATION PROPERTIES

    public virtual Status Status { get; set; }
}

I am trying to seed data into the database.

First, I seed the status table, then I wish to seed the Restaurants table.

var statuses = new List<Status>
{
    new Status { Name = "Draft" },
    new Status { Name = "Live" },
    new Status { Name = "Invisible" },
    new Status { Name = "Discontinued" }
};
statuses.ForEach(a => context.Statuses.Add(a));

var restaurants = new List<Restaurant> 
{ 
    new Restaurant { Name = "The Restaurant Name", Email = "email@restaurant.com", Telephone = "012345566787", StatusId = 1 }
};
restaurants.ForEach(a=>context.Restaurants.Add(a));

base.seed(context);

This doesn’t work, because it doesn’t like the way I try to seed StatusId = 1 into Restaurant.

I know that I can create a new Status within the new Restaurant, however, I have already seeded the statuses into the database. How do I go about setting the Restaurant’s Status to Draft???

Do I have to do this every time???

new Restaurant { Name = "The Restaurant Name", Email = "email@restaurant.com", Telephone = "012345566787", StatusId = new Status { Name = "Draft"} }

Will this not literally generate a new row in the status table called Status every time I create a new Restaurant with the status “Draft”?

  • 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-12T19:50:46+00:00Added an answer on June 12, 2026 at 7:50 pm

    Don’t set the StatusId, set the Status:

    var restaurants = new List<Restaurant> 
    { 
        new Restaurant { 
            Name = "The Restaurant Name", 
            Email = "email@restaurant.com", 
            Telephone = "012345566787", 
            Status = statuses.Single(s=>s.Name=="Draft") }
    };
    

    That way the StatusID is set by the context and uses the same ID as the Status.

    If you want to reference the Status in the list by Index (e.g. statuses[0] for "Draft") that works too; selecting by name is more appropriate, though, IMHO.

    EDIT

    Just read the end of your question – so to answer that:

    When you create a “new” Status and attach it to your Restaurant, the context doesn’t know that you want to use the existing status of "Draft", so it does what you tell it – creates a new one. When you attach an existing Status from the context is used that ID since it’s the same instance.

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

Sidebar

Related Questions

I have classes which have automatic properties only like public customerName {get; set;}. They
I have a set of classes which hold only data (no operations have been
i have two data classes which hold only data members(no functions). One is CallTask
Which .net class is designed to hold an image data in memory. For example,
I have two abstract classes which will hold references to each other.How / can
I have two classes in short here they are: public final class ServerMain {
I have a set of classes that are used to prepare data for serialization.
I have an array which I store different classes which again hold different arrays.
Consider I have some abstract Vehicle class and car, truck, motorcycle abstract classes which
I have four classes which share some arrangement of four properties. I have currently

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.