-2

I am creating an application in WPF, and I want connect to a database using SQL Server Compact 4.0.

I add data base to project. And using NuGet I added

  • System.Data.SqlServerCe
  • System.Data.SqlServerCe.Entity

In my app.config a connection string was added:

 <connectionStrings>
    <add name="Biblionerzy.Properties.Settings.Database1ConnectionString"
      connectionString="Data Source=|DataDirectory|\Database1.sdf"
      providerName="Microsoft.SqlServerCe.Client.4.0" />
  </connectionStrings>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlCeConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="System.Data.SqlServerCe.4.0" />
      </parameters>
    </defaultConnectionFactory>
  </entityFramework>

I created a simple class:

namespace Biblionerzy
{
   public class model
    {
        [Key] 
        public int Id { get; set; }
        public string Pytanie { get; set; }
        public string A { get; set; }
        public string B { get; set; }
        public string C { get; set; }
        public string D { get; set; }
        public string Prawidlowa_odp { get; set; }
        public int Stawka { get; set; } 
    }

   public class PytaniaModel : DbContext
   {
       public DbSet<model> Modele { get; set; }
   }
}

And now, how can I add anything into database?

user1031034
  • 836
  • 1
  • 14
  • 38
  • add stuff to Modele, and call SaveChanges on DbContext? – bas Jan 26 '13 at 21:28
  • 1
    Is this specific to WPF and/or SqlCe? I believe these tags are somehow misleading. – Wiktor Zychla Jan 26 '13 at 21:35
  • @Haxx I get value from textBox `var pytanie = tb_pytanie.Text;` `var A = tb_odpA.Text;` After that I make: `PytaniaModel dane = new PytaniaModel();` And I don't now what to do after. – user1031034 Jan 26 '13 at 21:51
  • http://msdn.microsoft.com/pl-pl/library/ff453894.aspx – Wiktor Zychla Jan 26 '13 at 22:04
  • I made: `dane.Modele.Add(new model { Pytanie = pytanie, A = A, B = B, C = C, D = D, Prawidlowa_odp = "A", Stawka = 100 });` but I get an error: `tSystem.Data.Entity.Edm.EdmEntityType: : EntityType 'model' has no key defined. Define the key for this EntityType.` – user1031034 Jan 26 '13 at 22:13
  • http://stackoverflow.com/questions/5011145/entity-framework-code-first-entitytype-has-no-key-define (add the [Key] attribute might help?) – bas Jan 26 '13 at 22:25
  • I added Key attribute, but still doesn't work... I get an error `The model backing the 'PytaniaModel' context has changed since the database was created. Consider using Code First Migrations to update the database (http://go.microsoft.com/fwlink/?LinkId=238269).` – user1031034 Jan 26 '13 at 22:45

1 Answers1

1

I think you are just trying to get started with EF code first (if not, i'll remove the answer).

by the looks of your question you got everything setup.

You might need to add a [Key] attribute, I can't understand what is below so I just added a property Id which I gave the [Key] attribute.

   public class model
    {
        [Key]
        public int Id {get;set;}
        public string Pytanie { get; set; }
        public string A { get; set; }
        public string B { get; set; }
        public string C { get; set; }
        public string D { get; set; }
        public string Prawidlowa_odp { get; set; }
        public int Stawka { get; set; } 
    }

   public class PytaniaModel : DbContext
   {
       public DbSet<model> Modele { get; set; }
   }

Just instantiate an object of the class and use it...

   var dbContext = new PytaniaModel();
   dbContext.Modele.Add(new Model());
   dbContext.SaveChanges();

And that's it, check your database and verify that the database is created and you stored your first row in the table.

bas
  • 13,550
  • 20
  • 69
  • 146
  • I have database and I don't want use code first. Some minutes ago I try do: `dane.Modele.Add(new model { Pytanie = pytanie, A = A, B = B, C = C, D = D, Prawidlowa_odp = "A", Stawka = 100 });` and I got na error: `tSystem.Data.Entity.Edm.EdmEntityType: : EntityType 'model' has no key defined. Define the key for this EntityType.` – user1031034 Jan 26 '13 at 22:16
  • I add kay attribute, but I get another error: `The model backing the 'PytaniaModel' context has changed since the database was created. Consider using Code First Migrations to update the database (http://go.microsoft.com/fwlink/?LinkId=238269).` – user1031034 Jan 26 '13 at 22:44
  • 1
    yeah, that makes sense. maybe it's time for you to follow a tutorial on EF. you made a change to your database (btw, you are using code first if you want to do this). so you need to enable migrations, add a migration, and update the database. – bas Jan 26 '13 at 22:51