我已经有一段时间了,我真的无法弄清楚。我正在使用 EF5、Mvc 4、table-per-type 继承。
我有继承自 UserProfile 的租户和房东类。注册用户时选择他们想要的帐户类型,并根据此选择将他们在表单上输入的详细信息(用户名、名字、姓氏、电子邮件、帐户类型、密码)添加到 UserProfile 表中。然后,我希望将该表的 PK 作为外键添加到与他们选择的帐户类型(租户或房东)匹配的表中。
我试图使用这种方法来实现上述目的,但这导致了该问题中描述的问题。
我现在创建了一个 RegisterTenantModel 类,其属性映射到租户,并且我将 RegisterTenantModel 类型的对象传递给 AccountController/Register,如下所示......
// ...
if (tenantModel.AccountType == AccountType.Tenant)
{
using (var db = new LetLordContext())
{
var t = db.UserProfile.Create<Tenant>();
WebSecurity.CreateUserAndAccount(tenantModel.UserName, tenantModel.Password,
t = new Tenant
{
AccountType = tenantModel.AccountType,
DateWantToRentFrom = DateTime.Now,
DateWantToRentTo = DateTime.Now,
DesiredPropertyLocation = "",
Email = tenantModel.Email,
FirstName = tenantModel.FirstName,
UserId = WebSecurity.CurrentUserId,
UserName = WebSecurity.CurrentUserName
// More properties that map to Tenant entity
// ...
},
false);
db.SaveChanges();
...但是现在我Invalid column name
在t= new Tenant
.
有没有人不得不做类似的事情?我以正确的方式接近这个吗?帮助将不胜感激。
编辑
根据 Antonio Simunovic 在下面发布的内容,我想我已经意识到问题所在了。我WebSecurity.InitializeDatabaseConnection()
的是这样的...
WebSecurity.InitializeDatabaseConnection("LetLordContext", "UserProfile",
"UserId", "UserName", autoCreateTables: true);
...所以当我调用它时Websecurity.CreatUserAndAccount()
,AccountController/Register
它会根据WebSecurity.InitializeDatabaseConnection()
. 查看上面链接的问题,您会看到,根据表单上选择的帐户类型,租户或房东将通过调用...添加到 UserProfile 表中。
var tenant = db.UserProfile.Create<Tenant>();
//...
db.UserProfile.Add(tenant);
db.SaveChanges();
...导致将重复条目添加到 UserProfile 表中。
我认为解决方案是创建一个新表并指向WebSecurity.InitializeDatabaseConnection()
它。