我有以下邀请:
public class Ticket
{
public int Id { get; set; }
public string RequestBy { get; set; }
public int PriorityId { get; set; }
public ApplicationUser Requester { get; set; }
public Priority Priority { get; set; }
}
public class ApplicationUser
{
public ApplicationUser()
{
Id = Guid.NewGuid().ToString();
}
public string FirstName { get; set; }
public string LastName { get; set; }
}
public class Priority
{
public int Id { get; set; }
public string Name { get; set; }
}
当我在控制器中按如下方式创建新故障单时:
public class BaseController : Controller
{
protected readonly IUnitOfWork UnitOfWork;
public BaseController(IUnitOfWork unitOfWork)
{
UnitOfWork = unitOfWork;
}
}
public class TicketController : BaseController
{
public TicketController(IUnitOfWork unitOfWork) : base(unitOfWork) { }
[HttpPost]
public ActionResult CreateTicket(CreateTicketVM viewModel)
{
// repopulate dropdowns incase we need to return view
// viewModel.Priorities is IEnumerable list
viewModel.Priorities = UnitOfWork.PriorityRepository.GetPriorities();
//validation code removed for brevity...
var ticket = new Ticket
{
RequestBy = !string.IsNullOrEmpty(viewModel.RequestBy) ? viewModel.RequestBy : User.Identity.GetUserId(),
PriorityId = viewModel.PriorityId != 0 ? viewModel.PriorityId : (int)PriorityLevel.Medium,
};
UnitOfWork.TicketRepository.Add(ticket);
}
}
现在,当我在调用var ticket
之后调试.Add(ticket)
时,为什么它加载了相关的导航属性Priority
而不是Requester
导航属性,尽管RequestBy
值被设定?如您所见,我没有针对任何导航属性定义任何virtual
个关键字?
Strangley只发生了ApplicationUser
类型的所有导航属性。即使不使用virtual
关键字???
使用流畅的api我已将RequestBy
FK定义如下:
modelBuilder.Entity<Ticket>()
.HasRequired(x => x.Requester)
.WithMany()
.HasForeignKey(x => x.RequestBy);
以下是提供更多背景信息的附加代码。
Ticket Repository:
public class TicketRepository : ITicketRepository
{
private readonly ApplicationDbContext _context;
public TicketRepository(ApplicationDbContext context)
{
_context = context;
}
public void Add(Ticket ticket)
{
_context.Ticket.Add(ticket);
}
}
优先级存储库:
public class PriorityRepository : IPriorityRepository
{
private readonly ApplicationDbContext _context;
public PriorityRepository(ApplicationDbContext context)
{
_context = context;
}
public IEnumerable<Priority> GetPriorities()
{
return _context.Priority.ToList();
}
}
工作单位:
public class UnitOfWork : IUnitOfWork
{
private readonly ApplicationDbContext _context;
public ITicketRepository TicketRepository { get; private set; }
public IPriorityRepository PriorityRepository { get; private set; }
public UnitOfWork(ApplicationDbContext context)
{
_context = context;
TicketRepository = new TicketRepository(_context);
PriorityRepository = new PriorityRepository(_context);
}
public void Complete()
{
_context.SaveChanges();
}
}
答案 0 :(得分:2)
不应加载导航属性。您所看到的很可能是所谓的导航属性修正功能的结果 - 如果实体已经被上下文跟踪,即使您没有特别请求,EF也会更新引用它的实体的导航属性。
您可以使用var _context = new YourDbContext();
本地变量代替_context
字段轻松验证。或类似的东西:
var ticket = new Ticket
{
RequestBy = !string.IsNullOrEmpty(viewModel.RequestBy) ? viewModel.RequestBy : User.Identity.GetUserId(),
PriorityId = viewModel.PriorityId != 0 ? viewModel.PriorityId : (int)PriorityLevel.Medium,
};
bool priorityLoaded = _context.Priority.Local.Any(e => e.Id == ticket.PriorityId);
bool userLoaded = _context.ApplicationUser.Local.Any(e => e.Id == ticket.RequestBy);
_context.Ticket.Add(ticket);
根据您的说明,priorityLoaded
应为true
userLoaded
- false
。