簡體   English   中英

C#實體框架-代碼優先-數據對象屬性解析為空

[英]C# Entity Framework - Code First - Data Object Property Resolves Null

我正在開發一個多層的ASP.NET MVC Web應用程序,但是由於無法在應用程序中檢索我的數據的准確對象表示而無法進一步發展。 為什么會這樣呢? 我已嘗試盡可能詳細,但如果您想了解更多信息,請請求澄清。 我的項目如下:


-MyProject.Data
-MyProject.Logic
-MyProject.Objects
-MyProject.Web



我的實體類來自MyProject.Objects.Entities

public class Report
{
    [Key]
    public int Id { get; set; }
    public string ReportName { get; set; }
    public int ProductId { get; set; }
}

public class Product
{
    [Key]
    public int Id { get; set; }
    public string ProductName { get; set; }
    public ICollection<Report> ProductReports { get; set; }
}



我從MyProject.Data獲得的上下文

public class MyDb : DbContext
{
    public DbSet<Product> Products { get; set; }
    public DbSet<Report> Reports { get; set; }
}



我的控制器來自MyProject.Web

 public class HomeController : Controller
{
    MyDb _db = new MyDb();

    public ActionResult Index()
    {
        var model =
            from p in _db.Products
            orderby p.ProductName ascending
            select p;

        return View(model);
    }
}



最后,來自MyProject.Data.Migrations.Configuration的我的Seed()方法

protected override void Seed(MyProject.Data.MyDb context)
    {
        context.Products.AddOrUpdate(r => r.ProductName,
                new Product
                {
                    ProductName = "AAA",
                    ProductReports =
                        new List<Report> { 
                            new Report { ReportName = "Report A" },
                            new Report { ReportName = "Report B" },
                            new Report { ReportName = "Report C" }
                            }
                },
                new Product
                {
                    ProductName = "MSI",
                    ProductReports =
                        new List<Report> { 
                            new Report { ReportName = "Report X" },
                            new Report { ReportName = "Report Z" }
                            }
                });
    }




我不知道為什么我無法從MyProject.Objects.Entities.Product獲取ICollection<Report> ProductReports來填充正確的值。 這是MyProject.Web中 Index()結果

在此處輸入圖片說明



起初,我以為是這個框架的新手,所以我遇到了一些與數據結構有關的問題。 據我所知,我的數據似乎設置正確。 這是我在SQL Server中的數據

在此處輸入圖片說明




為了進一步證明數據似乎正確,我運行了以下查詢以確保自己已正確設置了關系。 我將在查詢旁邊提供結果。

SELECT * From Reports INNER JOIN Products ON Reports.ProductId = Products.Id


在此處輸入圖片說明

就像這樣:

public class ReportMap : EntityTypeConfiguration<Report>
    {
        public ReportMap()
        {
            // Primary Key
            this.HasKey(t => t.Id);

            // Properties
            this.Property(t => t.ReportName)
                .IsRequired()
                .HasMaxLength(100);

            // Table & Column Mappings
            this.ToTable("Reports");
            this.Property(t => t.Id).HasColumnName("Id");
            this.Property(t => t.ReportName).HasColumnName("ReportName");

            // Relationships
            this.HasRequired(t => t.Product)
                .WithMany(t => t.ProductReports)
                .HasForeignKey(d => d.ProductId);
        }
    }

並將您的報告更改為也具有產品屬性,例如:

public class Report
{
    [Key]
    public int Id { get; set; }
    public string ReportName { get; set; }
    public int ProductId { get; set; }
    public virtual Product Product { get; set; }
}

然后在您的上下文中:

public partial class SomeContext : DbContext
{
    static SomeContext()
    {
        Database.SetInitializer<SomeContext>(null);
    }

    public SomeContext()
        : base("Name=SomeContext")
    {
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Configurations.Add(new ReportMap());
    }
}

如前所述,您似乎缺少映射:

public class Product
{
  [Key]
  public int ProductID { get; set; }
  public string ProductName { get; set; }
  public virtual ICollection<Report> ProductReports { get; set; }
}

public class Report
{
  [Key]
  public int ReportID { get; set; }
  public string ReportName { get; set; }
  public int ProductID { get; set; }
  public virtual Product Product { get; set; }
}

兩個表中的映射字段的名稱必須相同,並且映射將自動使用EF為您完成。 您還可以將虛擬屬性添加到報表中,以便在報表中可以調用Product.ProductName或其他名稱。

希望這可以幫助

這是我正在工作的EF關系的示例:

public class CourseSection
{
    [Key]
    public int CourseSectionID { get; set; }

    public int CourseID { get; set; }

    public string Title { get; set; }

    public virtual ICollection<SectionContent> SectionContents { get; set; }
}

public class Course
{
    [Key]
    public int CourseID { get; set; }

    [StringLength(50)]
    public string Name { get; set; }

    public virtual ICollection<CourseSection> CourseSections { get; set; }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM