My question is the Azure DB connection is working perfectly but when i use the sample Todo by given in the link https://docs.microsoft.com/en-us/azure/app-service/app-service-web-tutorial-dotnet-sqldatabase
so i have already table in the database called "PhotoTable" but every time i run the sample (asp.net)it creates it own table ( Todoes table)in my DB ( DB is already i created and its connect fine with my existing DB) so the issue is its not connecting with mt table , i have done changing the controller and all other stuff , but it dosen't work, if anyone knows how to connect with existing table please kindly let me know , thanks ( if more information needed please let me know, i have added the screenshot of my table list and the table i need to connect i have highlighted with red)
Here is the tutorial about how to connect with an existing table in the database.
Below is Student
class with the same attributes and properties as of the table.
public class Student
{
public int ID { get; set; }
public string LastName { get; set; }
public int Age { get; set; }
public string Address { get; set; }
}
The class StudentMap
is used to map with the existing table in the database.
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);
}
}
The SchoolContext
is the reference to the database. In this code, we are using the StudentMap
declared above for configuration.
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 });
}
}
Note : Set the different key in Student
and StudentMap
class so that the schoolcontext
could distinguish them correctly.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.