c# 简单实验 ef 控制台

1.概要

EF ,全称Entity FramWork。就是微软以ADO.NET为基础发展的所谓ORM(对象关系映射框架,或者说是数据持久化框架)。

这个技术,在很多工程中都使用过了,但是一直都没有搭建过,只要对orm模型有理解,基本上拿过来就用就可以,单是总感觉用的不那么爽,因为没有自己搭建过最基本的框架,总感觉理解的不够透彻。还有一点搭建一个简单的框架,以后有什么自己的想法不确定ef 是否支持,可以在这个简单的框架下做实验。

本来准备过一个winform版本的,单总感觉winform还是没有控制台说明的问题方式更精简,这中感觉用语言可能描述的不太准,总之就是更想用控制台说明问题,这样才舒服。于是准备了这个

2.代码

2.1代码

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

namespace EF简单实践
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("EF简单实践");
            using (DBContentMy db = new DBContentMy())
            {
                Tb1 tb1 = new Tb1();
                tb1.v1 = 1;
                db.Tb1s.Add(tb1);
                db.SaveChanges();
            }
            using (DBContentMy db = new DBContentMy())
            {
                db.Database.ExecuteSqlCommand("insert into tb1(v1) values(2)");
            }
            using (DBContentMy db = new DBContentMy())
            {
                var c = db.Database.SqlQuery<int>("select v1 from tb1");
                foreach (int a in c)
                {
                    Console.WriteLine(a);
                }
            }
            Console.ReadKey();
        }

        [Table("Tb1")]
        class Tb1
        {
            public int id { set; get; }
            public int v1 { set; get; }
        }
        [DbConfigurationType(typeof(MySqlEFConfiguration))]
        class DBContentMy : DbContext
        {
            public DBContentMy() : base("DbConnStr")
            {
                Database.SetInitializer<DBContentMy>(null);
            }
            protected override void OnModelCreating(DbModelBuilder modelBuilder)
            {
                base.OnModelCreating(modelBuilder);
            }


            public DbSet<Tb1> Tb1s { set; get; }
        }
    }
}

2.2配置文件

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.1" />
  </startup>
  <connectionStrings>
    <add name="DbConnStr" connectionString="Data Source=localhost;port=3306;Initial Catalog=mesdb_develop924;user id=root;password=123456;" providerName="MySql.Data.MySqlClient" />
  </connectionStrings>
  <entityFramework>
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    <provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.EntityFramework, Version=8.0.26.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d">
      </provider></providers>
  </entityFramework>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Buffers" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-4.0.3.0" newVersion="4.0.3.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="MySql.Data" publicKeyToken="c5687fc88969c44d" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-8.0.26.0" newVersion="8.0.26.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

3.运行结果

4.附加

4.1 数据库的创建脚本

CREATE TABLE `tb1` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `v1` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
)

4.2 mysql插件的下载

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值