EF实体未指定导航属性

时间:2017-07-17 14:20:29

标签: c# entity-framework model-view-controller ef-code-first

我尝试了几件事情,但我无法将其付诸实践。

我有2个实体。 User和UserDetails(如下所述)。我设法添加一个用户和它的用户详细信息但是当我尝试使用include(“UserDetails”)时,我得到以下错误。

  

指定的包含路径无效。 EntityType   'Floralist.Repository.Context.User'未声明导航   名为“UserDetails”的属性。

实体如下:

public class User : Entity
{
    public User()
    {
        Details = new UserDetails();
        //FlowerList = new Collection<Flower>();
    }

    public Guid Id { get; set; }

    public string Username { get; set; }

    [Index(IsUnique = true)]
    public string Email { get; set; }

    public string Password { get; set; }

    public Roles Role { get; set; }

    public UserState State { get; set; }

    public int? DetailsId { get; set; }
    public UserDetails Details { get; set; }

    public DateTime? LastLoginDate { get; set; }

    //public ICollection<Flower> FlowerList { get; set; }

    //public ICollection<Friend> FriendList { get; set; }

}

public class UserDetails : Entity
{
    public int Id { get; set; }

    public string FirstName { get; set; }

    public string LastName { get; set; }

    public string FullName => $"{this.FirstName} {this.LastName}";

    public string Address { get; set; }

    public string PostalCode { get; set; }

    public DateTime BirthDate { get; set; }

    public int? WatermarkId { get; set; }

    public Blob Watermark { get; set; }

    public int? ProfilePictureId { get; set; }

    public Blob ProfilePicture { get; set; }
}

映射如下:

public class UserMap : EntityTypeConfiguration<User>
{
    public UserMap()
    {
        this.HasKey(t => t.Id);

        this.ToTable("Users");

        this.Property(t => t.Id).HasColumnName("Id");

        this.Property(t => t.Username)
            .IsRequired()
            .HasMaxLength(48)
            .HasColumnName("Username");

        this.Property(t => t.Password)
            .HasMaxLength(256)
            .HasColumnName("Password");

        this.Property(t => t.Email)
            .IsRequired()
            .HasMaxLength(256)
            .HasColumnName("Email");

        this.Property(t => t.Role)
            .IsRequired()
            .HasColumnName("Role");

        this.Property(t => t.State)
            .IsRequired()
            .HasColumnName("State");

        this.Property(t => t.LastLoginDate)
            .IsOptional()
            .HasColumnName("LastLoginDate");

        this.Property(t => t.CreateDate)
            .IsRequired()
            .HasColumnName("CreateDate");

        this.Property(t => t.UpdateDate)
            .IsOptional()
            .HasColumnName("UpdateDate");

        this.Property(t => t.DetailsId)
            .HasColumnName("DetailsId");
    }
}

public UserDetailsMap()
    {
        this.HasKey(t => t.Id);

        this.ToTable("UsersDetails");

        this.Property(t => t.Id).HasColumnName("Id");

        this.Property(t => t.WatermarkId)
            .HasColumnName("WatermarkId");

        this.Property(t => t.ProfilePictureId)
            .HasColumnName("ProfilePictureId");

        this.Property(t => t.FirstName)
            .IsRequired()
            .HasMaxLength(64)
            .HasColumnName("FirstName");

        this.Property(t => t.LastName)
            .IsRequired()
            .HasMaxLength(64)
            .HasColumnName("LastName");

        this.Property(t => t.Address)
            .HasMaxLength(512)
            .HasColumnName("Address");

        this.Property(t => t.PostalCode)
            .HasMaxLength(64)
            .HasColumnName("PostalCode");

        this.Property(t => t.BirthDate)
            .HasColumnName("BirthDate");

        this.Property(t => t.CreateDate)
            .IsRequired()
            .HasColumnName("CreateDate");

        this.Property(t => t.UpdateDate)
            .IsRequired()
            .HasColumnName("UpdateDate");

        //this.HasOptional(t => t.ProfilePicture).WithMany().HasForeignKey(f => f.ProfilePictureId);
        //this.HasOptional(t => t.Watermark).WithMany().HasForeignKey(f => f.WatermarkId);
    }

上下文:

internal class FloralistContext : DbContext
{
    private const string Dbmodule = "Floralistdb";

    public FloralistContext() : base(Dbmodule)
    {
    }

    public DbSet<User> Users { get; set; }

    public DbSet<UserDetails> UserDetails { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Conventions.Remove<ManyToManyCascadeDeleteConvention>();
        modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();

        modelBuilder.Configurations.Add(new UserMap());
        modelBuilder.Configurations.Add(new UserDetailsMap());
    }
}

执行此操作时出现错误:

            var user = Repository.Users.Include("UserDetails").FirstOrDefault(u => u.Email.Equals(email, StringComparison.InvariantCultureIgnoreCase) && u.Password.Equals(password));

如果我没有指定包含路径,则用户详细信息将显示为空。

1 个答案:

答案 0 :(得分:0)

我终于通过阅读这篇文章EF understanding association

来实现这一目标

我必须将User中的虚拟属性设置为UserDetails,将UserDetails中的虚拟属性设置为User和映射this.HasOptional(x => x.Details).WithRequired(x => x.User);

感谢所有人