[英]Can't Connect to the table azure SQL in given DB connection using .net
我的問題是Azure DB連接工作正常,但是當我使用鏈接 https://docs.microsoft.com/en-us/azure/app-service/app-service-web-tutorial-中給出的示例Todo時dotnet-sql數據庫
因此我已經在數據庫中創建了名為“ PhotoTable”的表,但是每次我運行示例(asp.net)時,它都會在我的數據庫中創建它自己的表(Todoes表)(已經創建了數據庫,並且它與現有數據庫的連接良好)所以問題是它沒有與mt table連接,我已經完成了控制器和所有其他東西的更改,但是它沒有用,如果有人知道如何與現有表連接,請告訴我,謝謝(如果有更多信息,需要,請讓我知道,我已經添加了我的表列表和需要連接的表的屏幕截圖,並用紅色突出顯示了)
這是有關如何與數據庫中現有表連接的教程 。
下面是具有與表相同的屬性的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
能夠正確區分它們。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.