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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T23:44:08+00:00 2026-05-31T23:44:08+00:00

I am using EF Code First to create a database on local .\SQLEXPRESS. Among

  • 0

I am using EF Code First to create a database on local .\SQLEXPRESS.

Among others. I have these 2 classes:

public class Shop
{
    public int ShopID { get; set; }

    [Required(AllowEmptyStrings = false, ErrorMessage = "You must enter a name!")]
    [MaxLength(25, ErrorMessage = "Name must be 25 characters or less")]
    public string Name { get; set; }

    [Required(AllowEmptyStrings = false, ErrorMessage = "You must enter an address!")]
    [MaxLength(30, ErrorMessage = "Address must be 30 characters or less")]
    public string Address { get; set; }

    [Required(AllowEmptyStrings = false, ErrorMessage = "You must enter a valid city name!")]
    [MaxLength(30, ErrorMessage = "City name must be 30 characters or less")]
    public string City { get; set; }

    [Required(AllowEmptyStrings = false, ErrorMessage = "You must enter a phone number!")]
    [MaxLength(14, ErrorMessage = "Phone number must be 14 characters or less")]
    public string Phone { get; set; }

    [MaxLength(100, ErrorMessage = "Description must be 50 characters or less")]
    public string Description { get; set; }

    [Required(AllowEmptyStrings = false, ErrorMessage = "You must enter a WorkTime!")]
    public DateTime WorkTimeBegin { get; set; }

    [Required(AllowEmptyStrings = false, ErrorMessage = "You must enter a WorkTime!")]
    public DateTime WorkTimeEnd { get; set; }

    public DateTime? SaturdayWorkTimeBegin { get; set; }
    public DateTime? SaturdayWorkTimeEnd { get; set; }
    public DateTime? SundayWorkTimeBegin { get; set; }
    public DateTime? SundayWorkTimeEnd { get; set; }

    public int ShoppingPlaceID { get; set; }
    public virtual ShoppingPlace ShoppingPlace { get; set; }

    public virtual ICollection<Category> Categories { get; set; }
}

public class ShoppingPlace
{
    [Key]
    public int ShopingplaceID { get; set; }

    [Required(AllowEmptyStrings = false, ErrorMessage = "You must enter a name!")]
    [MaxLength(25, ErrorMessage = "Name must be 25 characters or less")]
    public string Name { get; set; }

    [Required(AllowEmptyStrings = false, ErrorMessage = "You must enter an address!")]
    [MaxLength(50, ErrorMessage = "Address must be 50 characters or less")]
    public string Address { get; set; }

    [Required(AllowEmptyStrings = false, ErrorMessage = "You must enter a city name!")]
    [MaxLength(30, ErrorMessage = "City must be 30 characters or less")]
    public string City { get; set; }

    [Required(AllowEmptyStrings = false, ErrorMessage = "You must enter a valid phone number!")]
    [MaxLength(14, ErrorMessage = "Phone number must be 14 characters or less")]
    public string Phone { get; set; }

    public int ShoppingCenterID { get; set; }
    public virtual ShoppingCenter ShoppingCenter { get; set; }

    public virtual ICollection<Shop> Shops { get; set; }
}

and a method in DbContext:

modelBuilder.Entity<Item>()
            .HasRequired(p => p.Category)
            .WithMany(a => a.Items)
            .HasForeignKey(a => a.CategoryID)
            .WillCascadeOnDelete(false);

        modelBuilder.Entity<Category>()
            .HasRequired(a => a.Shop)
            .WithMany(a => a.Categories)
            .HasForeignKey(a => a.ShopID)
            .WillCascadeOnDelete(false);

        modelBuilder.Entity<Shop>()
            .HasOptional(a => a.ShoppingPlace)
            .WithMany(a => a.Shops)
            .HasForeignKey(a => a.ShoppingPlaceID)
            .WillCascadeOnDelete(false);

        modelBuilder.Entity<ShoppingPlace>()
            .HasOptional(a => a.ShoppingCenter)
            .WithMany(a => a.ShoppingPlaces)
            .HasForeignKey(a => a.ShoppingCenterID)
            .WillCascadeOnDelete(false);

Why I can’t create Shop without creating and populating ShopingPlace. How to achieve that?

EDIT:

Tried with:

modelBuilder.Entity<Shop>()
            .HasOptional(a => a.ShoppingPlace)
            .WithOptionalPrincipal();

        modelBuilder.Entity<ShoppingPlace>()
            .HasOptional(a => a.ShoppingCenter)
            .WithOptionalPrincipal();

and it passed, but what is the difference? And why in SQL Server i am allowed to see
ShoppingPlaceID and ShoppingPlace_ShopingPlaceID when in the case of Item and Category i see only one?

  • 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-05-31T23:44:09+00:00Added an answer on May 31, 2026 at 11:44 pm

    Although it is hard to tell without looking at the code you are using to create the instances of the record or the exact message, it is probably because your foreign key ShoppingPlaceID is not nullable.

    public int ShoppingPlaceID { get; set; } 
    public virtual ShoppingPlace ShoppingPlace { get; set; } 
    

    change

    public int ShoppingPlaceID { get; set; } 
    

    to

    public int? ShoppingPlaceID { get; set; } 
    

    you should also add the following to your OnModelCreating() routine

    modelBuilder.Entity<Shop>()
        .HasOptional(m => m.ShoppingPlace)
        .WithMany(m=>m.someproperty)   // if this is not many then change this
        .HasForeignKey(m=>m.ShoppingPlaceID );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a code first database. The tables causing me issues are public class
I successfully created database on my local server using Code First, migrated it to
We're using EF Code first, and have a data context for our sales database.
I am using Entity Framework Code First to create a database table. My model
I have a table called Tournaments which is created using the EF code first
When defining a string in a class using Code First, the default for the
I'm using the Code First approach to build the database in this problem. I
I have an AXIS2/JAX-WS web service using a code first implementation (yes I know,
I have been using Entity Framework CTP with Code-First as in this tutorial by
I am using EF 4.1 Code First with SQL Server 2008 R2. I have

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.