通过表每个类型继承MVC中4添加对象数据库 [英] Adding object to database using table-per-type inheritance in Mvc 4

查看:82
本文介绍了通过表每个类型继承MVC中4添加对象数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类楼主用户配置继承使用表每个类型继承。

I have a class Landlord that inherits from UserProfile using table-per-type inheritance.

当新用户注册上的应用程序,他们进入的一些标准,并选择他们想要的帐户类型,无论是楼主租户

When a new user registers on the application, they enter some criteria and select the type of account they want, either Landlord or Tenant.

下面是我的AccountController /注册方式:

Here's my AccountController/Register method:

public ActionResult Register(RegisterModel model)
    {
        if (ModelState.IsValid)
        {
            // Attempt to register the user
            try
            {
                WebSecurity.CreateUserAndAccount(model.UserName, model.Password,
                                            new
                                            {
                                                Email = model.Email,
                                                FirstName = model.FirstName,
                                                LastName = model.LastName,
                                                AccountType = model.AccountType.ToString()
                                            },
                                            false);

                // Add user to role that corresponds with selected account type
                if (model.AccountType == AccountType.Tenant)
                {
                    try
                    {
                        Roles.AddUserToRole(model.UserName, "Tenant");

                        using (var db = new LetLordContext())
                        {
                            var tenant = db.UserProfile.Create<Tenant>();

                            tenant.TenantAge = null;
                            tenant.TenantGroupMembers = null;
                            tenant.UserId = WebSecurity.CurrentUserId;
                            tenant.UserName = model.UserName;
                            // more properties associated with tenant
                            // ...

                            db.UserProfile.Add(tenant);
                            db.SaveChanges();
                        }

                    }
                    catch (ArgumentException e)
                    {
                        ModelState.AddModelError("Unable to add user to role", e);
                    }
                }
                if (model.AccountType == AccountType.Landlord)
                {
                    try
                    {
                        Roles.AddUserToRole(model.UserName, "Landlord");

                        using (var db = new LetLordContext())
                        {
                            var landlord = db.UserProfile.Create<Landlord>();

                            // same idea as adding a tenant
                        }
                    }
                    catch (ArgumentException e)
                    {
                        ModelState.AddModelError("Unable to add user to role", e);
                    }
                }

                return RedirectToAction("Confirm", "Home");
            }
            catch (MembershipCreateUserException e)
            {
                ModelState.AddModelError("", ErrorCodeToString(e.StatusCode));
            }
        }

        // If we got this far, something failed, redisplay form
        return View(model);
    }

作为一个例子,如果我选择了租户为所需的帐户类型注册时, WebSecurity.CreateUserAndAccount 将增加用户到用户配置表中,正确的,有说1的 UserProfileId

As an example, if I selected Tenant as the desired account type when registering, WebSecurity.CreateUserAndAccount would add a user into the UserProfile table, correctly, with say a UserProfileId of 1.

然后,如果(model.AccountType == AccountType.Tenant)会看到所选帐户类型为租户,将用户添加到该角色,以 UserProfileId 和角色ID code> if语句,因为选择的角色是租户,我创建了一个新的租户像这样: VAR租户= db.UserProfile.Create&LT;承租人&GT;(); ,并保存到数据库中(使用正确的UserProfileID作为PK)

Then, if (model.AccountType == AccountType.Tenant) would see that the selected account type is Tenant, add the user to that role, with a UserProfileId of 1 and a RoleId of 1. Within this if-statement, because selected role is Tenant, I create a new Tenant like so: var tenant = db.UserProfile.Create<Tenant>(); and it is saved to the database (with correct UserProfileID as the PK).

问题:两个用户配置实体(两行)被添加到用户配置表中的每一我尝试时间注册一个用户。据我所知,这可能是由于这样的事实,我打电话 WebSecurity.CreateUserAndAccount ,我创建一个新的租户对象。

The problem: Two UserProfile entities (two rows) are being added to the UserProfile table each time I try to register ONE user. I understand that this is probably due to the fact that I am calling WebSecurity.CreateUserAndAccount AND I'm creating a new Tenant object.

我如何避免这种情况?

我如何加入 WebSecurity.CreateUserAndAccount 使用到用户配置模型表和租户表一次?

How do I add the model being used in WebSecurity.CreateUserAndAccount into UserProfile table and Tenant table ONCE?

推荐答案

在这种情况下,你就不会同时创建用户配置和租客或房东。一旦实体被创建实体类型不能改变(即使将其扩展到一个子类型)。所以你的情况,你只需要跳过创建用户配置的步骤,而直接创建和保存无论租客或继承其业主实体。

In this instance you would not create both a UserProfile and a Tenant or Landlord. Once the Entity is created the entity type cannot be changed (even to extend it to a subtype). So in your case, you only need to skip the step of creating the UserProfile and just create and save either the Tenant or the Landlord entity that inherits it.

更多信息从我回答这个问题的选项1链接:<一个href=\"http://stackoverflow.com/questions/14488148/$c$c-first-tpt-inheritance-how-can-i-supply-the-foreign-key-instead-of-ef-crea/14489063#14489063\">$c$c首先TPT继承 - 我如何可以提供外键,而不是创造EF父行对我来说

More info linked from Option 1 of my answer to this question: Code First TPT Inheritance - How can I supply the foreign key instead of EF creating the parent row for me?

这篇关于通过表每个类型继承MVC中4添加对象数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆