【问题标题】:Displaying Other Columns than Primary Key int asp MVC显示除主键 int asp MVC 之外的其他列
【发布时间】:2017-01-04 19:24:12
【问题描述】:

我正在尝试学习 ASP.NET MVC 5,但我一开始就卡住了 :) 我有简单的数据库

  • dbo.Books (int Id(PK), nvarchar(50) Title, int Author(foreignkey))
  • dbo.Author (int ID(PK), nvarchar(50) 名称)

我正在尝试使用实体框架显示书籍列表。我的模型看起来像:

public class Author
{
    public int ID { get; set; }
    public string  Name { get; set; }
    public IEnumerable<Book> Books { get; set; }
}

public class MovieContext :DbContext
{
    public DbSet<Books> Books{ get; set; }
    public DbSet<Author> Authors { get; set; }
}

public class Book
{
    public int Id { get; set; }
    public int Author { get; set; }
    public string Title { get; set; }
    [ForeignKey("Author")]
    public virtual Author Authorr { get; set; }
}

当我转到BooksController/index 时,它会显示书籍列表,但它显示的不是作者姓名,而是 ID。我试图更改代码

@Html.DisplayFor(modelItem => item.Author)

为此:

@Html.DisplayFor(modelItem => item.Authorr.Name)

在 Index.cshtml 类中

但现在我收到一个错误:

无效的对象名称 dbo.Authors。

我的表被称为Author,我不知道它为什么要寻找Authors

我应该更改什么以显示正常名称而不是 Author 的 Id?

【问题讨论】:

    标签: asp.net asp.net-mvc entity-framework razor


    【解决方案1】:

    将您的外键列更改为 AuthorId 并将外键导航属性从 Authorr 更改为 Author

    public class Book
    {
        public int Id { get; set; }
        public int AuthorId { get; set; }
        public string Title { get; set; }        
        public virtual Author Author { get; set; }
    }
    

    现在在你看来你可以这样做

    @Html.DisplayFor(modelItem => item.Author.Name)
    

    另外在获取数据时,也可以使用Include方法加载作者数据。

    var books = yourDbContext.Books.Include("Author").ToList();
    return View(books);
    

    【讨论】:

    • 我确实像你说的那样,但它没有解决问题。 EF 仍在寻找 Authors 表而不是 Author
    • 您是否通过这些更改重建了您的数据库?我看不出有任何失败的理由
    • 我刚刚复制并粘贴了您的代码,它完全适合我。唯一的变化是public DbSet&lt;Book&gt; Books{ get; set; } 不是public DbSet&lt;Books&gt; Books{ get; set; }
    • 是的,我做到了,但它没有帮助
    • 我通过在 MSSQL 中更改 TableName 解决了这个问题。感谢您的帮助。
    【解决方案2】:

    总共有 4 个问题。

    这里有 1 个问题:

    dbo.Books (int Id(PK), 
    nvarchar(50) Title, 
    int Author(foreignkey) //Wrong)
    

    应该是

      dbo.Books (int Id(PK), 
        nvarchar(50) Title, 
        int AuthorId)
    

    这里有 1 个问题:

    dbo.Author //Wrong
    (int ID(PK), nvarchar(50) Name)
    

    应该是

    dbo.Authors (int ID(PK), nvarchar(50) Name)
    

    这里有 2 个问题:

     public class Book
        {
            public int Id { get; set; }
            public int Author { get; set; } //Wrong
            public string Title { get; set; }
            [ForeignKey("Author")]
            public virtual Author Authorr { get; set; } //Wrong
        }
    

    您应该如下所示进行更改。

    public class Book
    {
        public int Id { get; set; }
        public string Title { get; set; }
    
        [ForeignKey("Author")]
        public virtual Author Author { get; set; } 
        public int AuthorId { get; set; }
    }
    

    强烈推荐你关注Getting Started with ASP.NET MVC 5系列文章学习ASP.net MVC 5

    【讨论】:

    • 我像你说的那样改变了它,但它没有用。它仍在尝试从作者而不是作者表获取数据
    • 我没有使用任何特定的教程。我只是在寻找一些关于 MVC 中模型之间关系的信息,我正在尝试创建自己的关系
    • 你不喜欢以正确的方式学习框架吗?如果你喜欢,那么你可以关注上面提到的系列文章。
    • 是的,我通过在 MS SQL 中更改表名解决了这个问题,我知道这不是正确的方法,但我不知道如何以其他方式解决它。谢谢您的帮助。我添加了链接,您在上面发布了一些帖子到收藏夹标签,我将开始从中学习:) 再次感谢您
    • 好的,祝你好运! :)
    猜你喜欢
    • 2021-08-15
    • 1970-01-01
    • 1970-01-01
    • 2020-01-10
    • 1970-01-01
    • 2017-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多