EF基础二

EF基础二,EF5,NET4.5,ANNOTATION AND FLUENT API,MOST FOCUS ON ENTITYTYPECONFIGURATION.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.Entity;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.ModelConfiguration;

namespace BlueSea
{
    class Program
    {
        static void Main(string[] args)
        {
            Database.SetInitializer(new DropCreateDatabaseIfModelChanges<BlueSeaContext>());
            InsertDestination();
            InsertTrip();
            UpdateTrip();
            InsertPserson();
            UpdatePerson();
        }
        private static void InsertDestination()
        {
            var destination = new Destination
            {
                Country = "Indonesia",
                Description = "Tourism in Bali",
                Name = "Bali"
            };
            using (var context = new BlueSeaContext())
            {
                context.Destinations.Add(destination);
                context.SaveChanges();
            }
        }
        private static void InsertTrip()
        {
            var trip = new Trip
            {
                CostUSD = 800,
                StatrtDate = new DateTime(2013, 3, 15),
                EndDate = new DateTime(2013, 3, 20)
            };
            using (var context = new BlueSeaContext())
            {
                context.Trips.Add(trip);
                context.SaveChanges();
            }
        }
        private static void UpdateTrip()
        {
            using (var cntext = new BlueSeaContext())
            {
                var trip = cntext.Trips.FirstOrDefault();
                trip.CostUSD = 750;
                cntext.SaveChanges();
            }
        }  
        private static void InsertPserson()
        {
            var person = new Person
            {
                FirstName = "Martin",
                LastName = "Jack",
                SocialSecurityNumber = 12345678
            };
            using (var context = new BlueSeaContext())
            {
                context.People.Add(person);
                context.SaveChanges();
            }
        }
        private static void UpdatePerson()
        {
            using (var context = new BlueSeaContext())
            {
                var person = context.People.FirstOrDefault();
                person.FirstName = "Peter";
                context.SaveChanges();
            }
        }
    }
    public class Destination
    {
        public int DestinatonID { get; set; }
        public string Name { get; set; }
        public string Country { get; set; }
        public string Description { get; set; }
        public byte[] Photo { get; set; }

        public virtual ICollection<Lodging> Lodgings { get; set; }
    }
    public class DestinationConfiguration : EntityTypeConfiguration<Destination>
    {
        public DestinationConfiguration()
        {
            HasKey(d => d.DestinatonID);
            Property(d => d.Name).IsRequired();
            Property(d => d.Description).HasMaxLength(500);
            Property(d => d.Photo).HasColumnType("image");
        }
    }
    public class Lodging
    {
        public int LodgingID { get; set; }
        public string Name { get; set; }
        public string Owener { get; set; }
        public bool IsResort { get; set; }
        public decimal MileFromAirport { get; set; }

        public virtual Destination Destination { get; set; }
    }
    public class LodgingConfiguration : EntityTypeConfiguration<Lodging>
    {
        public LodgingConfiguration()
        {
            Property(l => l.Name).IsRequired().HasMaxLength(200);
            Property(l => l.MileFromAirport).HasPrecision(8, 1);
            Property(l => l.Owener).IsUnicode(false);
        }
    }
    public class Trip
    {
        public Guid Identifier { get; set; }
        public DateTime StatrtDate { get; set; }
        public DateTime EndDate { get; set; }
        public decimal CostUSD { get; set; }
        public byte[] RowVVersion { get; set; }
    }
    public class TripConfiguration : EntityTypeConfiguration<Trip>
    {
        public TripConfiguration()
        {
            HasKey(t => t.Identifier);
            Property(t => t.RowVVersion).IsRowVersion();
            Property(t => t.Identifier).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
        }
    }
    public class Person
    {
        public Person()
        {
            Address = new Address();
            Info = new PersonnalInfo
            {
                Weight = new Measurement(),
                Height = new Measurement()
            };
        }
        public int PersonId { get; set; }
        public int SocialSecurityNumber { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public byte[] RowVersion { get; set; }
        public Address Address { get; set; }
        public PersonnalInfo Info { get; set; }
    }
    public class PersonConfiguration : EntityTypeConfiguration<Person>
    {
        public PersonConfiguration()
        {
            HasKey(p =>p.PersonId);
            Property(p => p.SocialSecurityNumber).IsConcurrencyToken();
            Property(p => p.RowVersion).IsRowVersion();
            Property(p =>p.SocialSecurityNumber).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
        }
    }
    public class Address
    {
        public int AddressID { get; set; }
        public string StreetAddress { get; set; }
        public string City { get; set; }
        public string State { get; set; }
        public string ZipCode { get; set; }
    }
    public class AddressConfiguration : ComplexTypeConfiguration<Address>
    {
        public AddressConfiguration()
        {
            Property(a => a.StreetAddress).HasMaxLength(150);
        }
    }
    public class PersonnalInfo
    {
        public Measurement Weight{get;set;}
        public Measurement Height{get;set;}
        public string DierRestrict { get; set;}
    }
    public class Measurement
    {
        public decimal Reading { get; set; }
        public string Units { get; set; }
    }
    public class BlueSeaContext : DbContext
    {
        public DbSet<Destination> Destinations { get; set; }
        public DbSet<Lodging> Lodgings { get; set; }
        public DbSet<Trip> Trips { get; set; }
        public DbSet<Person> People { get; set; }
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Configurations.Add(new DestinationConfiguration());
            modelBuilder.Configurations.Add(new LodgingConfiguration());
            modelBuilder.Configurations.Add(new TripConfiguration());
            modelBuilder.Configurations.Add(new PersonConfiguration());
            modelBuilder.Configurations.Add(new AddressConfiguration());
            modelBuilder.ComplexType<PersonnalInfo>();
            base.OnModelCreating(modelBuilder);
        }
    }
}


 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值