我的问题是Azure数据库连接运行正常,但是当我使用link https://docs.microsoft.com/en-us/azure/app-service/app-service-web-tutorial-dotnet-sqldatabase
中给出的示例Todo时所以我已经在数据库中创建了名为“ PhotoTable”的表,但是每次我运行示例(asp.net)时,它都会在我的数据库中创建它自己的表(Todoes表)(我已经创建了DB,并且它与我现有的数据库)所以问题是它没有与mt表连接,我已经完成了控制器和所有其他东西的更改,但是它没有用,如果有人知道如何与现有表连接,请让我知道,谢谢(如果需要更多信息,请让我知道,我已经添加了我的表格列表和需要连接的表格的屏幕截图,并用红色突出显示了)
答案 0 :(得分:0)
tutorial是有关如何与数据库中现有表进行连接的信息。
下面是Student
类,具有与表相同的属性。
public class Student
{
public int ID { get; set; }
public string LastName { get; set; }
public int Age { get; set; }
public string Address { get; set; }
}
类StudentMap
用于映射数据库中的现有表。
public class StudentMap : EntityTypeConfiguration<Student>
{
public int MapID { get; set; }
public string LastName { get; set; }
public int Age { get; set; }
public string Address { get; set; }
public StudentMap()
{
// Table & parimary key Mappings
this.ToTable("StudentMap");
this.HasKey(t => t.ID);
}
}
SchoolContext
是对数据库的引用。在这段代码中,我们使用上面声明的StudentMap
进行配置。
public class SchoolContext : DbContext
{
public SchoolContext() : base("SchoolContext")
{
}
public DbSet<Student> Students { get; set; }
public DbSet<StudentMap> StudentMaps { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
modelBuilder.Entity<StudentMap>().HasKey(x => new { x.MapID });
}
}
注意:在Student
和StudentMap
类中设置不同的键,以便schoolcontext
可以正确区分它们。