我的应用程序很好,在IdentityModels中我设置了每个类(表)。但是我希望在我的Razor中显示AspNetUsers中的UserName而不是GUID。我目前存储GUID,但我能够在视图中显示
我认为必须有一种简单易用的方法 - 而且我不需要做任何映射或做我
我使用EF6和MVC4.5
这是我的班级:
public partial class x23BatchImport
{
public int x23BatchImportId { get; set; }
public Nullable<DateTime> DateTimeFromFilename { get; set; }
public string UserId { get; set; }
public string Filename { get; set; }
public decimal? Length { get; set; }
public Nullable<DateTime> StartDateTime { get; set; }
public Nullable<DateTime> StopDateTime { get; set; }
//public virtual ApplicationUser ApplicationUser { get; set; }
}
...这是IdentityModels.cs
namespace AscendancyCF.Models
{
// You can add profile data for the user by adding more properties to your ApplicationUser class, please visit http://go.microsoft.com/fwlink/?LinkID=317594 to learn more.
public class ApplicationUser : IdentityUser
{
}
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("DefaultConnection")
{
}
//public System.Data.Entity.DbSet<AscendancyCF.Models.ApplicationUser> ApplicationUser { get; set; }
public System.Data.Entity.DbSet<AscendancyCF.Models.SupplyPointType> SupplyPointTypes { get; set; } ETC ETC
.....注意我所有的表都在这里宣布然后我用OnModelCreating
来建立关系......
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
// Reference : http://blogs.msdn.com/b/adonet/archive/2010/12/06/ef-feature-ctp5-fluent-api-samples.aspx
base.OnModelCreating(modelBuilder);
// Configure the 1-1
modelBuilder.Entity<SupplyPoint>()
.HasOptional(a => a.SupplyPointAddress)
.WithMany()
.HasForeignKey(u => u.SupplyPointAddressId);
}
答案 0 :(得分:0)
实体框架有一组默认使用的模型和属性 naming conventions 。目前,它无法确定UserId
是ApplicationUser
的外键。
如果您不想添加任何手动映射,则必须更改命名。最简单的方法是将ApplicationUser
属性重命名为User
。
public virtual ApplicationUser User { get; set; }
在您进行查询时,使用Include()
急切加载User
属性并匹配ApplicationUser
...
// using System.Data.Entity; // add this using to use Include()
var context = new ApplicationDbContext();
var batchImport = context.x23BatchImport
.Include(x => x.User)
.(x => x.x23BatchImportId == 1)
.Single();
var username = batchImport.User.UserName;
您的其他选择是:
UserID
属性更改为ApplicationUserId
OnModelCreating()