Entity Framework Code First ve MVC 5 ile çalışıyorum. Uygulamamı Individual User Accounts Authentication ile oluşturduğumda , bir Hesap denetleyicisi ve bununla birlikte Bireysel Kullanıcı Hesapları kimlik doğrulamasının çalışması için gerekli tüm gerekli sınıflar ve kodlar verildi .
Zaten mevcut olan kodlar arasında şunlar vardı:
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext() : base("DXContext", throwIfV1Schema: false)
{
}
public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
}
Ama sonra devam ettim ve önce kodu kullanarak kendi bağlamımı oluşturdum, bu yüzden şimdi aşağıdakilere de sahibim:
public class DXContext : DbContext
{
public DXContext() : base("DXContext")
{
}
public DbSet<ApplicationUser> Users { get; set; }
public DbSet<IdentityRole> Roles { get; set; }
public DbSet<Artist> Artists { get; set; }
public DbSet<Paintings> Paintings { get; set; }
}
Son olarak, geliştirme sırasında üzerinde çalışmam için bazı veriler eklemek için aşağıdaki tohum yöntemine sahibim:
protected override void Seed(DXContext context)
{
try
{
if (!context.Roles.Any(r => r.Name == "Admin"))
{
var store = new RoleStore<IdentityRole>(context);
var manager = new RoleManager<IdentityRole>(store);
var role = new IdentityRole { Name = "Admin" };
manager.Create(role);
}
context.SaveChanges();
if (!context.Users.Any(u => u.UserName == "James"))
{
var store = new UserStore<ApplicationUser>(context);
var manager = new UserManager<ApplicationUser>(store);
var user = new ApplicationUser { UserName = "James" };
manager.Create(user, "ChangeAsap1@");
manager.AddToRole(user.Id, "Admin");
}
context.SaveChanges();
string userId = "";
userId = context.Users.FirstOrDefault().Id;
var artists = new List<Artist>
{
new Artist { FName = "Salvador", LName = "Dali", ImgURL = "http://i62.tinypic.com/ss8txxn.jpg", UrlFriendly = "salvador-dali", Verified = true, ApplicationUserId = userId },
};
artists.ForEach(a => context.Artists.Add(a));
context.SaveChanges();
var paintings = new List<Painting>
{
new Painting { Title = "The Persistence of Memory", ImgUrl = "http://i62.tinypic.com/xx8tssn.jpg", ArtistId = 1, Verified = true, ApplicationUserId = userId }
};
paintings.ForEach(p => context.Paintings.Add(p));
context.SaveChanges();
}
catch (DbEntityValidationException ex)
{
foreach (var validationErrors in ex.EntityValidationErrors)
{
foreach (var validationError in validationErrors.ValidationErrors)
{
Trace.TraceInformation("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage);
}
}
}
}
Çözümüm iyi inşa ediliyor, ancak veritabanına erişim gerektiren bir denetleyiciye erişmeyi denediğimde aşağıdaki hatayı alıyorum:
DX.DOMAIN.Context.IdentityUserLogin:: EntityType 'IdentityUserLogin' tanımlı anahtar içermiyor. Bu EntityType için anahtarı tanımlayın.
DX.DOMAIN.Context.IdentityUserRole:: EntityType 'IdentityUserRole' tanımlı anahtar içermiyor. Bu EntityType için anahtarı tanımlayın.
Neyi yanlış yapıyorum? İki bağlamım olduğu için mi?
GÜNCELLEME
Augusto'nun cevabını okuduktan sonra Seçenek 3'e geçtim . DXContext sınıfım şu anda şöyle görünüyor:
public class DXContext : DbContext
{
public DXContext() : base("DXContext")
{
// remove default initializer
Database.SetInitializer<DXContext>(null);
Configuration.LazyLoadingEnabled = false;
Configuration.ProxyCreationEnabled = false;
}
public DbSet<User> Users { get; set; }
public DbSet<Role> Roles { get; set; }
public DbSet<Artist> Artists { get; set; }
public DbSet<Painting> Paintings { get; set; }
public static DXContext Create()
{
return new DXContext();
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<User>().ToTable("Users");
modelBuilder.Entity<Role>().ToTable("Roles");
}
public DbQuery<T> Query<T>() where T : class
{
return Set<T>().AsNoTracking();
}
}
Ayrıca bir User.cs
ve bir Role.cs
sınıf ekledim , şöyle görünüyorlar:
public class User
{
public int Id { get; set; }
public string FName { get; set; }
public string LName { get; set; }
}
public class Role
{
public int Id { set; get; }
public string Name { set; get; }
}
Varsayılan ApplicationUser'da bu ve bir sürü başka alan olduğundan, kullanıcı için bir şifre özelliğine ihtiyacım olup olmayacağından emin değildim!
Her neyse, yukarıdaki değişiklik iyi yapılıyor, ancak uygulama çalıştırıldığında yine bu hatayı alıyorum:
Geçersiz Sütun adı UserId
UserId
benim tam sayı özelliğidir Artist.cs