【问题标题】:How to use Identity with Entity Framework to make a relationship one to one with the AspNetUser tables如何使用 Identity 和 Entity Framework 与 AspNetUser 表建立一对一的关系
【发布时间】:2018-09-25 18:35:18
【问题描述】:

在我的场景中,用户有一条船,而一条船有几个注释。

我正在使用默认的 Identity Mvc 项目模板,但我从未使用 Identity 与其他表建立任何关系。我只使用身份登录,没有任何关系

谁有任何关于如何将 aspNetUsers 表关联到其他表的示例?

船级

 public class Boat: Entity
    {

        public string Nome { get; private set; }
        public bool Ativo { get; private set; }    
        public bool Excluido { get; private set; }    
        public int SapId { get; private set; }    
        public int CapacidadeAgua { get; private set; }    
        public int CapacidadeOleo { get; private set; }    
        public int Velocidade { get; private set; }    
        public decimal AreaReal { get; private set; }    
        public decimal AreaProgramada { get; private set; }    
        public decimal AreaLivre { get; private set; }    
        public string Email { get; private set; }    
        public string Setor { get; private set; }   
        public DateTime DataCadastro { get; private set; }        
        public Guid? ClasseBarcoId { get; set; }

        public virtual ClasseBarco ClasseBarco { get; set; }

        public Barco(
            String nome, 
            bool ativo, 
            bool excluido, 
            int sapid, 
            int capacidadeAgua,
            int capacidadeOleo,
            int velocidade,
            string email,
            string setor,               
            DateTime dataCadastro)
        {
            Nome = nome;
            Ativo = ativo;
            Excluido = excluido;
            SapId = sapid;
            CapacidadeAgua = capacidadeAgua;
            CapacidadeOleo = capacidadeOleo;
            Velocidade = velocidade;
            Email = email;
            Setor = setor;
            DataCadastro = dataCadastro;

            ClasseBarco = new ClasseBarco();
        }
      }

【问题讨论】:

  • 尝试从 IdentityUser 继承并添加 Icollection 船,然后在 Context 配置中使用继承的类而不是 IdentityUser

标签: c# entity-framework asp.net-mvc-5 asp.net-identity


【解决方案1】:

这是一个例子,你会发现一对多的关系。

用户结构:

public class ApplicationUser : IdentityUser
{
    public ApplicationUser()
    {

        this.UserProducts = new HashSet<UserProduct>();

    }
    [Required]
    public string CompanyName { get; set; }
    [Required]
    public string ContactPerson { get; set; }
    [Required]
    public string ContactNumber { get; set; }
    [Required]
    public string PermanentAddress { get; set; }
    [Required]
    public string CorrespondenceAddress { get; set; }
    [Required]
    public string TIN { get; set; }
    public string PAN { get; set; }
    public string ServiceTaxNumber { get; set; }
    public string Category { get; set; }
    public virtual ICollection<UserProduct> UserProducts { get; set; }

    public ClaimsIdentity GenerateUserIdentity(ApplicationUserManager manager)
    {
        // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
        var userIdentity = manager.CreateIdentity(this, DefaultAuthenticationTypes.ApplicationCookie);
        // Add custom user claims here
        return userIdentity;
    }

    public Task<ClaimsIdentity> GenerateUserIdentityAsync(ApplicationUserManager manager)
    {
        return Task.FromResult(GenerateUserIdentity(manager));
    }
}

数据库上下文:

    public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
    {
        public ApplicationDbContext()
            : base("DefaultConnection", throwIfV1Schema: false)
        {
        }

        public static ApplicationDbContext Create()
        {
            return new ApplicationDbContext();
        }

        public virtual DbSet<Category> Categories { get; set; }
        public virtual DbSet<Product> Products { get; set; }
        public virtual DbSet<Unit> Units { get; set; }
        public virtual DbSet<UserProduct> UserProducts { get; set; }
    }

实体框架中的一对一关系参考这里:
Configure One-to-Zero-or-One Relationship in Entity Framework 6

见下例:
我们将在以下StudentStudentAddress 实体之间实现一对零或一的关系

public class Student
{
    public int StudentId { get; set; }
    public string StudentName { get; set; }

    public virtual StudentAddress Address { get; set; } //Can have only one address
}

public class StudentAddress 
{
    public int StudentAddressId { get; set; }
    public string Address1 { get; set; }
    public string Address2 { get; set; }
    public string City { get; set; }
    public int Zipcode { get; set; }
    public string State { get; set; }
    public string Country { get; set; }

    public virtual Student Student { get; set; }  //Single Student 
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-01-26
    • 2021-09-25
    • 1970-01-01
    • 2017-02-17
    • 1970-01-01
    • 2018-03-08
    • 1970-01-01
    相关资源
    最近更新 更多