zoukankan      html  css  js  c++  java
  • Entity Framework Code Firest 连接Sqlserver数据库(增删改查)

    1、添加Nuget包:EntityFramework

      

     2、在App.config文件添加链接Sqlserver字符串

      <connectionStrings>
        <add name="MyStrConn" connectionString="server=Localhost;uid=sa;pwd=123456;database=WareHouseDemo;" providerName="System.Data.SqlClient"/>
      </connectionStrings>

    3、添加模型类

      新建LocationInfo类并写入下面代码

    [Table("LocationInfo")]
        class LocationInfo
        {
            [Key]
            public int LocationId { get; set; }//库位号
            public string Type { get; set; }//种类
            public int Number { get; set; }//数量
            public string Material { get; set; }//材质
            public string RFIDNumber { get; set; }//料盘的RFID号
    
            //定义无参数的构造函数主要是因为在通过DbSet获取对象进行linq查询时会报错
            //The class 'EFCodeFirstModels.Student' has no parameterless constructor.
            public LocationInfo() { }
    
            public LocationInfo(int locationId, string type, int number, string material, string rfidNumber)
            {
                this.LocationId = locationId;
                this.Type = type;
                this.Number = number;
                this.Material = material;
                this.RFIDNumber = rfidNumber;
            }
    
        }

    4、添加数据库上下文

      新建EFCodeFirstDbContext类并写入下面代码

    class EFCodeFirstDbContext : DbContext
        {
            public EFCodeFirstDbContext() : base("name=MyStrConn")
            {
            }
            protected override void OnModelCreating(DbModelBuilder modelBuilder)
            {
    
            }
            public DbSet<LocationInfo> LocationInfos { get; set; }
        }

    5、进行增删改查操作

      在Program类中写入下面代码

        class Program
        {
            static void Main(string[] args)
            {
                //using能及时释放资源,例如数据库连接异常,可以即使将上下文释放
                using (var db = new EFCodeFirstDbContext())
                {
    
                    //增------------------------------------------------------------------------------
                    LocationInfo locationInfo1 = new LocationInfo(1, "empty", 0, "empty", "00010000");
                    LocationInfo locationInfo2 = new LocationInfo(2, "empty", 0, "empty", "00020000");
                    LocationInfo locationInfo3 = new LocationInfo(3, "empty", 0, "empty", "00030000");
                    LocationInfo locationInfo4 = new LocationInfo(4, "empty", 0, "empty", "00040000");
                    LocationInfo locationInfo5 = new LocationInfo(5, "empty", 0, "empty", "00050000");
                    LocationInfo locationInfo6 = new LocationInfo(6, "empty", 0, "empty", "00060000");
                    LocationInfo locationInfo7 = new LocationInfo(7, "empty", 0, "empty", "00070000");
                    LocationInfo locationInfo8 = new LocationInfo(8, "empty", 0, "empty", "00080000");
                    LocationInfo locationInfo9 = new LocationInfo(9, "empty", 0, "empty", "00090000");
                    LocationInfo locationInfo10 = new LocationInfo(10, "empty", 0, "empty", "000A0000");
                    db.LocationInfos.Add(locationInfo1);
                    db.LocationInfos.Add(locationInfo2);
                    db.LocationInfos.Add(locationInfo3);
                    db.LocationInfos.Add(locationInfo4);
                    db.LocationInfos.Add(locationInfo5);
                    db.LocationInfos.Add(locationInfo6);
                    db.LocationInfos.Add(locationInfo7);
                    db.LocationInfos.Add(locationInfo8);
                    db.LocationInfos.Add(locationInfo9);
                    db.LocationInfos.Add(locationInfo10);
                    db.SaveChanges();
    
                    //查------------------------------------------------------------------------------
                    //StringBuilder stringBuilder = new StringBuilder();
                    //foreach (var item in db.LocationInfos)
                    //{
                    //    stringBuilder.AppendFormat(
                    //        "库位号:{0}	物料类型:{1}	物料数量:{2}	物料材质:{3}	RFID号:{4}
    ",
                    //        item.LocationId, item.Type, item.Number, item.Material, item.RFIDNumber);
                    //}
                    //Console.WriteLine(stringBuilder);
    
                    //查某条数据(1)
                    /*
                    var query = from q in db.LocationInfos
                                where q.LocationId == 5
                                select q;
                    StringBuilder stringBuilder = new StringBuilder();
                    foreach (var item in query)
                    {
                        stringBuilder.AppendFormat(
                            "库位号:{0}	物料类型:{1}	物料数量:{2}	物料材质:{3}	RFID号:{4}
    ",
                            item.LocationId, item.Type, item.Number, item.Material, item.RFIDNumber);
                    }
                    Console.WriteLine(stringBuilder);
                    */
    
                    //查某条数据(2)
                    /*
                    var query = db.LocationInfos.Where<LocationInfo>(c => c.LocationId == 5);
                    StringBuilder stringBuilder = new StringBuilder();
                    foreach (var item in query)
                    {
                        stringBuilder.AppendFormat(
                            "库位号:{0}	物料类型:{1}	物料数量:{2}	物料材质:{3}	RFID号:{4}
    ",
                            item.LocationId, item.Type, item.Number, item.Material, item.RFIDNumber);
                    }
                    Console.WriteLine(stringBuilder);
                    */
    
                    //改------------------------------------------------------------------------------
                    /*
                    LocationInfo locationInfo = new LocationInfo() { LocationId = 5 };
                    locationInfo = db.LocationInfos.Attach(locationInfo);
                    locationInfo.Type = "bottle";
                    locationInfo.Number = 4;
                    locationInfo.Material = "Sn";
                    locationInfo.RFIDNumber = "00050101";
                    db.SaveChanges();
                    */
    
                    //删------------------------------------------------------------------------------
                    /*
                    LocationInfo locationInfo = db.LocationInfos.Where(p => p.LocationId == 9).FirstOrDefault();
                    db.LocationInfos.Remove(locationInfo);
                    db.SaveChanges();
                    */
                }
                Console.ReadKey();
            }
        }

      源代码:

      链接:https://pan.baidu.com/s/1zj9lvwgVxmgh2mbskCZdZA
      提取码:kszh
      后续会陆续更新其他资料,喜欢请关注哦!

  • 相关阅读:
    C++ 概念易错点
    C++的位操作符备忘
    C++关键词
    在ubuntu下安装drupal6
    C++符号优先级一览
    开启drupal的clear urls
    VC6.0使用PlaySound函数报错
    小记一下以非string为结束条件的循环
    C++中查看数据类型的方法
    在ubuntu下安装和配置drupal
  • 原文地址:https://www.cnblogs.com/duhaoran/p/12886888.html
Copyright © 2011-2022 走看看