如何使用一个模型和一个视图添加多个表?

时间:2014-10-27 09:11:15

标签: c# asp.net-mvc

我希望有一个页面,用户从下拉列表中选择该类别,然后添加一个关于该类别的小文本,并上传一个图像,其中该图像的路径保存在数据库中而不是整个图像中。我创建了一个表“类别”,管理员有权填写它,用户只能从类别列表中选择。

这是我到目前为止所做的:

创建类别模型:

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

namespace DemoIdentity.Models
{
    public class CategoriesAdmin
    {
        public int ID { get; set; }

        [Required(AllowEmptyStrings = false)]
        [Display(Name = "category name")]
        public string categoryName { get; set; }
    }

    public class DefaultConnection:DbContext
    {
        public DbSet<CategoriesAdmin> categories { get; set; }

    }
}

现在我想要另一个表(Data),其中包括(ID,Category(从表类别中选择的类别名称),News,Image_Path)。此表位于“默认连接”数据库中。类别名称是下拉列表中选定的类别名称,图像路径是保存路径而不是整个图像的上传图像。

我不确定如何实现这一目标。

1 个答案:

答案 0 :(得分:2)

您似乎混淆了ASP.NET MVCEntity Framework的组件。

正如实体框架网站所述:

  

实体框架(EF)是一个支持.NET的对象关系映射器   开发人员使用特定于域的对象处理关系数据。   它消除了对大多数数据访问代码的需求   开发人员通常需要写。

MVC网站声明:

  

ASP.NET MVC是一个开源的Web应用程序框架   实现模型 - 视图 - 控制器(MVC)模式。

这两个框架通过您的模型类相遇。 MVC使用模型类来定义应用程序的数据,逻辑和规则。在Entity Framework中,模型类映射到数据库中的表,它处理直接读写操作。

创建CategoriesAdmin模型类并将其作为DbContext类中的属性公开,如下所示:

public class DefaultConnection:DbContext
{
    public DbSet<CategoriesAdmin> categories { get; set; }
}

实体框架将您的模型类映射到名为CategoriesAdmins的数据库表。如果您的数据库中尚不存在此表,则会自动为您创建该表。实体框架中的这种方法称为Code First to a new Database

现在,由于您已经有一个存储可用类别(CategoriesAdmin)的表,因此您需要创建第二个模型类(为了您的示例而称为Data),其中包含您要存储的其他信息。

public class Data
{
      // gets or sets the ID of this Data record.
      public int ID {get;set;}

      public string ImagePath {get;set;}

      // other properties
      ...
}

现在您有两个模型类,您需要在两者之间创建关系。在SQL数据库中,这是通过外键实现的。在Entity Framework中,您可以使用Navigational Properties来实现相同的目标。

因此我们更新Data模型类:

public class Data
{
      // gets or sets the ID of this Data record.
      public int ID {get;set;}

      public string ImagePath {get;set;}

      // gets or sets the ID of the related CategoriesAdmin record.
      public int CategoriesAdminId {get;set;}

      // gets or sets the related CategoriesAdmin record. Entity Framework will
      // automatically populate this property with an object for the related
      // CategoriesAdmin record.
      [ForeignKey("CategoriesAdminId")]
      public virtual CategoriesAdmin CategoriesAdmin {get;set;}

      // other properties
      ...
}

CategoriesAdmin属性上的ForeignKeyAttribute可以为Entity Framework提供进一步提示加载导航属性的外键列。

最后,为了能够将新的Data模型类与Entity Framework一起使用,您需要向DbContext类添加另一个属性,以便您可以访问数据:

public class DefaultConnection:DbContext
{
    public DbSet<CategoriesAdmin> Categories { get; set; }

    public DbSet<Data> Data { get; set; }
}

现在您已经创建了模型类并将它们连接到Entity Framework,现在您可以在MVC中使用它们了。如果您将Data模型加载到视图中(使用DefaultConnection.Data),则可以通过访问CategoriesAdmin对象上的Data属性来访问相关的CategoriesAdmin记录。

简而言之:两个表意味着您需要两个模型。两种模型都可以加载到单个视图中。

脚注:道歉,如果我的答案中存在很大的差距,因为有很多解释已经在其他地方解释得比我能做的要好得多。我所链接的参考文献应该填补空白。

如果您需要更多帮助,请参阅the tutorials on the ASP.NET MVC website on working with data的全部内容。他们的写作要比我简洁的尝试好得多。我建议您完全遵循它们并在完成自己的项目之前使示例工作,以便您更好地理解这两个框架如何工作并相互交互。