0

我在這裏看到了幾個問題,但我找不到解決方案。配置VS localDB和EF部署

我有一個winform應用程序VS2013與EF運行一個LocalDB。這個LocalDB是從模型創建的。

我應該怎麼做才能在客戶端上使用LocalDB安裝此應用程序?

我是否需要向解決方案添加MDF文件? 要做什麼?

有類似的問題:localDB DeploymentInstaller

+0

什麼是你計劃使用部署?點擊一次?讓你自己的引導程序鏈MSIS?如果用戶要從空數據庫開始,則不需要包含mdf,因爲如果您設置了連接字符串和正確的初始化程序,EF可以爲您創建一個。 – jjj

回答

2
  1. 你可能已經安裝了 「的LocalDB」。 您的客戶需要一個安裝程序。

http://www.microsoft.com/en-us/download/details.aspx?id=42299

  • 您需要填充種子數據。看到這個帖子:
  • Recreate and Reseed LocalDb Before Each Unit Test

    public class MyDatabaseInitializer :  System.Data.Entity.DropCreateDatabaseAlways<MyDbContext> 
    { 
        protected override void Seed(MyDbContext context) 
        { 
         // Add entities to database. 
    
         context.SaveChanges(); 
        } 
    } 
    
  • 您的客戶端應用程序將需要正確的連接字符串。
  • https://www.connectionstrings.com/sqlconnection/localdb-automatic-instance-with-specific-data-file/

    Server=(localdb)\v11.0;Integrated Security=true; 
    AttachDbFileName=C:\MyFolder\MyData.mdf; 
    

    但隨着 「種子數據」 overload..it應該能夠動態地創建它。

    這裏是做一個例子,只是我相信:

    https://code.msdn.microsoft.com/ASPNET-MVC-Application-b01a9fe8

    namespace ContosoUniversity.DAL 
    { 
        public class SchoolInitializer : System.Data.Entity.DropCreateDatabaseIfModelChanges<SchoolContext> 
        { 
         protected override void Seed(SchoolContext context) 
         { 
          var students = new List<Student> 
          { 
          new Student{FirstMidName="Carson",LastName="Alexander",EnrollmentDate=DateTime.Parse("2005-09-01")}, 
          new Student{FirstMidName="Meredith",LastName="Alonso",EnrollmentDate=DateTime.Parse("2002-09-01")}, 
          new Student{FirstMidName="Arturo",LastName="Anand",EnrollmentDate=DateTime.Parse("2003-09-01")}, 
          new Student{FirstMidName="Gytis",LastName="Barzdukas",EnrollmentDate=DateTime.Parse("2002-09-01")}, 
          new Student{FirstMidName="Yan",LastName="Li",EnrollmentDate=DateTime.Parse("2002-09-01")}, 
          new Student{FirstMidName="Peggy",LastName="Justice",EnrollmentDate=DateTime.Parse("2001-09-01")}, 
          new Student{FirstMidName="Laura",LastName="Norman",EnrollmentDate=DateTime.Parse("2003-09-01")}, 
          new Student{FirstMidName="Nino",LastName="Olivetto",EnrollmentDate=DateTime.Parse("2005-09-01")} 
          }; 
    
          students.ForEach(s => context.Students.Add(s)); 
          context.SaveChanges(); 
          var courses = new List<Course> 
          { 
          new Course{CourseID=1050,Title="Chemistry",Credits=3,}, 
          new Course{CourseID=4022,Title="Microeconomics",Credits=3,}, 
          new Course{CourseID=4041,Title="Macroeconomics",Credits=3,}, 
          new Course{CourseID=1045,Title="Calculus",Credits=4,}, 
          new Course{CourseID=3141,Title="Trigonometry",Credits=4,}, 
          new Course{CourseID=2021,Title="Composition",Credits=3,}, 
          new Course{CourseID=2042,Title="Literature",Credits=4,} 
          }; 
          courses.ForEach(s => context.Courses.Add(s)); 
          context.SaveChanges(); 
          var enrollments = new List<Enrollment> 
          { 
          new Enrollment{StudentID=1,CourseID=1050,Grade=Grade.A}, 
          new Enrollment{StudentID=1,CourseID=4022,Grade=Grade.C}, 
          new Enrollment{StudentID=1,CourseID=4041,Grade=Grade.B}, 
          new Enrollment{StudentID=2,CourseID=1045,Grade=Grade.B}, 
          new Enrollment{StudentID=2,CourseID=3141,Grade=Grade.F}, 
          new Enrollment{StudentID=2,CourseID=2021,Grade=Grade.F}, 
          new Enrollment{StudentID=3,CourseID=1050}, 
          new Enrollment{StudentID=4,CourseID=1050,}, 
          new Enrollment{StudentID=4,CourseID=4022,Grade=Grade.F}, 
          new Enrollment{StudentID=5,CourseID=4041,Grade=Grade.C}, 
          new Enrollment{StudentID=6,CourseID=1045}, 
          new Enrollment{StudentID=7,CourseID=3141,Grade=Grade.A}, 
          }; 
          enrollments.ForEach(s => context.Enrollments.Add(s)); 
          context.SaveChanges(); 
         } 
        } 
    } 
    
    +0

    我會嘗試這些步驟,看起來沒錯。謝謝。 – grteibo

    +0

    在您的開發機器上搜索「SQLLOCALDB.MSI」使用此工具(http://www.voidtools.com/downloads/)快速找到它。 MSI可能是你所需要的。 – granadaCoder