using System; using System.Collections.Generic; using System.Data.Entity;//在NuGet中获取后,手动引用之 namespace EF_Test { class Program { static void Main(string[] args) { var d = DateTime.Now.Date.ToString("yyyyMM"); var destination = new Destination { Country = "Indonesia", Description = "EcoTourism at its best in exquisite Bali", Name = "Bali" }; using (var context = new EF_TestContext()) { context.Destinations.Add(destination); context.SaveChanges(); } Console.WriteLine("OK"); Console.ReadLine(); } } //实体类1 ===> 映射为表:Destinations中的记录 public class Destination { public int DestinationId { get; set; } public string Name { get; set; } public string Country { get; set; } public string Description { get; set; } public byte[] Photo { get; set; } public List<Lodging> Lodgings { get; set; } } //实体类2 ===> 映射为表:Lodgings中的记录 public class Lodging { public int LodgingId { get; set; } public string Name { get; set; } public string Owner { get; set; } public bool IsResort { get; set; } public Destination Destination { get; set; } } public class EF_TestContext : DbContext { //表Destinations public DbSet<Destination> Destinations { get; set; } //表Lodgings public DbSet<Lodging> Lodgings { get; set; } } }
时间: 2024-11-05 15:18:00