MVC5+EF 练习 读取数据问题

先要在Details页面里面显示详细信息 但是
SysUserRole表 和 SysRole表中的数据读不出来
但是好像就没有写进数据库 因为数据库中只有SysUser表中有内容 其他两个表是空的
图片说明

图片说明

下面是AccountInitializer.cs
初始化代码

 using MVC5_EF.Models;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;


namespace MVC5_EF.DAL
{
    public class AccountInitializer:DropCreateDatabaseIfModelChanges<AccountContext>//如果model改变 删除重建数据库
    {
        protected override void Seed(AccountContext context)
        {
            var sysUsers = new List<SysUser>
            {
                new SysUser { UserName="Tom",Email="Tom@qq.com",Password="1" },
                new SysUser { UserName="Terry",Email="Jerry@qq.com",Password="2" }
            };
            sysUsers.ForEach(s=>context.SysUsers.Add(s));
            context.SaveChanges();

            var sysRoles = new List<SysRole>
            {
                new SysRole { RoleName="Administrators",RoleDesc="Administrators have full authorization to perform system administration."},
                new SysRole { RoleName="Terry",RoleDesc="General Users can access the shared data they are authorizen for."}
            };
            sysRoles.ForEach(s=>context.SysRoles.Add(s));
            context.SaveChanges();

            var sysUserRoles = new List<SysUserRole>
            {
                new SysUserRole { SysUserID=1,SysRoleID=1 },
                new SysUserRole { SysUserID=1,SysRoleID=2 },
                new SysUserRole { SysUserID=2,SysRoleID=2 }
            };
            sysUserRoles.ForEach(s=>context.SysUserRoles.Add(s));
            context.SaveChanges();
        }
    }
}

下面是Details页面

 @model MVC5_EF.Models.SysUser


<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Detail</title>
</head>
<body>
    <div>
        <div>
            <dl>
                <dt>@Html.DisplayNameFor(model => model.UserName)</dt>
                <dd>@Html.DisplayFor(model => model.UserName)</dd>

                <dt>@Html.DisplayNameFor(model => model.Email)</dt>
                <dd>@Html.DisplayFor(model => model.Email)</dd>

                <dt>@Html.DisplayNameFor(model => model.SysUserRoles)</dt>
                <dd>
                    <table>
                        <tr>
                            <th>RoleName</th>
                            <th>RoleDesc</th>
                        </tr>
                        @foreach (var item in Model.SysUserRoles)
                        {
                            <tr>
                                <td>@Html.DisplayFor(modelItem => item.SysRole.RoleName)</td>
                                <td>@Html.DisplayFor(modelItem => item.SysRole.RoleDesc)</td>
                            </tr>
                        }
                    </table>
                </dd>
            </dl>
        </div>

    </div>
</body>
</html>


SysUser类

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

namespace MVC5_EF.Models
{
    public class SysUser
    {
        public int ID { get; set; }
        public string UserName { get; set; }
        public string Email { get; set; }
        public string Password { get; set; }
        public virtual ICollection<SysUserRole> SysUserRoles { get; set; }
    }
}

最后只能读取这么多
图片说明

大佬们帮忙给看看

1个回答

你的 Seed 里面没有写 User 和 Role 的关系代码。
你用了导航属性吗?

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐