Я использую EF Code First для создания базы данных на локальном компьютере. \ SQLEXPRESS.
Среди прочих.У меня есть эти 2 класса:
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; }
}
и метод в 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);
Почему я не могу создать Магазин без создания и заполнения ShopingPlace.Как этого добиться?
РЕДАКТИРОВАТЬ:
Пробовал с:
modelBuilder.Entity<Shop>()
.HasOptional(a => a.ShoppingPlace)
.WithOptionalPrincipal();
modelBuilder.Entity<ShoppingPlace>()
.HasOptional(a => a.ShoppingCenter)
.WithOptionalPrincipal();
и прошло, но какая разница?И почему в SQL Server мне разрешено видеть ShoppingPlaceID
и ShoppingPlace_ShopingPlaceID
, когда в случае Item
и Category
я вижу только один?