Asp.net mvc AspNet如何识别我的模型
我正试图在上完成本教程。我需要一些初步的解释才能继续下去。查看MVC5附带的默认模板,我看到以下内容:Asp.net mvc AspNet如何识别我的模型,asp.net-mvc,asp.net-identity,Asp.net Mvc,Asp.net Identity,我正试图在上完成本教程。我需要一些初步的解释才能继续下去。查看MVC5附带的默认模板,我看到以下内容: // You can add profile data for the user ... public class ApplicationUser : IdentityUser { public string HomeTown { get; set; } public DateTime? BirthDate { get; set; } } 如果我想将其称为User而不是App
// You can add profile data for the user ...
public class ApplicationUser : IdentityUser
{
public string HomeTown { get; set; }
public DateTime? BirthDate { get; set; }
}
如果我想将其称为User而不是ApplicationUser,该怎么办?也许我想添加一个导航属性,以便与其他模型建立关系?另外,当我查看该表时,名称是AspNetUsers,而不是ApplicationUser。最后,如果我想使用自己的上下文呢
谢谢你的帮助
1) 如果我想叫它User而不是ApplicationUser呢
您可以更改为所需的任何名称,只需确保将ApplicationUser替换为该名称即可
2) 也许我想添加一个导航属性,以便建立
我和其他模特的关系
如果您有类调用地址,则要将addressId添加为外键,请参见以下内容:
public class Address
{
public int Id { get; set; }
public string Street { get; set; }
}
public class User : IdentityUser
{
public string HomeTown { get; set; }
public DateTime? BirthDate { get; set; }
public int AddressId { get; set; }
[ForeignKey("AddressId")]
public virtual Address Address { get; set; }
}
public class ApplicationDbContext : IdentityDbContext<User>
{
public ApplicationDbContext()
: base("DefaultConnection")
{
}
public DbSet<Address> Addresses { get; set; }
}
公共类地址
{
公共int Id{get;set;}
公共字符串Street{get;set;}
}
公共类用户:IdentityUser
{
公共字符串{get;set;}
公共日期时间?生日{get;set;}
public int AddressId{get;set;}
[外键(“地址ID”)]
公共虚拟地址{get;set;}
}
公共类ApplicationDbContext:IdentityDbContext
{
公共应用程序上下文()
:base(“默认连接”)
{
}
公共数据库集
4) 如果我想使用自己的上下文呢
您可以创建自己的DBContex,MVC 5允许您拥有多个DBContext,例如ApplicationDbContext和DataDbContext(自定义DBContext)。
ApplicationDbContext通常包含ASP.NET成员资格数据表。
DataDbContext通常包含与用户无关的数据表
public class Blog
{
public int BlogId { get; set; }
public int Title { get; set; }
}
public class MyDbContext : DbContext
{
public MyDbContext()
: base("DefaultConnection")
{
}
public DbSet<Blog> Blogs { get; set; }
}
公共类博客
{
public int BlogId{get;set;}
公共整数标题{get;set;}
}
公共类MyDbContext:DbContext
{
公共MyDbContext()
:base(“默认连接”)
{
}
公共数据库集博客{get;set;}
}
注意:您可能需要使用EF迁移,请参阅此处的详细信息:
默认数据库和表是在哪里创建的?@Lin,非常感谢您的回答。另一个快速问题-如果我想在自己的模型中有一个字段引用应用程序用户,该怎么办?我该如何完成?