gpt4 book ai didi

c# - Entity Framework table-per-type inheritance - 将继承类型添加到数据库给出错误 "Invalid column name"

转载 作者:搜寻专家 更新时间:2023-10-30 20:19:16 25 4
gpt4 key购买 nike

我已经研究了一段时间了,但我真的想不通。我正在使用 EF5、Mvc 4、每个类型的表继承。

我有继承自 UserProfile 的 Tenant 和 Landlord 类。注册用户时选择他们想要的帐户类型,并根据此选择将他们在表单中输入的详细信息(用户名、名字、姓氏、电子邮件、帐户类型、密码)添加到 UserProfile 表中。然后,我希望将该表的 PK 作为外键添加到与他们选择的帐户类型(租户或房东)相匹配的表中。

我正在努力实现上述 using this method ,但这导致了该问题中描述的问题。

我现在已经创建了一个 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();

...但现在对于 t= new Tenant 中的每个列名,我都收到错误消息 Invalid column name

有没有人做过类似的事情?我以正确的方式接近这个吗?非常感谢您的帮助。

编辑

根据 Antonio Simunovic 在下面发布的内容,我想我已经意识到问题所在了。我的 WebSecurity.InitializeDatabaseConnection() 是这样的...

WebSecurity.InitializeDatabaseConnection("LetLordContext", "UserProfile", 
"UserId", "UserName", autoCreateTables: true);

...因此,当我在 AccountController/Register 中调用 Websecurity.CreatUserAndAccount() 时,它会根据 WebSecurity.InitializeDatabaseConnection 中的参数写入 UserProfile 表()。查看上面链接的问题,您会看到,根据在表单上选择的帐户类型,租户或房东将通过调用...添加到 UserProfile 表中...

var tenant = db.UserProfile.Create<Tenant>();
//...
db.UserProfile.Add(tenant);
db.SaveChanges();

...导致将重复的条目添加到 UserProfile 表中。

我认为解决方案是创建一个新表,并将 WebSecurity.InitializeDatabaseConnection() 指向它。

最佳答案

您的 WebSecurity.InitializeDatabaseConnection() 方法调用是什么样的?该调用标识用于存储用户数据的数据库表。

WebSecurity.CreateUserAndAccount() 方法不使用上下文来存储提供的数据,它只是将第三个调用参数的对象属性名称映射到 InitializeDatabaseFile() 方法中定义的表中的列。

如果您不熟悉 SimpleMembership 机制,请查看这篇文章: http://weblogs.asp.net/jgalloway/archive/2012/08/29/simplemembership-membership-providers-universal-providers-and-the-new-asp-net-4-5-web-forms-and-asp-net-mvc-4-templates.aspx

关于c# - Entity Framework table-per-type inheritance - 将继承类型添加到数据库给出错误 "Invalid column name",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14609189/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com