问题
I use Visual Studio 2013 and need to use Entity Framework code first approach in a project. I used NuGet Manager to install EF v6.1 into my project. But I can't create an empty code first model or even a code first model from database although there are two other choices (DB first and Model first).
How I can add Entity Framework code first feature to my project?
回答1:
Here is a beginners tutorial for using entity framework code first: An Absolute Beginner's Tutorial for understanding Entity Framework's Code First Approach in ASP.NET MVC
回答2:
Install EntityFramework In Nuget
Add connectionString In Web.Config And YourConnection In The Name Of ConnectDB
Add One Folder In The Name Of db
In db Add Class In The Name Of Tbl_Student
public class Tbl_Student { [Key] public int Id { get; set; } [Required(AllowEmptyStrings =false)] [StringLength(maximumLength:40)] public string Name { get; set; } [Required(AllowEmptyStrings = false)] [StringLength(maximumLength: 40)] public string Family { get; set; } }
Out Of db Create One Class In The Name Of Context
using System.Data.Entity;
Edit Context To This
public class DataContext:DbContext { public DataContext() : base("ConnectDB") { } protected override void OnModelCreating(DbModelBuilder modelBuilder) { } public virtual DbSet<Tbl_Student> Tbl_Students { get; set; } }
To Package Maneger Console Write Enable-Migrations
And Write Add-Migration mig1 since mig1 is desired
After Write Update-DataBase
来源:https://stackoverflow.com/questions/23657655/how-to-apply-entity-framework-code-first-approach-to-a-project