1

私は、異なる特定のプロパティを持つ 3 種類のユーザー (顧客、サービス プロバイダー、および管理者) を持つ MVC プロジェクトに取り組み始めました。Visual Studio 2012 で、ASP.NET MVC Web アプリケーション インターネット アプリケーション テンプレートの既定の SimpleMembership 実装を拡張したいと考えています。

私は Customer クラスを持っています (Key 属性と関係についてはわかりません)

public class Customer
{
    [Key]
    public string UserName { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Phone { get; set; }

    public virtual UserProfile UserProfile { get; set; }
}

SQL Server Express に Customer テーブルを作成します。

CREATE TABLE [dbo].[Customer]
(
    [UserName] NVARCHAR(MAX) NOT NULL, 
    [FirstName] NVARCHAR(50) NOT NULL, 
    [LastName] NVARCHAR(50) NOT NULL, 
    [Phone] NVARCHAR(50) NULL
)

エンティティ フレームワークのコンテキスト:

public class UsersContext : DbContext
{
    public UsersContext() : base("DefaultConnection")
    {
    }

    public DbSet<UserProfile> UserProfiles { get; set; }

    public DbSet<Customer> Customers { get; set; }
}

UserProfile テーブルと共に Customer テーブルを作成するには、以下にどのコードを追加する必要がありますか?

// Attempt to register the user
try
{
     WebSecurity.CreateUserAndAccount(model.UserName, model.Password);

     WebSecurity.Login(model.UserName, model.Password);
     return RedirectToAction("Index", "Home");
 }

前もって感謝します

4

1 に答える 1

0

Customer クラスから次の行を削除しました。

public virtual UserProfile UserProfile { get; set; }

顧客クラスは次のようになります。

public class Customer
{
    [Key]
    public string UserName { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Phone { get; set; }
}

次のコードを AccountController の Register メソッドに追加しました。

try
{
     WebSecurity.CreateUserAndAccount(model.UserName, model.Password);

     Customer cust = new Customer
     {
         UserName = model.UserName,
         FirstName = "Dikembe",
         LastName = "Mutombo",
         Phone = model.Phone
     };                  
     UsersContext context = new UsersContext();
     context.Customer.Add(cust);
     context.SaveChanges();

     WebSecurity.Login(model.UserName, model.Password);
     return RedirectToAction("Index", "Home");
}
于 2013-03-13T11:10:05.950 に答える