我已经使用流利的样式配置了这个外键: p>
modelBuilder.Entity< Publication>() .HasOptional(p => p.Template)。 WithMany() .Map(p => p.MapKey(MailoutTemplateID));我有一个MVC表单处理程序,其中包含一些如下所示的代码:
public void Handle(PublicationEditViewModel publicationEditViewModel) {发布出版物= Mapper.Map< PublicationEditViewModel,Publication>(publicationEditViewModel); publication.Template = _mailoutTemplateRepository.Get(publicationEditViewModel.Template.Id); if(publication.Id == 0) { _publicationRepository.Add(publication); } else { _publicationRepository.Update(publication); } _unitOfWork.Commit(); }在这种情况下,我们正在更新现有的出版物实体,重新走过另一条路。当_unitOfWork.Commit()触发时,UPDATE将发送到数据库,我可以在SQL Profiler和Intellitrace中看到它,但它不包括更新中的MailoutTemplateID。
获取实际更新模板的技巧是什么?
存储库代码:
public virtual void Update(TEntity entity) { _dataContext.Entry(entity).State = EntityState.Modified; } public virtual TEntity Get(int id) { return _dbSet.Find(id); }UnitOfWork代码:
public void Commit() { _dbContext.SaveChanges(); }解决方案
取决于您的存储库代码。 :)如果您在出版物被上下文跟踪时设置publication.Template,我希望它可以正常工作。当您断开连接,然后连接(在场景中,您具有导航属性,但没有显式的FK属性)我猜测上下文在SaveChanges被调用时没有足够的信息来解决细节。我会做一些实验。 1)做一个集成测试,在那里你查询一个pub并保持它连接到上下文,然后添加模板,然后保存。 2)在Publicaction类上粘贴一个MailOutTemplateId属性,看看它是否有效。不建议#2作为一个解决方案,就像一种疏于行为的方式。我试着做这个实验,但还有其他一些我需要做的工作。)
I'm using EF 4.1 Code First. I have an entity defined with a property like this:
public class Publication { // other stuff public virtual MailoutTemplate Template { get; set; } }I've configured this foreign key using fluent style like so:
modelBuilder.Entity<Publication>() .HasOptional(p => p.Template) .WithMany() .Map(p => p.MapKey("MailoutTemplateID"));I have an MVC form handler with some code in it that looks like this:
public void Handle(PublicationEditViewModel publicationEditViewModel) { Publication publication = Mapper.Map<PublicationEditViewModel, Publication>(publicationEditViewModel); publication.Template = _mailoutTemplateRepository.Get(publicationEditViewModel.Template.Id); if (publication.Id == 0) { _publicationRepository.Add(publication); } else { _publicationRepository.Update(publication); } _unitOfWork.Commit(); }In this case, we're updating an existing Publication entity, so we're going through the else path. When the _unitOfWork.Commit() fires, an UPDATE is sent to the database that I can see in SQL Profiler and Intellitrace, but it does NOT include the MailoutTemplateID in the update.
What's the trick to get it to actually update the Template?
Repository Code:
public virtual void Update(TEntity entity) { _dataContext.Entry(entity).State = EntityState.Modified; } public virtual TEntity Get(int id) { return _dbSet.Find(id); }UnitOfWork Code:
public void Commit() { _dbContext.SaveChanges(); }解决方案
depends on your repository code. :) If you were setting publication.Template while Publication was being tracked by the context, I would expect it to work. When you are disconnected and then attach (with the scenario that you have a navigation property but no explicit FK property) I'm guessing the context just doesn't have enough info to work out the details when SaveChanges is called. I'd do some experiments. 1) do an integration test where you query the pub and keep it attached to the context, then add the template, then save. 2) stick a MailOutTemplateId property on the Publicaction class and see if it works. Not suggesting #2 as a solution, just as a way of groking the behavior. I"m tempted to do this experiment, but got some other work I need to do. ;)
更多推荐
实体框架代码第一次更新不会更新外键
发布评论