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

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

 ps:卸载nuget包:

Uninstall-Package System.ComponentModel.Annotations

posted @   大空白纸  阅读(897)  评论(0编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
点击右上角即可分享
微信分享提示