1. 程式人生 > >Entity Framework Complex Types

Entity Framework Complex Types

string public work 分享 base framework name entity con

1.模型類

技術分享圖片
    public class Order
    {
        public int OrderId { get; set; }
        public string OrderNumber { get; set; }
        public DateTime OrderData { get; set; }
        public string DisposeDate { get; set; }

        public Address ShipAddress { get; set; }
        public Address BillAddress { get
; set; } } public class Address { public string FirstName { get; set; } public string LastName { get; set; } public string Addr { get; set; } public string Street { get; set; } public string City { get; set; } public string ZipCode { get; set
; } public string Email { get; set; } public string Phone { get; set; } public string State { get; set; } public string Country { get; set; } }
View Code

2.寫入數據庫

技術分享圖片
            using (var db = new BaseDbContext())
            {
                var address = new Address { 
                    FirstName
="浙江省", LastName="溫嶺市" }; var address2 = new Address { FirstName = "浙江省", LastName = "椒江" }; Order order = new Order { ShipAddress = address, BillAddress = address2, OrderData=DateTime.Now, OrderNumber="001" }; db.Orders.Add(order); db.SaveChanges(); }
View Code

Entity Framework Complex Types