Note that there are some explanatory texts on larger screens.

plurals
  1. POEF Code First to SQL Azure
    primarykey
    data
    text
    <p>I am using EF Code First to create a database on local .\SQLEXPRESS.</p> <p>Among others. I have these 2 classes:</p> <pre><code>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&lt;Category&gt; 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&lt;Shop&gt; Shops { get; set; } } </code></pre> <p>and a method in DbContext:</p> <pre><code>modelBuilder.Entity&lt;Item&gt;() .HasRequired(p =&gt; p.Category) .WithMany(a =&gt; a.Items) .HasForeignKey(a =&gt; a.CategoryID) .WillCascadeOnDelete(false); modelBuilder.Entity&lt;Category&gt;() .HasRequired(a =&gt; a.Shop) .WithMany(a =&gt; a.Categories) .HasForeignKey(a =&gt; a.ShopID) .WillCascadeOnDelete(false); modelBuilder.Entity&lt;Shop&gt;() .HasOptional(a =&gt; a.ShoppingPlace) .WithMany(a =&gt; a.Shops) .HasForeignKey(a =&gt; a.ShoppingPlaceID) .WillCascadeOnDelete(false); modelBuilder.Entity&lt;ShoppingPlace&gt;() .HasOptional(a =&gt; a.ShoppingCenter) .WithMany(a =&gt; a.ShoppingPlaces) .HasForeignKey(a =&gt; a.ShoppingCenterID) .WillCascadeOnDelete(false); </code></pre> <p>Why I can't create Shop without creating and populating ShopingPlace. How to achieve that?</p> <p><strong>EDIT:</strong></p> <p>Tried with:</p> <pre><code>modelBuilder.Entity&lt;Shop&gt;() .HasOptional(a =&gt; a.ShoppingPlace) .WithOptionalPrincipal(); modelBuilder.Entity&lt;ShoppingPlace&gt;() .HasOptional(a =&gt; a.ShoppingCenter) .WithOptionalPrincipal(); </code></pre> <p>and it passed, but what is the difference? And why in SQL Server i am allowed to see <code>ShoppingPlaceID</code> and <code>ShoppingPlace_ShopingPlaceID</code> when in the case of <code>Item</code> and <code>Category</code> i see only one?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload