I want to be able to generate a table from a class object from a short-code. Is there any available extension in Microsoft Visual Studio 2017 that does that?
1 Answer
It's more than a simple extension, but the Code First workflow of Entity Framework does this. Here is a tutorial on this topic.
The code contains three sample classes, e.g. Student
:
public class Student
{
public int ID { get; set; }
public string LastName { get; set; }
public string FirstMidName { get; set; }
public DateTime EnrollmentDate { get; set; }
public virtual ICollection<Enrollment> Enrollments { get; set; }
}
Later on, the tutorial mentions a database initializer:
public class SchoolInitializer : System.Data.Entity.DropCreateDatabaseIfModelChanges<SchoolContext>
which will create the database when you use the application for the first time:
When you ran the Students page and the application tried to access the database, EF discovered that there was no database and created one.
This particular database initializer is most useful when creating a database from scratch, but the are migration options to add columns or tables to existing databases. All in all, the framework is pretty powerful; it supports most things you'll be looking for in a database like foreign keys and indices.