如何将标识与实体框架结合使用,以便与 AspNetUser 表建立一对一的关系



在我的场景中,用户有一艘船,一艘船有几个音符。

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

有人有任何如何将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();
}
}

这里有一个例子,你会发现一对多关系船。

用户结构:

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

有关实体框架中的一对一关系,请参阅此内容:
在实体框架 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 
}

相关内容

  • 没有找到相关文章

最新更新