【问题标题】:Use existing tables on ASP.net MVC使用 ASP.net MVC 上的现有表
【发布时间】:2018-05-17 12:30:33
【问题描述】:

我有一个现有的数据库,其中包含一个名为“Unidades”的表,我正在尝试为 AspNetUsers 提供一个单一的属性 Unidades:

public class ApplicationUser : IdentityUser
{

    public Unidades Unidad { get; set; }
...

但是,当我这样做时,ASP 正在尝试创建一个新表 Unidades 而不是使用我的。 如何告诉 ASP 使用我现有的表?

谢谢!

【问题讨论】:

  • 谢谢你 Waragi,但我已经用数据库优先方法导入了数据库。这就是为什么我有 Unidades 课程。但是 ASP 在 AspNetUser 类上没有使用现有的表。
  • 所以您想使用Unidades 而不是AspNetUsers?我假设您在Unidades 中拥有所有列,这些列都应该在AspNetUsers 中出现
  • @Izzy,我希望我的 AspNetUsers 拥有一个“Unidades”属性

标签: c# asp.net .net model-view-controller


【解决方案1】:

如果您使用的是 Entity Framework 6,那么将您的属性从 Unidades 类型更改为虚拟 ICollection{Unidade} 可能会更容易,如https://www.entityframeworktutorial.net/entity-relationships.asp 所示。

 public class ApplicationUser : IdentityUser
    {
        public virtual ICollection<Unidad> Unidades { get; set; }    
    }

然后您可以在 Unidad 类上添加一个 Table 数据注释,如此处所示https://www.entityframeworktutorial.net/code-first/table-dataannotations-attribute-in-code-first.aspx

[Table("Unidades")]
public class Unidad {    
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-09-10
    • 2011-01-17
    • 1970-01-01
    • 2010-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-22
    相关资源
    最近更新 更多