EF Code First to SQL Azure

Posted by Predrag Pejic on Stack Overflow See other posts from Stack Overflow or by Predrag Pejic
Published on 2012-03-23T11:14:51Z Indexed on 2012/03/23 11:30 UTC
Read the original article Hit count: 598

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?

© Stack Overflow or respective owner

Related posts about c#

Related posts about database