首页 > 其他 > 详细

ORM(1)

时间:2019-08-31 14:40:17      阅读:53      评论:0      收藏:0      [点我收藏+]
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StudeyORM
{
    class Program
    {
        public static string conStr = "Data Source=HEIDI-PC\\LJJ;Initial Catalog=TestMvc;Integrated Security=True";
        static void Main(string[] args)
        {
            TestMvcEntities dbContext = new TestMvcEntities();
           // Add(dbContext);
            Query(dbContext);
            Console.ReadLine();
        }
        private static void Add(TestMvcEntities dbContext)
        {
            UserDetail userDetail = new UserDetail() {UserName="ljj",Sex="",BirthDay=Convert.ToDateTime("2013-1-1"),Emalil="ljj@163.com",Addr1="苏州市",Addr2="上海市",ComIntNo=1 };
         
            DbEntityEntry<UserDetail> entry = dbContext.Entry<UserDetail>(userDetail);
            entry.State = EntityState.Added;
            dbContext.SaveChanges();
        }

        private static void Query(TestMvcEntities dbContext)
        {
            //用标准查询语句,查出Id为1的所有的学生

            //用EF查询方法,实际创建的是 某个实体类的代理类,代理类 继承于 该实体类
            UserDetail stu = dbContext.UserDetails.Where(s =>s.UserID == 2).Select(s => s).FirstOrDefault();
            Console.WriteLine(stu.UserName + "" + stu.Sex + "");
        }
    }
}

配置文件:

<?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=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
  <connectionStrings>
    <add name="TestMvcEntities" connectionString="metadata=res://*/StudentModel.csdl|res://*/StudentModel.ssdl|res://*/StudentModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=HEIDI-PC\LJJ;initial catalog=TestMvc;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
  </connectionStrings>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="v11.0" />
      </parameters>
    </defaultConnectionFactory>
  </entityFramework>
</configuration>

通过下图创建edmx

技术分享图片

ORM(1)

原文:https://www.cnblogs.com/sxjljj/p/11438585.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!