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. shell中定义变量用双引号和单引号以及不用引号的区别

    1. 单引号 使用单引号的情况下,不管里面的是否有变量或者其他的表达是都是原样子输出 2. 双引号 如果其定义变量的时候使用双引号的话,则里面的变量或者函数会通过解析,解析完成后再输出内容,而不是把双 ...

  2. Codeforces1076D. Edge Deletion(最短路树+bfs)

    题目链接:http://codeforces.com/contest/1076/problem/D 题目大意: 一个图N个点M条双向边.设各点到点1的距离为di,保证满足条件删除M-K条边之后使得到点 ...

  3. CF 1013E Hills

    这是一道DP题...我居然有那么半个小时思考非DP解决方案,实在是太弱了. 题意:给您若干山,您可以花费1代价削去1高度,求有k个山峰时的最小代价. 输出k = 1 ~ (n + 1) >> ...

  4. Django 路由报错友好提示

    这个方法要在设置路由文件内使用也就是urls.py内. """mysite URL Configuration The `urlpatterns` list routes ...

  5. my97DatePicker选择年、季度、月、周、日(转)

    My97DatePicker是一款非常灵活好用的日期控件.使用非常简单. 下面总结下使用该日历控件选择年.季度.月.周.日的方法. 1.选择年 <input id="d1212&quo ...

  6. Windows下MySQL下载安装、配置与使用

    用过MySQL之后,不论容量的话,发现比其他两个(sql server .oracle)好用的多,一下子就喜欢上了.下面给那些还不知道怎么弄的童鞋们写下具体的方法步骤. (我这个写得有点太详细了,甚至 ...

  7. linux下的crontab安装及简单使用

    1.安装 # yum install vixie-cron # yum install crontabs # chkconfig crond on  #设为开机启动,先要安装chkconfig(yum ...

  8. Vue之vue-cli安装与简单调试

    一.安装nodejs https://nodejs.org/en/download/ nodejs简单使用 node -v 查看版本 npm -v 查看对应npm版本 如果npm版本太低小于 4.0 ...

  9. As 400错

    8:25 Gradle sync started 8:25 Gradle sync failed: Unable to tunnel through proxy. Proxy returns &quo ...

  10. data_type

    import logging logger = logging.getLogger("simple_example") logger.setLevel(logging.DEBUG) ...