内容目录
In a .NET Core project (library), using Entity Framework, the database generation fails when establishing the model.
Could not load assembly 'xxx'. Ensure it is referenced by the startup project 'xxx'.
Switching to 64-bit can resolve the issue.
Assuming ef code
public class ApplicationDbContext : IdentityDbContext<User>{
</span><span style="color: #0000ff;">public</span><span style="color: #000000;"> ApplicationDbContext(DbContextOptions options) : </span><span style="color: #0000ff;">base</span><span style="color: #000000;">(options) { } </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">virtual</span> DbSet<User> Users { <span style="color: #0000ff;">get</span>; <span style="color: #0000ff;">set</span><span style="color: #000000;">; } </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">virtual</span> DbSet<Role> Roles { <span style="color: #0000ff;">get</span>; <span style="color: #0000ff;">set</span><span style="color: #000000;">; } </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">virtual</span> DbSet<Group> Groups { <span style="color: #0000ff;">get</span>; <span style="color: #0000ff;">set</span><span style="color: #000000;">; } </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">virtual</span> DbSet<GroupRole> GroupRoles { <span style="color: #0000ff;">get</span>; <span style="color: #0000ff;">set</span><span style="color: #000000;">; } </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">virtual</span> DbSet<Log> Logs { <span style="color: #0000ff;">get</span>; <span style="color: #0000ff;">set</span><span style="color: #000000;">; } </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">virtual</span> DbSet<LogDetail> LogDetails { <span style="color: #0000ff;">get</span>; <span style="color: #0000ff;">set</span><span style="color: #000000;">; } </span><span style="color: #0000ff;">protected</span> <span style="color: #0000ff;">override</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> OnConfiguring(DbContextOptionsBuilder optionsBuilder) </span>=><span style="color: #000000;"> optionsBuilder.UseSqlServer( </span><span style="color: #800000;">@"</span><span style="color: #800000;">Data Source=.;Initial Catalog=DotNetCore;Persist Security Info=True;User ID=sa;Password=25423456;</span><span style="color: #800000;">"</span><span style="color: #000000;">); </span><span style="color: #0000ff;">protected</span> <span style="color: #0000ff;">override</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity</span><User><span style="color: #000000;">() .HasOne(d </span>=><span style="color: #000000;"> d.Group) .WithMany(t </span>=><span style="color: #000000;"> t.Users) .HasForeignKey(d </span>=><span style="color: #000000;"> d.GroupId) .OnDelete(DeleteBehavior.Cascade); }
}
To switch to 64-bit, do not change it on VS; instead, modify the csproj file in the project directory.
ps
With the model created, you can use migrations to create the database.
Run dotnet ef migrations add InitialCreate to scaffold the migration and create an initial set of tables for the model.
Run dotnet ef database update to apply the new migration to the database. This command will create the database before applying the migration.
文章评论