gpt4 book ai didi

c# - 在 asp.net mvc 6 Entity Framework 7 中获取 session 值 OnConfiguring

转载 作者:太空宇宙 更新时间:2023-11-03 12:50:20 25 4
gpt4 key购买 nike

我有 DbContext 类以便使用迁移和创建新数据库或为现有数据库创建表。

 public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
#region Tables

public DbSet<Author> Author { get; set; }

public DbSet<Books> Books { get; set; }

#endregion




protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
// Customize the ASP.NET Identity model and override the defaults if needed.
// For example, you can rename the ASP.NET Identity table names and more.
// Add your customizations after calling base.OnModelCreating(builder);
}

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer("");
base.OnConfiguring(optionsBuilder);
}

}

登录 Controller :

  using (var context = new ApplicationDbContext())
{
context.Database.Migrate(); // if this code works it gets connection string from appsettings automaticly.How can i change connection string ?
}

迁移类:

 [DbContext(typeof(ApplicationDbContext))]
partial class ApplicationDbContextModelSnapshot : ModelSnapshot
{
protected override void BuildModel(ModelBuilder modelBuilder)
{
modelBuilder
.HasAnnotation("ProductVersion", "7.0.0-rc1-16348")
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);

modelBuilder.Entity("Microsoft.AspNet.Identity.EntityFramework.IdentityRole", b =>
{
b.Property<string>("Id");

b.Property<string>("ConcurrencyStamp")
.IsConcurrencyToken();

b.Property<string>("Name")
.HasAnnotation("MaxLength", 256);

b.Property<string>("NormalizedName")
.HasAnnotation("MaxLength", 256);

b.HasKey("Id");

b.HasIndex("NormalizedName")
.HasAnnotation("Relational:Name", "RoleNameIndex");

b.HasAnnotation("Relational:TableName", "AspNetRoles");
});

modelBuilder.Entity("Microsoft.AspNet.Identity.EntityFramework.IdentityRoleClaim<string>", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd();

b.Property<string>("ClaimType");

b.Property<string>("ClaimValue");

b.Property<string>("RoleId")
.IsRequired();

b.HasKey("Id");

b.HasAnnotation("Relational:TableName", "AspNetRoleClaims");
});

modelBuilder.Entity("Microsoft.AspNet.Identity.EntityFramework.IdentityUserClaim<string>", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd();

b.Property<string>("ClaimType");

b.Property<string>("ClaimValue");

b.Property<string>("UserId")
.IsRequired();

b.HasKey("Id");

b.HasAnnotation("Relational:TableName", "AspNetUserClaims");
});

modelBuilder.Entity("Microsoft.AspNet.Identity.EntityFramework.IdentityUserLogin<string>", b =>
{
b.Property<string>("LoginProvider");

b.Property<string>("ProviderKey");

b.Property<string>("ProviderDisplayName");

b.Property<string>("UserId")
.IsRequired();

b.HasKey("LoginProvider", "ProviderKey");

b.HasAnnotation("Relational:TableName", "AspNetUserLogins");
});

modelBuilder.Entity("Microsoft.AspNet.Identity.EntityFramework.IdentityUserRole<string>", b =>
{
b.Property<string>("UserId");

b.Property<string>("RoleId");

b.HasKey("UserId", "RoleId");

b.HasAnnotation("Relational:TableName", "AspNetUserRoles");
});

modelBuilder.Entity("Risklator.Models.ApplicationUser", b =>
{
b.Property<string>("Id");

b.Property<int>("AccessFailedCount");

b.Property<string>("ConcurrencyStamp")
.IsConcurrencyToken();

b.Property<string>("Email")
.HasAnnotation("MaxLength", 256);

b.Property<bool>("EmailConfirmed");

b.Property<bool>("LockoutEnabled");

b.Property<DateTimeOffset?>("LockoutEnd");

b.Property<string>("NormalizedEmail")
.HasAnnotation("MaxLength", 256);

b.Property<string>("NormalizedUserName")
.HasAnnotation("MaxLength", 256);

b.Property<string>("PasswordHash");

b.Property<string>("PhoneNumber");

b.Property<bool>("PhoneNumberConfirmed");

b.Property<string>("SecurityStamp");

b.Property<bool>("TwoFactorEnabled");

b.Property<string>("UserName")
.HasAnnotation("MaxLength", 256);

b.HasKey("Id");

b.HasIndex("NormalizedEmail")
.HasAnnotation("Relational:Name", "EmailIndex");

b.HasIndex("NormalizedUserName")
.HasAnnotation("Relational:Name", "UserNameIndex");

b.HasAnnotation("Relational:TableName", "AspNetUsers");
});

modelBuilder.Entity("Risklator.Models.CompanyDBModel.Author", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd();

b.Property<int>("AuthorID");

b.Property<string>("FirstMidName");

b.Property<string>("LastName")
.IsRequired();

b.HasKey("Id");
});

modelBuilder.Entity("Risklator.Models.CompanyDBModel.Books", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd();

b.Property<int>("AuthorID");

b.Property<int>("BookID");

b.Property<string>("Genre");

b.Property<decimal>("Price");

b.Property<string>("Title")
.IsRequired();

b.Property<int>("Year");

b.HasKey("Id");
});

modelBuilder.Entity("Microsoft.AspNet.Identity.EntityFramework.IdentityRoleClaim<string>", b =>
{
b.HasOne("Microsoft.AspNet.Identity.EntityFramework.IdentityRole")
.WithMany()
.HasForeignKey("RoleId");
});

modelBuilder.Entity("Microsoft.AspNet.Identity.EntityFramework.IdentityUserClaim<string>", b =>
{
b.HasOne("Risklator.Models.ApplicationUser")
.WithMany()
.HasForeignKey("UserId");
});

modelBuilder.Entity("Microsoft.AspNet.Identity.EntityFramework.IdentityUserLogin<string>", b =>
{
b.HasOne("Risklator.Models.ApplicationUser")
.WithMany()
.HasForeignKey("UserId");
});

modelBuilder.Entity("Microsoft.AspNet.Identity.EntityFramework.IdentityUserRole<string>", b =>
{
b.HasOne("Microsoft.AspNet.Identity.EntityFramework.IdentityRole")
.WithMany()
.HasForeignKey("RoleId");

b.HasOne("Risklator.Models.ApplicationUser")
.WithMany()
.HasForeignKey("UserId");
});

modelBuilder.Entity("Risklator.Models.CompanyDBModel.Books", b =>
{
b.HasOne("Risklator.Models.CompanyDBModel.Author")
.WithMany()
.HasForeignKey("AuthorID");
});
}
}

问题:

是否可以将连接字符串设置为 optionsBuilder.UseSqlServer("");通过在 asp.net mvc 6 中使用 session ,如下所示?

optionsBuilder.UseSqlServer(HttpContext.Session.GetString("connectionString"))

谢谢

最佳答案

我不确定它是否有帮助,因为我将为 EF 6 编写解决方案。但是在 entityframework 6 中,您可以在创建上下文对象时将连接字符串名称或连接字符串传递给构造函数。

如果你正在使用依赖注入(inject),那么当你构造对象时最好在那里做它

Here is实际上示例如何使用 EF 7 执行此操作

您提到了迁移,我再次不确定 EF7,但在 EF6 中有 DBMigrator。

 DbMigrationsConfiguration configuration = new DbMigrationsConfiguration()
{
MigrationsAssembly = typeof(YOURASSEMBLY).Assembly,
ContextType = typeof(YOURASSEMBLY),
TargetDatabase = new System.Data.Entity.Infrastructure.DbConnectionInfo(__YOUR_CONNECTION_STRING__)
};

DbMigrator dbMigrator = new DbMigrator(configuration);
dbMigrator.Update();

因此您需要创建循环以在应用程序启动或控制台应用程序上更新所有数据库。取决于您的设置。

附言。在 EF7 中更容易 look here

关于c# - 在 asp.net mvc 6 Entity Framework 7 中获取 session 值 OnConfiguring,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35792499/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com