从模型类生成数据库

时间:2013-03-07 12:59:01

标签: asp.net webforms ef-code-first

我正在asp.net中用c#作为语言和ms sql server express进行项目。到目前为止我有四个模型类:

首先:

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

namespace WerIstWo.Models
{
    public class Benutzer
    {
        //Eigenschaften
        private int benutzerid, gruppenid, urlaubstageinsgesamt, urlaubstagegenommen, urlaubstagerest;
        private string titel, bezeichnung, vorname, nachname, geburtsdatum, geburtsort,
            nationalitaet, strasse, hausnummer, plz, ort, land, mobil, fax, festnetz, email,
            homepage, benutzerart, status, benutzername, passwort;      
        List<Gruppe> gruppen;

        //Properties
        public int BenutzerID
        {
            get { return this.benutzerid; }
            set { this.benutzerid = value; }
        }
        public int GruppenID
        {
            get { return this.gruppenid; }
            set { this.gruppenid = value; }
        }
        public int UrlaubstageInsgesamt
        {
            get { return this.urlaubstageinsgesamt; }
            set { this.urlaubstageinsgesamt = value; } 
        }
        public int UrlaubstageGenommen 
        {
            get { return this.urlaubstagegenommen; }
            set { this.urlaubstagegenommen = value; } 
        }
        public int UrlaubstageRest 
        {
            get { return this.urlaubstagerest; }
            set { this.urlaubstagerest = value; } 
        }
        public string Titel 
        {
            get { return this.titel; }
            set { this.titel = value; } 
        }
        public string Bezeichnung 
        {
            get { return this.bezeichnung; }
            set { this.bezeichnung = value; } 
        }
        public string Vorname 
        {
            get { return this.vorname; }
            set { this.vorname = value; } 
        }
        public string Nachname 
        {
            get { return this.nachname; }
            set { this.nachname = value; } 
        }
        public string Geburtsdatum 
        {
            get { return this.geburtsdatum; }
            set { this.geburtsdatum = value; } 
        }
        public string Geburtsort 
        {
            get { return this.geburtsort; }
            set { this.geburtsort = value; } 
        }
        public string Nationalitaet 
        {
            get { return this.nationalitaet; }
            set { this.nationalitaet = value; } 
        }
        public string Strasse 
        {
            get { return this.strasse; }
            set { this.strasse = value; } 
        }
        public string Hausnummer 
        {
            get { return this.hausnummer; }
            set { this.hausnummer = value; } 
        }
        public string PLZ 
        {
            get { return this.plz; }
            set { this.plz = value; } 
        }
        public string Ort 
        {
            get { return this.ort; }
            set { this.ort = value; } 
        }
        public string Land 
        {
            get { return this.land; }
            set { this.land = value; } 
        }
        public string Mobil 
        {
            get { return this.mobil; }
            set { this.mobil = value; } 
        }
        public string Fax 
        {
            get { return this.fax; }
            set { this.fax = value; } 
        }
        public string Festnetz 
        {
            get { return this.festnetz; }
            set { this.festnetz = value; } 
        }
        public string Email 
        {
            get { return this.email; }
            set { this.email = value; } 
        }
        public string HomePage 
        {
            get { return this.homepage; }
            set { this.homepage = value; } 
        }
        public string Benutzerart 
        {
            get { return this.benutzerart; }
            set { this.benutzerart = value; } 
        }
        public string Status 
        {
            get { return this.status; }
            set { this.status = value; } 
        }
        public string Benutzername 
        {
            get { return this.benutzername; }
            set { this.benutzername = value; } 
        }
        public string Passwort 
        {
            get { return this.passwort; }
            set { this.passwort = value; } 
        }

        //Konstruktor
        public Benutzer()
        { 

        }
    }
}

第二

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

namespace WerIstWo.Models
{
    public class Gruppe
    {
        //Eigenschaften
        private int gruppenid;
        private string bezeichnung;
        private int anzahlmitglieder;

        //Properties
        public int GruppenID 
        {
            get { return this.gruppenid; }
            set { this.gruppenid = value; } 
        }
        public string Bezeichnung
        {
            get { return this.bezeichnung; }
            set { this.bezeichnung = value; } 
        }
        public int AnzahlMitglieder 
        {
            get { return this.anzahlmitglieder; }
            set { this.anzahlmitglieder = value; } 
        }

        //Konstruktor
        public Gruppe()
        {

        }
    }
}

第三

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

namespace WerIstWo.Models
{
    public class Kalender
    {
        //Eigenschaften
        private int kalenderid;
        private int gruppenid;
        private int benutzerid;
        private string art;

        //Properties
        public int KalenderID 
        {
            get { return this.kalenderid; }
            set { this.kalenderid = value; } 
        }
        public int GruppenID 
        {
            get { return this.gruppenid; }
            set { this.kalenderid = value; } 
        }
        public int BenutzerID 
        {
            get { return this.benutzerid; }
            set { this.benutzerid = value; } 
        }
        public string Art 
        {
            get { return this.art; }
            set { this.art = value; } 
        }

        //Konstruktor
        public Kalender()
        {

        }
    }
}

和第四:

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

namespace WerIstWo.Models
{
    public class KalenderEintrag
    {
        //Eigenschaften
        private int kalendereintragid;
        private int kalenderid;
        private string art;
        private string datum;
        private string status;
        private string kommentar;

        //Properties
        public int KalendereintragID 
        {
            get { return this.kalendereintragid; }
            set { this.kalendereintragid = value; } 
        }
        public int KalenderID 
        {
            get { return this.kalenderid; }
            set { this.kalenderid = value; } 
        }
        public string Art 
        {
            get { return this.art; }
            set { this.art = value; } 
        }
        public string Datum 
        {
            get { return this.datum; }
            set { this.datum = value; } 
        }
        public string Status 
        {
            get { return this.status; }
            set { this.status = value; } 
        }
        public string Kommentar 
        {
            get { return this.kommentar; }
            set { this.kommentar = value; } 
        }

        //Konstruktor
        public KalenderEintrag()
        { 

        }
    }
}

我阅读这些教程以实现这一目标:

http://webproject.scottgu.com/csharp/Data/Data.aspx
http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/creating-an-entity-framework-data-model-for-an-asp-net-mvc-application

据我所知,我需要4个步骤从模型类生成数据库:

创建模型
创建上下文类
创建初始化程序
运行应用程序 - &gt; db生成

但是没有初始化程序,甚至没有上下文类,是否可以这样做?我只想让vs生成一个基于我的四个模型类的数据库?或者是必要的上下文和初始化类?

谢谢!

enter image description here

1 个答案:

答案 0 :(得分:0)

如果您使用脚手架,那么只要您没有任何多对多关系,就会生成所有内容。您可以访问此网站,阅读脚手架http://blog.stevensanderson.com/2011/01/13/scaffold-your-aspnet-mvc-3-project-with-the-mvcscaffolding-package/