ORM(1)
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="data source=HEIDI-PC\LJJ;initial catalog=TestMvc;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" /> </connectionStrings> <entityFramework> <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework"> <parameters> <parameter value="v11.0" /> </parameters> </defaultConnectionFactory> </entityFramework> </configuration>
通过下图创建edmx