如何使用Identity与Entity Framework来与AspNetUser表建立一对一关系

时间:2018-09-25 18:35:19

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

在我的情况下,用户有一条船,一条船有几条音符。

我正在使用默认的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();
        }
      }

1 个答案:

答案 0 :(得分:0)

在此示例中,您会发现一对多关系。

用户结构:

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));
    }
}

DbContext:

    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 
}
相关问题