【发布时间】:2016-12-29 14:51:27
【问题描述】:
我已从 Entity Framework 6 迁移到 EF Core,也将 Web Api .net 框架迁移到 .net core。
我已经建立了多对多的关系,如下所示
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
var instrumentsToPlaces = modelBuilder.Entity<InstrumentPlace>();
instrumentsToPlaces.ToTable("InstrumentsToPlaces");
instrumentsToPlaces.HasKey(x => new { x.PlaceId, x.InstrumentId });
instrumentsToPlaces.HasOne(i => i.Instrument)
.WithMany(p => p.InstrumentsPlaces)
.HasForeignKey(ip => ip.InstrumentId);
instrumentsToPlaces.HasOne(p => p.Place)
.WithMany(i => i.InstrumentsPlaces)
.HasForeignKey(ip => ip.PlaceId);
var instrumentsToStyle = modelBuilder.Entity<InstrumentStyle>();
instrumentsToStyle.ToTable("InstrumentsToStyles");
instrumentsToStyle.HasKey(x => new { x.StyleId, x.InstrumentId });
instrumentsToStyle.HasOne(i => i.Instrument)
.WithMany(s => s.InstrumentStyles)
.HasForeignKey(si => si.InstrumentId);
instrumentsToStyle.HasOne(s => s.Style)
.WithMany(i => i.InstrumentStyles)
.HasForeignKey(si => si.StyleId);
}
我已在存储库方法中包含导航属性,如下所示
public Instrument GetInstrumentByName(string name)
{
using (var starsAndCatzDbContext = new StarsAndCatzDbContext())
{
var instrument = _starsAndCatzDbContext.Instruments
.Include(a=>a.InstrumentsPlaces)
.ThenInclude(a=>a.Place)
.Include(a=>a.InstrumentStyles)
.ThenInclude(a=>a.Style)
.FirstOrDefault(i => i.Name == name);
return instrument;
}
}
这里是类
public class Instrument {
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<InstrumentPlace> InstrumentsPlaces { get; set; }
public virtual ICollection<InstrumentStyle> InstrumentStyles { get; set; }
}
public class InstrumentPlace
{
public int InstrumentId { get; set; }
public Instrument Instrument { get; set; }
public int PlaceId { get; set; }
public Place Place { get; set; }
}
public class InstrumentStyle
{
public int InstrumentId { get; set; }
public Instrument Instrument { get; set; }
public int StyleId { get; set; }
public Style Style { get; set; }
}
public class Style {
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<InstrumentStyle> InstrumentStyles { get; set; }
}
public class Place {
public int Id { get; set; }
public string Name { get; set; }
public string Division { get; set; }
public int Tier { get; set; }
public string State { get; set; }
public string Postcode { get; set; }
public float? Latitude { get; set; }
public float? Longitude { get; set; }
public virtual ICollection<InstrumentPlace> InstrumentsPlaces { get; set; }
}
要调用的WebAPI方法是
[HttpGet("GetInstrumentByName/{suburb}/{instrument}"), Produces("application/json")]
public Instrument GetInstrumentByName(string suburb, string instrument)
{
try
{
var result = _instrumentRepository.GetInstrumentByName(instrument);
return result;
}
catch (Exception ex)
{
_logger.LogError(ex.Message);
return new Instrument();
}
}
当我将请求发送到“/api/instruments/west-end/guitar”时,当我在发送响应之前放置断点时,我会得到预期的结果,如下所示
如您所见,导航属性已加载(当我展开集合时,我可以看到所有属性也正在加载)。 但是我收到的json响应如下
有什么建议还是我在这里遗漏了什么?
谢谢大家
【问题讨论】:
-
你能发布仪器类定义吗?
-
'public class Instrument { public int Id { get;放; } 公共字符串名称 { 获取;放; } 公共列表
InstrumentsPlaces { 获取;放; } 公共列表 InstrumentStyles { 获取;放; } }' 我之前将集合作为虚拟 ICollection.. 但更改了它以查看是否是问题但没有发生任何事情 -
我有一个类似的功能,我使用 public virtual Collection
OrderDetails { get;放;我得到了带有导航属性的实体,我认为你需要检查你的序列化程序设置,你检查了吗? -
我没有对项目进行任何修改,只是默认设置。您能否指出正确的序列化程序设置的正确方向?我还应该提到 EFcore 位于单独的库/项目中,WebAPI 也是如此。我采用了这种方法,因为我还需要参考其他项目的模型
-
在这种情况下,请尝试使用 public virtual Collection
Name { get;放;并让我知道这是否可以解决您的问题。我在您的代码中看到的另一件事是您使用标量类型而不是可为空的类型(例如 int?)
标签: entity-framework-core asp.net-core-webapi