【问题标题】:Entity framework(Code first) - & ASP.net Membership Provider实体框架(代码优先) - & ASP.net Membership Provider
【发布时间】:2014-02-06 16:09:28
【问题描述】:

我目前正在尝试使用 EF6 Code First 构建一个新应用程序,我设法根据我的模型创建数据库没有问题。

现在我想做的是使用 asp.net 会员功能。所以我必须在我的数据库中有数据库模式。

我该怎么做?

互联网上有关于在 EF 中使用 simplemembershipprovider 的信息,但非常混乱。

只是为了让你知道我到目前为止所做的事情......(下)

那么,在 EF 中使用会员资格的最佳方式是什么?

namespace AsoRock.Data.DTOs
{
    public class Customer
    {
        [Key]
        public int CustomerId { get; set; }
        public string Title { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Email { get; set; }
        public string HomeNumber { get; set; }
        public string MobileNumber { get; set; }
        public string Gender { get; set; }
        public DateTime Dob { get; set; }
    }

    public class Order
    {
        [Key]
        public int OrderId { get; set; }
        public Address Address { get; set; }
        public Customer Customer { get; set; }
        public List<Product> Products { get; set; }
    }

    public class Product
    {
        [Key]
        public int ProductId { get; set; }
        public string ProductName { get; set; }
        public decimal ProductPrice { get; set; }
    }

    public class Address
    {
        [Key]
        public int AddressId { get; set; }
        public Customer Customer { get; set; }
        public string AddressLine1 { get; set; }
        public string AddressLine2 { get; set; }
        public string City { get; set; }
        public string PostCode { get; set; }
        public string LastUsed { get; set; }
        public bool isDefault { get; set; }
    }


    public class AsoRockContext : DbContext
    {
        public DbSet<Customer> Customers { get; set; }
        public DbSet<Address> Addresses { get; set; }
        public DbSet<Order> Orders { get; set; }
       // public DbSet<Product> Products { get; set; }


        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Order>().HasMany(p => p.Products).WithMany().Map(m =>
            {
                m.MapLeftKey("OrderId").MapRightKey("ProductId").ToTable("OrderdProducts");
            });

            base.OnModelCreating(modelBuilder);
        }

    }

}

【问题讨论】:

  • 哪个版本的asp.net mvc?

标签: c# asp.net entity-framework ef-code-first code-first


【解决方案1】:

如果您使用的是 MVC 5,那么您可以更改您的 公共类 AsoRockContext : DbContext

成为

public class AsoRockContext : IdentityDbContext<ApplicationUser>

然后创建你的 ApplicationUser

public class ApplicationUser : IdentityUser
{   //You can add extra properties in if you want
    public string FirstName { get; set; }
    public string LastName { get; set; }
    //Or add a link to your customer table
    public Customer CustomerDetails { get; set; }
}

【讨论】:

  • 你能给我更多的信息吗?我目前在我的解决方案中只有一个项目 (AsoRock.Data)
    这是我放置所有 EF 代码的地方。 (代码优先)所以如果我创建一个 MVC5 项目(AsoRock.Web),你是说我会在 AsoRock.Data 中调用 AsoRockContext
  • 你能给我更多的信息吗?我的解决方案中目前只有一个项目 (AsoRock.Data) 这是我放置所有 EF 代码的地方。 (代码优先)因此,如果我创建一个 MVC5 项目(AsoRock.Web),您是说我会在 AsoRock.Data 内部调用 AsoRockContext,这对更新模型时数据库的自动创建有何影响? @ASG
  • 您可以将您的上下文(和 DbSet)放入您的 MVC 项目中,然后添加对您的 AsoRock.Data 项目的引用
  • 当您说将 Context 和 Dbsets 放入 MVC 项目时,我假设您的意思是创建数据库的所有代码...对吗?那么,如果我这样做了,我就不需要 .data 项目了。?
  • 视项目规模而定。如果您要在其他地方重用您的数据类,请保留它。只要您的 MVC Poject 引用它,您就可以使用任何其他项目创建您的 Context 和 DbSets
【解决方案2】:

Asp.net MVC 4+ 带有 asp.net 会员提供程序。 所以你的上下文名称是TestConnection 然后

在模型目录的 AccountModel 中,将 defaultConnection 更改为 TestConnection 像这样,然后它将在您的数据库中创建会员表。

 public UsersContext()
            : base("TestConnection")
        {
        }

要了解更多关于它是如何实现的,请查看代码,查看 AccountController 文件并在顶部找到以下内容 [初始化SimpleMembership] 公共类 AccountController : 控制器

检查[InitializeSimpleMembership] 属性以了解它是如何实现的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-01
    • 2014-05-23
    相关资源
    最近更新 更多