WPF EF First

namespace Micro.IMSSDK.Entity
{
    /// <summary>
    /// 指定表名
    /// </summary>
    [Table("UserInfors")]
    public class UserInfor
    {
        /// <summary>
        /// [主键(每个表必须要有主键),表示是自增列]
        /// </summary>
        [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int id { get; set; }
        /// <summary>
        /// [指定对应的列名,限定列的字符长度]
        /// </summary>
        [Column("Name"), StringLength(10)]
        public string sex { get; set; }
        /// <summary>
        /// 可空类型指定
        /// </summary>
        public byte? Age { get; set; }
    }
}

  

1.增 

using (var bridgeContext = new BridgeContext())
{
    //添加一个对象
    T_EF t_EF = bridgeContext.T_EF.Add(new T_EF() { Name = "RB" });
    //将修改后的保存到数据库
    bridgeContext.SaveChanges();
}

2.删

//删除数据也需先查出后删除再保存
DbSqlQuery<T_EF> dbSqlQuery = bridgeContext.T_EF.SqlQuery("select * from [T_EF] where [ID] = {0}", 2);
bridgeContext.T_EF.RemoveRange(dbSqlQuery);
bridgeContext.SaveChanges();

3.改 

//修改数据,需先查出实体,再修改保存
T_EF t_EF1 = bridgeContext.T_EF.Find(1);
t_EF1.Location ="GD";
bridgeContext.SaveChanges();

4.查

//根据主键查询
T_EF t_EF1 = bridgeContext.T_EF.Find(1);
//根据TSQL查询
DbSqlQuery<T_EF> dbSqlQuery = bridgeContext.T_EF.SqlQuery("select * from [T_EF] where [ID] = {0}", 2);

  

posted @ 2021-07-26 09:59  microsoft-zhcn  阅读(72)  评论(0编辑  收藏  举报