我使用的是 EF 6,代码优先,我有一个域模型类,它有 2 个 FK 和相关对象作为类成员。插入工作正常,但从数据库中获取值时,与该对象相关的 FK 对象为空。
我尝试将延迟初始化设置为 false 并将 FK 类成员标记为虚拟。我已经尝试过 Eagerly Loading,但仍然没有积极的结果(无论如何我都不想使用 Eagerly)。
public class AffectedLocation
{
public AffectedLocation()
{
Id = Guid.NewGuid();
}
[Key]
[Required]
public Guid Id { get; set; }
[Required]
public string Name { get; set; }
public string Description { get; set; }
[Required]
[ForeignKey("RadiationLevel")]
public Guid FK_RadiationLevel { get; set; }
public virtual RadiationLevel RadiationLevel { get; set; }
[Required]
[ForeignKey("GeographicalCoordinates")]
public Guid FK_GeographicalCoordinates { get; set; }
public virtual GeographicalCoordinates GeographicalCoordinates { get; set;
}
public class RadiationLevel
{
public RadiationLevel()
{
Id = Guid.NewGuid();
}
[Key]
public Guid Id { get; set; }
[Required]
public int Level { get; set; }
public string Description { get; set; }
}
public class GeographicalCoordinates
{
public GeographicalCoordinates()
{
Id = Guid.NewGuid();
}
[Key]
public Guid Id { get; set; }
[Required]
public float Latitude { get; set; }
[Required]
public float Longitude { get; set; }
[Required]
public float Range { get; set; }
}
public class Repository<TEntity> : IRepository<TEntity> where TEntity : class
{
protected readonly DbContext Context;
public Repository(DbContext context)
{
Context = context;
}
public IEnumerable<TEntity> GetAll()
{
return Context.Set<TEntity>().ToList();
}
}
public class LocationRepository : Repository<AffectedLocation>, ILocationRepository
{
public LocationRepository(RadioactiveAreaContext context)
: base(context)
{
}
}
public class UnitOfWork : IUnitOfWork
{
private readonly RadioactiveAreaContext _context;
public UnitOfWork()
{
_context = new RadioactiveAreaContext();
Location = new LocationRepository(_context);
}
public ILocationRepository Location { get; private set; }
public int Complete()
{
return _context.SaveChanges();
}
public void Dispose()
{
_context.Dispose();
}
}
public class RadioactiveAreaContext : DbContext
{
public RadioactiveAreaContext()
: base("name=RadioactiveAreaContext")
{
this.Configuration.LazyLoadingEnabled = false;
}
public virtual DbSet<AffectedLocation> Locations { get; set; }
public virtual DbSet<RadiationLevel> RadiationLevels { get; set; }
public virtual DbSet<GeographicalCoordinates> GeographicalCoordinates { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Configurations.Add(new LocationConfiguration());
modelBuilder.Configurations.Add(new RadiationLevelsConfiguration());
modelBuilder.Configurations.Add(new GeographicalCoordinatesConfiguration());
}
}
var unitOfWork = new UnitOfWork();
unitOfWork.Location.Add(someLocation);
unitOfWork.Complete();
var locations = unitOfWork.Location.GetAll(); // first location will be correctly loaded from the database, please see the first picture (the values are not null)
//Adding a new location and call again the GetAll(), will give 2 results now, but the second result is a EF dynamic proxy and doesn't have the FK class members loaded...
请看那些图片,第一次插入后获取数据是可以的,但是在第二次插入后,获取数据时会出现这种情况: