1、引用EF对应的程序集

使用命令安装EntityFramework包
Install-Package EntityFramework

Entity Framework简单目录:

1.context数据库上下文class:

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Web; namespace ClothMvcApp.EF
{
public class ClothDBContext: DbContext, IDisposable
{
public ClothDBContext()
: base("name=ClothDBContext")
{
} protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
} public DbSet<News> News { get; set; }
public DbSet<Product> Product { get; set; }
public DbSet<SysUser> SysUser { get; set; } public DbSet<Brand> Brand { get; set; } public DbSet<ImageInfo> ImageInfo { get; set; } public DbSet<Contact> Contact { get; set; }
}
}

2.Model实体类:

添加所需程序集:

Install-Package System.ComponentModel.Annotations

如下图:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Web; namespace ClothMvcApp.EF
{
[Table("Brand")]
public class Brand
{
[Column("Id")]
public Guid Id { get; set; } [Column("Content")]
public string Content { get; set; } [Column("Picture")]
public string Picture { get; set; } [Column("CreateTime")]
public DateTime CreateTime { get; set; }
}
}

有外键字段Model:

using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema; namespace Lemon.Media.Entities
{
/// <summary>
/// 渠道应用
/// </summary>
[Table("Channel_Apps")]
public class ChannelApp
{
[Key, Column("Id")]
public Guid Id { get; set; } /// <summary>
/// 渠道ID(主要指物业)
/// </summary>
[Column("ChannelId")]
public int ChannelId { get; set; } /// <summary>
/// 是否由H5承载实现
/// </summary>
[Column("IsH5")]
public bool IsH5 { get; set; } /// <summary>
/// App应用唯一标识
/// </summary>
[Column("AppKey")]
public string AppKey { get; set; } /// <summary>
/// 添加时间
/// </summary>
[Column("AddTime")]
public DateTime AddTime { get; set; } /// <summary>
/// 是否删除
/// </summary>
[Column("IsDel")]
public bool IsDel { get; set; } /// <summary>
/// 是否需要PPTV
/// </summary>
[Column("HasPPTV")]
public bool HasPPTV { get; set; } /// <summary>
/// 渠道
/// </summary>
[ForeignKey("ChannelId")]
public virtual Channel Channel { get; set; }
}
}

3.web.config 数据库连接字符串:

<connectionStrings>

    <add name="ClothDBContext" connectionString="Data Source=.;Initial Catalog=ClothDB;User ID=sa;Password=123456;Pooling=true;" providerName="System.Data.SqlClient" />
</connectionStrings>

4.简单的调用方式:

using (var context = new ClothDBEntities())
{
context.ImageInfo.Where(c => c.Cate == "banner").OrderByDescending(c => c.CreateTime).Take().ToList();
}

ps:卸载nuget包:

Uninstall-Package System.ComponentModel.Annotations

手动写Entity Framework 数据库上下文和Model实体的更多相关文章

  1. Entity Framework 数据库初始化四种策略

    策略一:数据库不存在时重新创建数据库 Database.SetInitializer<testContext>(new CreateDatabaseIfNotExists<testC ...

  2. Entity Framework数据库初始化四种策略

    策略一:数据库不存在时重新创建数据库 程序代码 Database.SetInitializer<testContext>(new CreateDatabaseIfNotExists< ...

  3. [Programming Entity Framework] 第3章 查询实体数据模型(EDM)(一)

    http://www.cnblogs.com/sansi/archive/2012/10/18/2729337.html Programming Entity Framework 第二版翻译索引 你可 ...

  4. Entity Framework 数据库先行、模型先行、代码先行

    数据库先行(Database First):基于已存在的数据库,利用某些工具(如Vs提供的EF设计器)创建实体类,数据库对象与实体类的匹配关系等,你也可以手动修改这些自动生成的代码及匹配文件. 模型先 ...

  5. Entity Framework入门教程:创建实体数据模型

    下图为一个已经创建好的数据库表关系 实体数据模型的创建过程 在Visual Studio项目中,右键程序集菜单,选择[添加]->[新建项],在[添加新项窗口]中选择[ADO.NET实体数据模型] ...

  6. Visual Studio2017中如何让Entity Framework工具【ADO.NET实体数据模型】支持MYSQL数据源

    熟悉Entity Framework应该对以下图片不陌生,他就是ADO.NET实体数据模型向导:可以将数据库的表自动生成模型类,或者创建Code First的模型文件. 但是这个模型向导默认只显示微软 ...

  7. Entity framework 加载多层相关实体数据

    Entity framework有3种加载数据的方式:懒汉式(Lazy loading),饿汉式(Eager loading),显示加载(Explicit loading).3种加载方式有各自的优缺点 ...

  8. Mysql 该如何 Entity Framework 数据库迁移 和 如何更好的支持EntityFramework.Extended

    问题 1.在使用EntityFramework访问Mysql的时候,使用迁移来生成数据库或者更新数据库时候会遇到一些问题 2.EntityFramework.Extended对Mysql的支持不是很完 ...

  9. Entity FrameWork Code First 之Model分离

    之前一直用DB First新建类库进行使用,最近开始研究Code First.Code First也可以将Model新建在类库里面,然后通过数据迁移等操作生成数据库. 现在说下主要步骤: 1.新建类库 ...

随机推荐

  1. iptables防火墙详解(三)

    linux 高级路由 策略路由(mangle表) lartc(linux advanced routing and traffic control) http://www.lartc.org # rp ...

  2. 借网站日记分析~普及一下Pandas基础

      对网站日记分析其实比较常见,今天模拟演示一下一些应用场景,也顺便说说Pandas,图示部分也简单分析了下 1.数据清洗¶ 一般数据都不可能直接拿来用的,或多或少都得清理一下,我这边就模拟一下清洗完 ...

  3. A1057. Stack

    Stack is one of the most fundamental data structures, which is based on the principle of Last In Fir ...

  4. A1064. Complete Binary Search Tree

    A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...

  5. io系列之字节流

    java中io流系统庞大,知识点众多,作为小白通过五天的视频书籍学习后,总结了io系列的随笔,以便将来复习查看. 本篇为此系列随笔的第一篇:io系列之字节流. 一.字节流的File读写操作. Inpu ...

  6. 【内核】Linux内核Initrd机制解析,内核更新步骤,grub配置说明

    什么是Initrd initrd的英文含义是 boot loader initialized RAM disk,就是由boot loader初始化的内存盘.在 linux内核启动前, boot loa ...

  7. 【清北学堂2018-刷题冲刺】Contest 2

     这场比赛的T1相当智熄.由于至今无法理解题意,我只能解出前20分.诸位dalao谁能比较好地理解题意(独立性)的,请联系我,不胜感激.  在此本蒟蒻只能贴上题面: Task 1:选举 [问题描述] ...

  8. Yii2的mongodb的聚合操作

    最近项目使用到mongodb的聚合操作,但是yii文档中对这方面资料较少,记录下 $where['created_time'] = ['$gt' => "$start_date_str ...

  9. aspcms逻辑错误导致后台地址泄露

    访问即可跳转后台地址: URL:http://www.xxx.org.cn/plug/oem/AspCms_OEMFun.asp 注入:plug/comment/commentList.asp?id= ...

  10. mysql执行update报错 Err] 1055 - 'information_schema.PROFILING.SEQ' isn't in GROUP BY

    mysql执行update报错 Err] 1055 - 'information_schema.PROFILING.SEQ' isn't in GROUP BY 今天开发的同事发来如下错误信息,最最简 ...