实体框架7导航关系对象引用未设置为对象实例

时间:2016-01-03 05:20:40

标签: c# asp.net-core-mvc entity-framework-core

我在sqlserver 2014中创建了一个数据库。我正在使用实体框架7,我通过dmx ef scaffolding命令在代码中反向设计了数据库。

在我尝试访问导航属性之前,事情似乎很好。所有关系对象都是null并且给出了错误:对象引用未设置为对象的实例。数据库中存在虚拟数据,因此似乎引用其他对象的属性永远不会被实例化。

任何想法我做错了什么?

波索' S:

public partial class Users
{
    public Users()
    {
        Orders = new HashSet<Orders>();
    }

    public int Id { get; set; }
    public bool Active { get; set; }
    public DateTime? BirthDate { get; set; }
    public DateTime Created { get; set; }
    public string Email { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Password { get; set; }
    public DateTime? Updated { get; set; }
    public string Username { get; set; }

    public virtual ICollection<Orders> Orders { get; set; }
}

public partial class Orders
{
    public Orders()
    {
        OrderLineItems = new HashSet<OrderLineItems>();
    }

    public int Id { get; set; }
    public bool Active { get; set; }
    public string Address { get; set; }
    public string City { get; set; }
    public DateTime Created { get; set; }
    public string State { get; set; }
    public decimal SubTotal { get; set; }
    public decimal Tax { get; set; }
    public decimal Total { get; set; }
    public DateTime? Updated { get; set; }
    public int? UserId { get; set; }
    public string Zip { get; set; }

    public virtual ICollection<OrderLineItems> OrderLineItems { get; set; }
    public virtual Users User { get; set; }
}

上下文:

public partial class DemoContext : DbContext
{
    protected override void OnConfiguring(DbContextOptionsBuilder options)
    {
        options.UseSqlServer(@"Data Source=localhost;Initial Catalog=Demo;Integrated Security=True");
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Items>(entity =>
        {
            entity.Property(e => e.Active).HasDefaultValue(true);

            entity.Property(e => e.Cost).HasColumnType("decimal");

            entity.Property(e => e.Created)
                .HasColumnType("datetime")
                .HasDefaultValueSql("getdate()");

            entity.Property(e => e.Description)
                .IsRequired()
                .HasMaxLength(60);

            entity.Property(e => e.Number)
                .IsRequired()
                .HasMaxLength(30);

            entity.Property(e => e.Price).HasColumnType("decimal");

            entity.Property(e => e.Sku).HasMaxLength(30);

            entity.Property(e => e.Upc).HasMaxLength(30);

            entity.Property(e => e.Updated).HasColumnType("datetime");
        });

        modelBuilder.Entity<OrderLineItems>(entity =>
        {
            entity.Property(e => e.Active).HasDefaultValue(true);

            entity.Property(e => e.Created)
                .HasColumnType("datetime")
                .HasDefaultValueSql("getdate()");

            entity.Property(e => e.Updated).HasColumnType("datetime");

            entity.HasOne(d => d.Item).WithMany(p => p.OrderLineItems).HasForeignKey(d => d.ItemId);

            entity.HasOne(d => d.Order).WithMany(p => p.OrderLineItems).HasForeignKey(d => d.OrderId);
        });

        modelBuilder.Entity<Orders>(entity =>
        {
            entity.Property(e => e.Active).HasDefaultValue(true);

            entity.Property(e => e.Address)
                .IsRequired()
                .HasMaxLength(45);

            entity.Property(e => e.City)
                .IsRequired()
                .HasMaxLength(45);

            entity.Property(e => e.Created)
                .HasColumnType("datetime")
                .HasDefaultValueSql("getdate()");

            entity.Property(e => e.State)
                .IsRequired()
                .HasMaxLength(45);

            entity.Property(e => e.SubTotal).HasColumnType("decimal");

            entity.Property(e => e.Tax).HasColumnType("decimal");

            entity.Property(e => e.Total).HasColumnType("decimal");

            entity.Property(e => e.Updated).HasColumnType("datetime");

            entity.Property(e => e.Zip)
                .IsRequired()
                .HasMaxLength(9)
                .HasColumnType("varchar");

            entity.HasOne(d => d.User).WithMany(p => p.Orders).HasForeignKey(d => d.UserId);
        });

        modelBuilder.Entity<Users>(entity =>
        {
            entity.Property(e => e.Active).HasDefaultValue(true);

            entity.Property(e => e.BirthDate).HasColumnType("date");

            entity.Property(e => e.Created)
                .HasColumnType("datetime")
                .HasDefaultValueSql("getdate()");

            entity.Property(e => e.Email)
                .IsRequired()
                .HasMaxLength(50);

            entity.Property(e => e.FirstName)
                .IsRequired()
                .HasMaxLength(40);

            entity.Property(e => e.LastName)
                .IsRequired()
                .HasMaxLength(40);

            entity.Property(e => e.Password)
                .IsRequired()
                .HasMaxLength(60);

            entity.Property(e => e.Updated).HasColumnType("datetime");

            entity.Property(e => e.Username)
                .IsRequired()
                .HasMaxLength(40);
        });
    }

    public virtual DbSet<Items> Items { get; set; }
    public virtual DbSet<OrderLineItems> OrderLineItems { get; set; }
    public virtual DbSet<Orders> Orders { get; set; }
    public virtual DbSet<Users> Users { get; set; }
}

执行代码:

var db = new DemoContext();
var orders = db.Orders.ToList();
var model = orders[0].User;
return View(model);

我为订单上的用户属性获取null。任何想法???

1 个答案:

答案 0 :(得分:0)

我以前遇到过这个问题 你可以删除

  Orders = new HashSet<Orders>();

并设置

  this.Configuration.LazyLoadingEnabled = true;

在延迟加载的上下文构造函数中,你不需要.include(.............)