手动写Entity Framework 数据库上下文和Model实体

1、引用EF对应的程序集

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

手动写Entity Framework 数据库上下文和Model实体

Entity Framework简单目录:

手动写Entity Framework 数据库上下文和Model实体

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

如下图:

手动写Entity Framework 数据库上下文和Model实体

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(3).ToList();
            }

 ps:卸载nuget包:

Uninstall-Package System.ComponentModel.Annotations