我现在有以下代码,使用EF 6:
public class ApplicationUser : IdentityUser
{
public virtual ICollection<Client> Clients { get; set; }
}
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("DefaultConnection")
{
}
}
自定义:
public class MembershipManagerDbContext : DefaultDbContext
{
public MembershipManagerDbContext()
{
}
// db entities
public DbSet<Student> Students { get; set; }
public DbSet<Member> Members { get; set; }
public DbSet<Payment> Payments { get; set; }
public DbSet<PaymentType> PaymentTypes { get; set; }
public DbSet<Address> Addresses { get; set; }
public DbSet<Client> Client { get; set; }
public DbSet<ClientPayment> ClientPayment { get; set; }
//public DbSet<ClientUser> ClientUser { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
// moved migrations to Migrations folder
modelBuilder.Configurations.Add(new StudentConfiguration());
modelBuilder.Configurations.Add(new MemberConfiguration());
modelBuilder.Configurations.Add(new AddressConfiguration());
modelBuilder.Configurations.Add(new PaymentConfiguration());
modelBuilder.Configurations.Add(new PaymentTypeConfiguration());
modelBuilder.Configurations.Add(new ClientConfiguration());
modelBuilder.Configurations.Add(new ClientPaymentConfiguration());
}
}
Client.cs
public class Client: DbEntityWithId
{
public string Name { get; set; }
public string Email { get; set; }
public string Website { get; set; }
public string Telephone { get; set; }
public string Mobile { get; set; }
public DateTime LicenseStart { get; set; }
public DateTime LicenseEnd { get; set; }
public bool IsSuspended { get; set; } = false;
public int MemberLicense { get; set; }
public string AdditionalInfo { get; set; }
public int AddressId { get; set; }
public virtual Address Address { get; set; }
public virtual ICollection<ClientPayment> ClientPayments { get; set; }
public virtual ICollection<ApplicationUser> Users { get; set; }
}
FluentApi映射:
public class ClientConfiguration : EntityTypeConfiguration<Client>
{
public ClientConfiguration()
{
// create FK
HasRequired<Address>(s => s.Address)
.WithMany(s => s.Clients);
HasMany<ApplicationUser>(s => s.Users)
.WithMany(c => c.Clients)
.Map(cs =>
{
cs.MapLeftKey("ClientId");
cs.MapRightKey("UserId");
cs.ToTable("ClientUser");
});
}
}
在Package Manager中执行update-database命令时,出现以下错误:
MembershipManager.EntityFramework.Context.IdentityUserLogin :: EntityType'IdentityUserLogin'没有定义键。定义密钥 这个EntityType。 MembershipManager.EntityFramework.Context.IdentityUserRole :: EntityType'IdentityUserRole'没有定义键。定义密钥 这个EntityType。 IdentityUserLogins:EntityType:EntitySet 'IdentityUserLogins'基于没有的类型'IdentityUserLogin' 键定义。 IdentityUserRoles:EntityType:EntitySet 'IdentityUserRoles'基于没有的'IdentityUserRole'类型 键定义。
答案 0 :(得分:0)
您可能希望了解自定义IdentityDbContext的基础知识:https://codereview.stackexchange.com/questions/85741/identity-repository-pattern-with-unit-of-work/102087#102087
我认为这个主题相当困难,特别是如果您尝试在一个解决方案中使用多个项目。但使用IdentityDbContext会对你有所帮助!