• 手动写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

  • 相关阅读:
    HDU 4725 The Shortest Path in Nya Graph(优先队列+dijkstra)
    POJ 3216 Repairing Company(二分图匹配)
    POJ 3414 Pots(bfs打印路径)
    POJ 3278 Catch That Cow(bfs)
    poj 3009 curling2.0 (dfs)
    用“道”的思想解决费用流问题---取/不取皆是取 (有下界->有上界) / ACdreamoj 1171
    hdu2448 / 费用流 / harbin赛区c题
    大数类模板(+-*/%等等)
    hdu4619 / 最大独立集
    hdu4888 多校B 最大流以及最大流唯一判断+输出方案
  • 原文地址:https://www.cnblogs.com/zoro-zero/p/5680700.html
Copyright © 2020-2023  润新知