【问题标题】:generate database from model classes从模型类生成数据库
【发布时间】:2013-03-07 12:59:01
【问题描述】:

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

第一:

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 个步骤来从模型类生成数据库:

创建模型
创建上下文类
创建初始化程序
运行应用程序 -> 生成数据库

但是没有初始化器或者甚至没有上下文类也可以做到吗?我的想法是让 vs 仅基于我的四个模型类生成数据库?还是需要上下文和初始化类?

谢谢!

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

    • 谢谢,我已经设置了 mvc 脚手架......但我仍然有问题,我收到一个错误:Invoke-Scaffolder:“找不到与名为“Benutzer”的控制器相对应的模型类型。尝试提供 -ModelType 参数值”我认为它找不到我的模型类,它在一个额外的模型文件夹中。
    • 你能贴一张你项目的文件夹/文件目录树的截图吗?
    • 一种解决方法是将 DBContext 从 DbSet 更改为 DbSet。这应该可以解决您的问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-07-01
    • 2011-03-23
    • 2018-12-12
    • 2011-02-23
    • 2021-04-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多