c#EntityFrameworkcodeFirst模式

 

一、首先定义数据类

[DataContract(Namespace="http://www.cninnovation.com/Services/2012")]
  public class RoomReservation : INotifyPropertyChanged
  {
    private int id;
    [DataMember]
    public int Id
    {
      get { return id; }
      set { SetProperty(ref id, value); }
    }

    private string roomName;
    [DataMember]
    [StringLength(30)]
    public string RoomName
    {
      get { return roomName; }
      set { SetProperty(ref roomName, value); }
    }

    private DateTime startTime;
    [DataMember]
    public DateTime StartTime
    {
      get { return startTime; }
      set { SetProperty(ref startTime, value); }
    }

    private DateTime endTime;
    [DataMember]
    public DateTime EndTime
    {
      get { return endTime; }
      set { SetProperty(ref endTime, value); }
    }

    private string contact;
    [DataMember]
    [StringLength(30)]
    public string Contact
    {
      get { return contact; }
      set { SetProperty(ref contact, value); }
    }

    private string text;
    [DataMember]
    [StringLength(50)]
    public string Text
    {
      get { return text; }
      set { SetProperty(ref text, value); }
    }
   

    protected virtual void OnNotifyPropertyChanged(string propertyName)
    {
      PropertyChangedEventHandler eventHandler = PropertyChanged;
      if (eventHandler != null)
      {
        eventHandler(this, new PropertyChangedEventArgs(propertyName));
      }
    }

    protected virtual void SetProperty<T>(ref T item, T value, [CallerMemberName] string propertyName = null)
    {
      if (!EqualityComparer<T>.Default.Equals(item, value))
      {
        item = value;
        OnNotifyPropertyChanged(propertyName);
      }
    }

    public event PropertyChangedEventHandler PropertyChanged;
  }

二、定义继承DbContext类

  public class RoomReservationContext : DbContext
  {

    public RoomReservationContext()
      : base("name=RoomReservation")
    {

    }
    public DbSet<RoomReservation> RoomReservations { get; set; }
  }

三、编译项目

编译后会生成对应dbf数据库文件

怎么通过visualstudio打开呢?

1.

2.天假连接数据库

3.添加成功后

数据库物理位置C:\Users\Administrator\RoomReservation.mdf

 

posted @ 2018-01-23 15:14  beautifulday  阅读(1112)  评论(0编辑  收藏  举报