我有一个简单的TPH层次结构:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<context-param>
<param-name>
org.apache.tiles.impl.BasicTilesContainer.DEFINITIONS_CONFIG</param-name>
<param-value>/WEB-INF/tiles.xml</param-value>
</context-param>
<listener>
<listener-class>org.apache.struts2.tiles.StrutsTilesListener</listener-class>
</listener>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<error-page>
<exception-type>java.lang.Throwable</exception-type>
<location>/error.jsp</location>
</error-page>
<error-page>
<error-code>400</error-code>
<location>/error.jsp</location>
</error-page>
<error-page>
<error-code>401</error-code>
<location>/error.jsp</location>
</error-page>
<error-page>
<error-code>404</error-code>
<location>/error.jsp</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/error.jsp</location>
</error-page>
<error-page>
<error-code>501</error-code>
<location>/error.jsp</location>
</error-page>
并在DbContext中:
public abstract class UserAccount
{
public string FirstName { get;set; }
}
public class InterpreterAccount : UserAccount
{
public string PhoneNumber { get;set; }
}
此代码有效(将FirstName和PhoneNumber保存到DB):
//.......
public DbSet<UserAccount> UserAccounts { get; set; }
public DbSet<InterpreterAccount> InterpreterAccounts { get; set; }
这并不能保存PhoneNumber属性
var account = _context.InterpreterAccounts.Single(i => i.Id == id);
account.FirstName = "Ivan";
account.PhoneNumber = "+123";
_context.SaveChanges();
但在实际场景中,只有在检索到对象之后才知道对象的类型,因此需要查询基本集合。
所以问题是:如何通过查询EF7中的基础对象集合来保存对TPH层次结构的派生对象的更改?
答案 0 :(得分:1)
......并在接下来的10分钟内找到了解决方案。看起来仍然有点难看。
要保存对通过基础对象集检索的派生对象的更改,我必须1)在查询时分离对象,2)根据类型附加到集合
var account = _context.UserAccounts.AsNoTracking().Single(i => i.Id == id);
account.FirstName = "Ivan";
if (account is InterpreterAccount)
{
_context.InterpreterAccounts.Attach(account);
((InterpreterAccount)account).PhoneNumber = "+123";
}
_context.SaveChanges();
工作,但涉及条件逻辑,似乎有点矫枉过正。我希望看到没有它的工作。