我正在尝试创建一个(function () {
const items = document.querySelector("#items");
const trigger = document.querySelector("#trigger");
const options = { root: null, rootMargin: "0px", threshold: 0.0 };
const observer = new IntersectionObserver((entries) => {
entries.forEach((entry) => {
if (entry.intersectionRatio > 0) {
getMoreItems(15);
items.appendChild(trigger);
getMoreItems(5);
}
});
}, options);
observer.observe(trigger);
let itemcount = 0;
getMoreItems(15);
items.appendChild(trigger);
getMoreItems(5);
function getMoreItems(n) {
for (let i = itemcount; i < itemcount + n; i++) {
let item = document.createElement("p");
item.innerHTML = "this is item #" + i;
items.appendChild(item);
}
itemcount += n;
}
})();
作为子对象的ApplicationUser
,这就是模型的样子:
ApplicationUser:
User
用户:
public class ApplicationUser : IdentityUser
{
public User User { get; set; }
}
在我的DbContext中,我有:
public class User
{
public int Id { get; set; }
[ForeignKey("AspNetUser")]
public string AspNetUserId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public virtual ApplicationUser AspNetUser { get; set; }
}
AdminConfiguration:
public class IdentityDbContext : IdentityDbContext<ApplicationUser>
{
public IdentityDbContext(DbContextOptions<IdentityDbContext> options)
: base(options)
{
}
public DbSet<User> Users { get; set; }
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
builder.ApplyConfiguration(new AdminConfiguration());
}
}
使用此代码,我创建了一个迁移并尝试执行public class AdminConfiguration : IEntityTypeConfiguration<ApplicationUser>
{
public void Configure(EntityTypeBuilder<ApplicationUser> builder)
{
var id = "bc62cdff-77ca-4473-a467-210eb36fdd5d";
var admin = new ApplicationUser
{
Id = id,
UserName = "admin",
NormalizedUserName = "ADMIN",
Email = "admin@dotvvm.com",
NormalizedEmail = "ADMIN@DOTVVM.COM",
EmailConfirmed = true,
SecurityStamp = new Guid().ToString("D")
};
admin.PasswordHash = GeneratePassword(admin, "Admin12345!");
builder.HasData(admin);
builder.OwnsOne(a => a.User).HasData(new User
{
Id = 1,
AspNetUserId = id,
FirstName = "Test",
LastName = "Test"
});
}
private string GeneratePassword(ApplicationUser user, string password)
{
var passHash = new PasswordHasher<ApplicationUser>();
return passHash.HashPassword(user, password);
}
}
,但出现此错误:
Update-Database
我不知道自己在做什么错,有人知道吗?
答案 0 :(得分:1)
我几乎可以确定您使用的是.OwnsOne错误(但我怀疑这是根本原因,我稍后再谈) 拥有的类型是Value对象。值对象本身没有身份,仅像其所有者的一部分一样存在
//this is entity, it has identity
public class Person
{
public Guid Id { get; set; }
public Name Name { get; set; }
}
//and this is value object and could be owned type
public class Name
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
如果您希望ApplicationUser
和User
都是实体(有意义),则可以考虑在它们之间建立一对一的关系,像这样
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<ApplicationUser>()
.HasOne(a => a.User)
.WithOne(b => b.ApplicationUser)
.HasForeignKey<ApplicationUser>(b => b.AspNetUserId);
}
然后是您的
builder.HasData(new User
{
Id = 1,
AspNetUserId = id,
FirstName = "Test",
LastName = "Test"
});
可能有效,而...可能无效
因为您可能遇到的其他问题,可能是“自动增量ID”字段(在您的User
类中是自动增量吗?)
如果是-
builder.OwnsOne(a => a.User).HasData(new User
{
Id = 1, //<<---- try removing this
AspNetUserId = id,
FirstName = "Test",
LastName = "Test"
});
这可以解决您的问题