ASP.NET关于书籍详情和删除的Demo(HttpHandler进行页面静态化[自动生成html网页]+Entity Framework通过类创建数据库+EF删查)

这次的Demo如标题所示,

首先第一步EF创建数据库

创建两个类,一个是图书类,一个是图书类别的类

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;

namespace WebApplication2.DAL
{
   
    public class Book
    {
   
        [Key]
        public int BookId {
    get; set; }
        public string BookName {
    get; set; }
        public string BookAuthor {
    get; set; }
        public virtual BookType TypeId {
    get; set; }
        public decimal Price {
    get; set; }
        public DateTime Addtime {
    get; set; }
        public string Img {
    get; set; }
    }
}
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;

namespace WebApplication2.DAL
{
   
    public class BookType
    {
   
        [Key]
        public int TypeId {
    get; set; }
        public string TyoeName {
    get; set; } 
    }
}

添加一个ef创建数据库

在这里插入图片描述

在这里插入图片描述

这是EF自动生成的那个类,下面画横线的是需要自己写的,第一行是一个添加数据的类,稍后会在下面附上
在这里插入图片描述

namespace WebApplication2.DAL
{
   
    using System;
    using System.Data.Entity;
    using System.Linq;

    public class Model1 : DbContext
    {
   
        //您的上下文已配置为从您的应用程序的配置文件(App.config 或 Web.config)
        //使用“Model1”连接字符串。默认情况下,此连接字符串针对您的 LocalDb 实例上的
        //“WebApplication2.DAL.Model1”数据库。
        // 
        //如果您想要针对其他数据库和/或数据库提供程序,请在应用程序配置文件中修改“Model1”
        //连接字符串。
        public Model1()
            : base("name=Model1")
        {
   
            Database.SetInitializer(new InitDataBase());
        }
        public virtual DbSet<Book> Books {
    get; set; }

        public virtual DbSet<BookType> BookTypes {
    get; set; }

        //为您要在模型中包含的每种实体类型都添加 DbSet。有关配置和使用 Code First  模型
        //的详细信息,请参阅 http://go.microsoft.com/fwlink/?LinkId=390109。

        // public virtual DbSet<MyEntity> MyEntities { get; set; }
    }

    //public class MyEntity
    //{
   
    //    public int Id { get; set; }
    //    public string Name { get; set; }
    //}
}

自动加载数据的类

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;

namespace WebApplication2.DAL
{
   
    public class InitDataBase : DropCreateDatabaseIfModelChanges<Model1>
    {
    
        protected override void Seed(Model1 context)
        {
   
            BookType bookType1 = new BookType
            {
   
                TyoeName = "武侠"
            };
            BookType bookType4 = new BookType
            {
   
                TyoeName = "科幻"
            }
  • 24
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 12
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 12
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值