我想制作一个简单的应用程序,其中涉及IT技术。 我通过Entity Framework引入了三个SQL表,并自动创建了模型。 我希望能够在Visual Studio中自动地生成Create/Details/Edit等视图。当我从单个模型(例如只有Name)进行脚手架时,我可以自动完成此操作,但是使用View Model作为源时却无法做到。
这是我的模型:
Name
public partial class Name
{
public Name()
{
this.Addresses = new HashSet<Address>();
this.Emails = new HashSet<Email>();
}
public int ID { get; set; }
public string FIRST_NAME { get; set; }
public string LAST_NAME { get; set; }
public virtual ICollection<Address> Addresses { get; set; }
public virtual ICollection<Email> Emails { get; set; }
}
地址
public partial class Address
{
public int ADDRESS_ID { get; set; }
public int NameID { get; set; }
public string ADDRESS_1 { get; set; }
public string CITY { get; set; }
public string STATE { get; set; }
public string ZIP { get; set; }
public virtual Name Name { get; set; }
}
电子邮件
public partial class Email
{
public int EMAIL_ID { get; set; }
public int NameID { get; set; }
public string EMAIL { get; set; }
public virtual Name Name { get; set; }
}
以及我创建的包含这三个内容的 视图模型(View Model)
public class MainVM
{
public Name Name { get; set; }
public Address Address { get; set; }
public Email Email { get; set; }
}
我可以指导您创建控制器: 右键单击“Controllers” >> “Add” >> “Controller” >> “MVC 5 Controller with views, using Entity Framework”。 接下来,我会看到如下屏幕。 如果我点击“Add”,就会出现以下错误。 我已经在其他答案中阅读到,如果您使用视图模型,则需要清除第一个图像中的数据上下文类,但是如果我这样做,添加按钮将变为无效。 我不能再走得更远了。 有什么建议吗?