我在網上看到很多.netCore的信息,就動手自己寫一個例子測試哈,但是想不到其中這么多坑;
1.首先.netCore和EF的安裝就不用多說了,網上有很多的講解可以跟着一步一步的下載和安裝,但是需要注意點就安裝Microsoft.EntityFrameworkCore.SqlServer程序包(Install-Package Microsoft.EntityFrameworkCore.Sqlite –Pre);
2.創建實體
/// <summary> /// 學生類 /// </summary> public class Student { /// <summary> /// ID /// </summary> [Key] public Guid ID { get; set; } = Guid.NewGuid(); /// <summary> /// 名字 /// </summary> [StringLength(50)] [Required] public string Name { get; set; } /// <summary> /// 年齡 /// </summary> public int Age { get; set; } /// <summary> /// 性別 /// </summary> public EmSex Sex { get; set; } = EmSex.未填; } public enum EmSex { 男 = 0, 女 = 1, 未填 = 2 }
3.創建EF的上下文DbContext
public class DbContextHelper : DbContext { public DbSet<Student> StudentEntity { get; set; } public DbContextHelper() { } public DbContextHelper(DbContextOptions options) : base(options) { } //protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) //{ // string str = "data source=.; Initial Catalog=NetCore_TestDB ; uid=sa; pwd=qwertyuiop"; // optionsBuilder.UseSqlServer(str); // //optionsBuilder.UseSqlite(str); //} }
4.編寫數據庫操作的接口和實現類
public interface IStudentService { Task<int> AddStudnet(Student entity); Task<int> DeltStudent(Guid id); List<Student> GetStudent(Expression<Func<Student,bool>> fun); } public class StudentService : IStudentService { public async Task<int> AddStudnet(Student entity) { using (DbContextHelper dbHelper =new DbContextHelper () ) { await dbHelper.AddAsync(entity); return dbHelper.SaveChanges(); } } public async Task<int> DeltStudent(Guid id) { using (DbContextHelper dbHelper =new DbHelper.DbContextHelper ()) { var entity =await dbHelper.StudentEntity.FindAsync(id); if (entity == null) { return -1; } else { dbHelper.StudentEntity.Remove(entity); return dbHelper.SaveChanges(); } } } public List<Student> GetStudent(Expression<Func<Student, bool>> fun) { using (DbContextHelper dbHelper =new DbHelper.DbContextHelper ()) { var List = dbHelper.StudentEntity.Where(fun).ToList(); return List; } } }
5.創建Controller進行調用
public class HomeController : Controller { private IStudentService StudentDb = new StudentService(); // GET: /<controller>/ public IActionResult Index() { var list = StudentDb.GetStudent(it=>true); return View(); } }
6.下面配置數據庫鏈接和EF的初始化創建
public void ConfigureServices(IServiceCollection services) { // Add framework services. services.AddApplicationInsightsTelemetry(Configuration); //services.AddEntityFrameworkSqlServer() // .AddDbContext<DbContextHelper>(); //options => // options.UseSqlServer(Configuration["AppSettings:DefaultConnectionStr"]) services.AddDbContext<DbContextHelper>(options => options.UseSqlite(Configuration["AppSettings:DefaultConnectionStr"])); services.AddMvc(); services.AddTransient<IStudentService, StudentService>(); }
"AppSettings": { "DefaultConnectionStr": "data source=.; Initial Catalog=NetCore_TestDB ; uid=sa; pwd=qwertyuiop" },
上面很多網上都有相關的資料可以查詢,我這里只是梳理了哈,下面開始運行
開始報這個錯誤

后面經過查看很多資料,曉得是上下文的注冊造成的,這個問題就需要注意幾點:
1.獲取字符串一定要注意不要不能包含空格符號
services.AddDbContext<DbContextHelper>(options => options.UseSqlite(Configuration["AppSettings:DefaultConnectionStr"]));
2.一定給要注意區分哈UseSqlite和UseSqlServer;
修改之后運行,還是會有上面的問題,具體原因我也不是很清楚,但是我找寧外一種解決方案進行初始化數據庫鏈接字符串,重寫OnConfiguring
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { string str = "data source=.; Initial Catalog=NetCore_TestDB ; uid=sa; pwd=qwertyuiop"; optionsBuilder.UseSqlServer(str); }
修改之后在運行,問題得到了解決。
需要源碼留下郵箱我看到后發,一起討論!
