【发布时间】:2021-07-29 09:35:18
【问题描述】:
实体框架不会多次包含嵌套实体及其数据。
以下是邮递员的回复:
[{
"ProjectID": 29,
"ProjectName": "Angular",
"ProjectDescription": "This is angular project for developing webpages.",
"ProjectURL": "angular.com",
"ApplicationEntity": [],
"ApplicationID": 21
},
{
"ProjectID": 30,
"ProjectName": "Dot Net 6",
"ProjectSlug": "MAUI",
"ProjectDescription": "This is project for .net framwork MAUI.",
"ProjectURL": "maui.com",
"ApplicationEntity": [
{
"ApplicationID": 21,
"ApplicationName": "Custom Application",
"ApplicationVersion": "1.0.0.0",
"ApplicationDescription": "This is for a custom implementation."
}
],
"ApplicationID": 21
}]
如您所见,两个条目中的"ApplicationID" : 21 相同。但是ApplicationEntity 不包括"ProjectID":29 的值。
CreateProject.cs
public ProjectEntity CreateProject(int id, ProjectEntity projectEntity)
{
projectEntity.ApplicationID = id;
CustomerSupportDBContext dBContext = new CustomerSupportDBContext();
ApplicationEntity applicationEntity = dBContext.ApplicationEntities.SingleOrDefault(e => e.ApplicationID == id);
using (dBContext)
{
projectEntity.ApplicationEntity.Add(applicationEntity);
dBContext.ProjectEntities.Add(projectEntity);
dBContext.SaveChanges();
}
return projectEntity;
}
编辑: ProjectController.cs
[Route("getAllProjects")]
[HttpGet]
public HttpResponseMessage GetAllProjects()
{
try
{
ProjectService projectService = new ProjectService();
IQueryable<ProjectEntity> response = projectService.GetAllProjects();
return Request.CreateResponse(HttpStatusCode.OK, response);
}
catch (Exception ex)
{
return Request.CreateResponse(HttpStatusCode.InternalServerError, ex.Message + " " + ex.StackTrace);
}
}
ProjectService.cs
public IQueryable<ProjectEntity> GetAllProjects()
{
CustomerSupportDBContext dBContext = new CustomerSupportDBContext();
var projectEntities = dBContext.ProjectEntities.Include("ApplicationEntity");
return projectEntities;
}
ProjectEntity.cs
[Table("ProjectEntity")]
public class ProjectEntity
{
[Key]
public int ProjectID { get; set; }
public string ProjectName { get; set; }
public string ProjectDescription { get; set; }
public string ProjectURL { get; set; }
public List<ApplicationEntity> ApplicationEntity { get; set; }
public int ApplicationID { get; set; }
public ProjectEntity()
{
ApplicationEntity = new List<ApplicationEntity>();
IssueEntity = new List<IssueEntity>();
}
}
ApplicationEntity.cs
[Table("ApplicationEntity")]
public class ApplicationEntity
{
[Key]
public int ApplicationID { get; set; }
public string ApplicationName { get; set; }
public string ApplicationVersion { get; set; }
public string ApplicationDescription { get; set; }
}
DbContext.cs
public class CustomerSupportDBContext : DbContext
{
public DbSet<ProjectEntity> ProjectEntities { get; set; }
public DbSet<ApplicationEntity> ApplicationEntities { get; set; }
public CustomerSupportDBContext()
{
Configuration.LazyLoadingEnabled = false;
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
}
}
只有最后一个实体有 ApplicationEntity 及其详细信息。
【问题讨论】:
标签: c# .net entity-framework asp.net-web-api