【问题标题】:asp.net entity framework confusedasp.net 实体框架一头雾水
【发布时间】:2013-07-02 09:29:08
【问题描述】:

早安,

我的程序给我带来了很多麻烦。突然给我一个错误:

列名“UserProfile_UserId”无效。

对于此代码:

public ActionResult Index()
{
---> var model = _db.Roles.ToList();    
return View(model);

这是有道理的。但问题是我什至没有使用用户配置文件类。我正在使用 Roles 类。

这是我的控制器:

 FSKDb _db = new FSKDb();
    //
    // GET: /Roles/

    public ActionResult Index()
    {
        var model = _db.Roles.ToList();

        return View(model);
    }

这是我的数据库类:

namespace Attempt3.Models
{
public class FSKDb : DbContext
{
    public FSKDb() : base("name=DefaultConnection")
    {
    }

    public DbSet<UserProfile> UserProfiles { get; set; }
    public DbSet<Roles> Roles { get; set; }   
}
}

角色类:

[Table("webpages_Roles")]
public class Roles
{
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int RoleId { get; set; }
    public string RoleName { get; set; }
    public string Description { get; set; }
}

用户配置文件类

   [Table("UserProfile")]
public class UserProfile
{
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int UserId { get; set; }
    public string UserName { get; set; }
    public string Surname { get; set; }
    public string ClientCode { get; set; }
    public ICollection<Roles> UserRoles { get; set; }
}

我不知道为什么当我尝试在我的控制器中查找 EF 试图查找 User Profiles_UserID 的角色时

它应该只在我寻找用户而不是角色时寻找。即便如此,UserProfile UserId 列名只是“UserId”而不是“UserProfile_UserId”

谢谢

【问题讨论】:

  • 嘿,我从你的问题中复制了所有代码,我没有遇到任何问题。你在 OnModelCreating 或 EF 的其他 fluent api 部分有什么技巧吗?
  • 第二个问题:您的 EF 代码在用户和角色之间生成一对多的关系(一个用户有多个角色,一个角色有一个用户)。这似乎很奇怪。也许你需要多对多的关系? (一个用户有很多角色,一个角色有很多用户)。如果是这样,您必须添加到 UserProfiles 的 Roles 类集合中。
  • 有些事情很奇怪,它也对我有用,它从这个错误开始,但它与我的模型有关。我正在尝试将角色输入到默认生成的角色表中。}}
  • 我只是在使用默认角色表。它有另一个名为“角色中的用户”的表,您可以在其中为一个角色链接多个用户。

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


【解决方案1】:

UserProfileRoles 之间存在一对多关系。因此,每个角色都与一个用户相关联,因此 UserProfile_ID 存储在角色表中。要建立多对多关系,您必须这样做:

  1. UserProfile 类中指定它有很多 Roles - 由您完成
  2. Role 类中指定它有许多用户配置文件:

    [Table("webpages_Roles")]
    public class Roles
    {
        [Key]
        [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
        public int RoleId { get; set; }
        public string RoleName { get; set; }
        public string Description { get; set; }
        public ICollection<UserProfile> Users { get; set; }
    }
    

是的,就是这样。现在,EF 将生成名为UserProfileRoles 的第三个表,并将在UserProfileUserProfileRoles 之间、RoleUserProfileRoles 之间建立隐藏的一对多关系。如果你想改变表名,在你的上下文类中你必须覆盖OnModelCreating并使用fluent api来配置类到表的映射:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder
        .Entity<UserProfile>()
        .HasMany<Roles>(u => u.UserRoles)
        .WithMany(u => u.Users)
        .Map(m => {
            m.ToTable("_myOwnMapTable_UserProfile__Roles");
        });

    base.OnModelCreating(modelBuilder);
}

链接:

  1. Get Started with Entity Framework (EF)
  2. Configuring/Mapping Properties and Types with the Fluent API
  3. Configuring Relationships with the Fluent API

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-07
    • 2015-09-27
    • 2011-12-10
    • 1970-01-01
    • 2020-05-26
    • 2010-09-25
    相关资源
    最近更新 更多