C#实体框架-代码优先-数据对象属性解析为Null
我正在开发一个多层ASP.NET MVC web应用程序,但由于无法在应用程序中检索数据的准确对象表示形式,因此无法进一步开发为什么会这样?我试图尽可能详细,但如果您想了解更多,请要求澄清。我的项目如下:C#实体框架-代码优先-数据对象属性解析为Null,c#,.net,asp.net-mvc,entity-framework,n-tier-architecture,C#,.net,Asp.net Mvc,Entity Framework,N Tier Architecture,我正在开发一个多层ASP.NET MVC web应用程序,但由于无法在应用程序中检索数据的准确对象表示形式,因此无法进一步开发为什么会这样?我试图尽可能详细,但如果您想了解更多,请要求澄清。我的项目如下: MyProject.Objects.Entities中的我的实体类 public class Report { [Key] public int Id { get; set; } public string ReportName { get; set; }
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; }
}
最后,我的
Seed()
方法来自MyProject.Data.Migrations.Configuration
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" }
}
});
}
可能是这样的:
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 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分部类SomeContext:DbContext
{
静态SomeContext()
{
Database.SetInitializer(null);
}
公共上下文()
:base(“Name=SomeContext”)
{
}
模型创建时受保护的覆盖无效(DbModelBuilder modelBuilder)
{
modelBuilder.Configurations.Add(newreportmap());
}
}
应该是这样的:
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 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分部类SomeContext:DbContext
{
静态SomeContext()
{
Database.SetInitializer(null);
}
公共上下文()
:base(“Name=SomeContext”)
{
}
模型创建时受保护的覆盖无效(DbModelBuilder modelBuilder)
{
modelBuilder.Configurations.Add(newreportmap());
}
}
如前所述,您似乎缺少映射:
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; }
}
公共类产品
{
[关键]
public int ProductID{get;set;}
公共字符串ProductName{get;set;}
公共虚拟ICollection ProductReports{get;set;}
}
公开课报告
{
[关键]
public int ReportID{get;set;}
公共字符串ReportName{get;set;}
public int ProductID{get;set;}
公共虚拟产品产品{get;set;}
}
两个表中映射字段的名称必须相同,并且映射将使用EF自动完成。您还可以向报表中添加虚拟属性,以便在报表中调用Product.ProductName或其他名称
希望这有帮助如前所述,您似乎缺少映射:
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; }
}
公共类产品
{
[关键]
public int ProductID{get;set;}
公共字符串ProductName{get;set;}
公共虚拟ICollection ProductReports{get;set;}
}
公开课报告
{
[关键]
public int ReportID{get;set;}
公共字符串ReportName{get;set;}
public int ProductID{get;set;}
公共虚拟产品产品{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; }
}
公共课程部分
{
[关键]
public int CourseSectionID{get;set;}
public int CourseID{get;set;}
公共字符串标题{get;set;}
公共虚拟ICollection节内容{get;set;}
}
公共课
{
[关键]
public int CourseID{get;set;}
[长度(50)]
公共字符串名称{get;set;}
公共虚拟ICollection CourseSections{get;set;}
}
以下是我工作时的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; }
}
公共课程部分
{
[关键]
public int CourseSectionID{get;set;}
public int CourseID{get;set;}
公共字符串标题{get;set;}
公共虚拟ICollection节内容{get;set;}
}
公共课
{
[关键]
public int CourseID{get;set;}
[长度(50)]
公共字符串名称{get;set;}
公共虚拟ICollection CourseSections{get;set;}
}
看起来您实际上从未定义过两个实体之间的映射。那个密码丢失了吗?也许。我的项目中没有任何类型的映射代码。我还没有看到这样的例子。我会研究一下的,谢谢@wilso132它看起来不像你真正定义过两个实体之间的映射。那个密码丢失了吗?也许。我的项目中没有任何类型的映射代码。我还没有看到这样的例子。我会调查的,谢谢@wilso132顶部的映射就是您要查找的,特别是关系部分。您已经通过属性定义了键等等,这只是一种不同的方式。所以我需要为我希望设置关系的每个实体创建一个映射类?我认为EF的秘密在于,如果命名正确,简单的东西“只起作用”。@scniro,只有当你需要用一种特定的方式定义关系时。Jay提供的答案显示了如何建立自动关系,但它通常无法区分HasRequired、HasOptional和HasMany中的几个选项。感谢您的进一步解释。我现在可以看到覆盖自动设置的好处。Jay还将我的ICollection ProductReports标记为虚拟,这对我很有用。顶部的映射是您要查找的,特别是关系部分。您已经通过属性定义了键等等,这只是一种不同的方式。所以我需要为我希望设置关系的每个实体创建一个映射类?我认为EF的秘密在于,如果命名正确,简单的东西“只起作用”。@scniro,只有当你需要用一种特定的方式定义关系时。Jay提供的答案显示了如何建立自动关系,但它通常无法区分HasRequired、HasOptional和HasMany中的几个选项。感谢您的进一步解释。我现在可以看到覆盖自动设置的好处。Jay还将我的ICollection ProductReports标记为虚拟,这对我来说很有效
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; }
}