欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

.net mvc5(二)

程序员文章站 2023-12-29 09:49:04
...

上次创建了基本的框架,现在开始进行Model层的编写

一、新建User类

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

namespace MyDiary.Models
{
    
    public class User
    {
        [DisplayName("Id")]
        public int Id { get; set; }

        [DisplayName("UserName"),Required]
        public string UserName { get; set; }

        [DisplayName("PassWord"),Required,DataType(DataType.Password)]
        public string PassWord { get; set; }

        [DisplayName("Diaries")]
        public virtual List<Diary> Diaries { get; set; }
    }
}

(1)public int Id { get; set; }

(2)属性的展现 [DisplayName("Id")]

(3)对属性的验证是否为空,关键字Required

[DisplayName("UserName"),Required]

(3)属性是密码类型的 DataType(DataType.Password)

(4)一个用户可以拥有多个日记,所以用一个List来标识该属性

 public virtual List<Diary> Diaries { get; set; }

二、同样新建Diary类

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

namespace MyDiary.Models
{
    public class Diary
    {
        [DisplayName("Id")]
        public int Id { get; set; }

        [DisplayName("Title"), Required]
        public string Title { get; set; }

        [DisplayName("Content"), Required]
        public string Content { get; set; }

        [DisplayName("PubDate"), DataType(DataType.Date)]
        public DateTime? PubDate { get; set; }

        [DisplayName("UserId")]
        public int UserId { get; set; }

        [DisplayName("User")]
        public virtual User User { get; set; }

    }
}
(1)一篇日记属于某一个用户的,关联用户Id

public int UserId { get; set; }



三、新建DiaryDB类来关联数据库的增,删,改,查



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

namespace MyDiary.Models
{
    public class DiaryDB:DbContext
    {
        public DbSet<Diary> Diaries { get; set; }

        public DbSet<User> Users { get; set; }
    }
}

(1)DbContext主要是与数据库进行会话
(2)DbSet:提供对数据实体的操作,如增、改、查,继承了DBQuery的一些功能

四、创建数据库,新建
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;

namespace MyDiary.Models
{
    public class SampleData:DropCreateDatabaseAlways<DiaryDB>
    {
        protected override void Seed(DiaryDB context)
        {

            context.Users.Add(new User { 
            UserName ="lili",
            PassWord = "lili",
            Diaries = new List<Diary>
            {
                new Diary{Title="Title01", Content="Conten1",PubDate=System.DateTime.Now },
                new Diary{Title="Title02", Content="Conten2",PubDate=System.DateTime.Now },
                new Diary{Title="Title03", Content="Conten3",PubDate=System.DateTime.Now }
            }
            });

            context.Users.Add(new User
            {
                UserName = "Kevin",
                PassWord = "Kevin",
                Diaries = new List<Diary>
            {
                new Diary{Title="Title04", Content="Conten4",PubDate=System.DateTime.Now },
                new Diary{Title="Title05", Content="Conten5",PubDate=System.DateTime.Now },
                new Diary{Title="Title06", Content="Conten6",PubDate=System.DateTime.Now }
            }
            });
            base.Seed(context);
        }
    }
}

(1)DropCreateDatabaseAlways:每次都删除数据库以及创建数据库
 (2) Seed方法里面加入表User和Diary的测试数据

五、数据库字符串链接,修改web.config文件
.net mvc5(二)
字符串链接的

  <connectionStrings>
    <add name="MyDiary.Models.DiaryDB" providerName="System.Data.Sqlclient" connectionString="Data Source=.;Initial Catalog=DiaryDB;User ID=sa;Password=123456" />
  </connectionStrings>

六、修改Global文件,开始启动的时候就初始化数据库
 protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            WebApiConfig.Register(GlobalConfiguration.Configuration);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);

            Database.SetInitializer(new SampleData());
        }


总上所述,就完成了Model层的数据库的创建以及相关的链接了


上一篇:

下一篇: