Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the location of database if doing Code First using SqlServer CE 4.0?

I am learning Code First development with a console application. In many of the examples, including Scott Gu's post, they suggest using Sql Server CE 4.0 and set it up so that the code creates the database automagically.

I am doing a console app and would like to examine the database with a management tool, such as management studio or linqpad. What is the location of the sdf file that is created?

I created an App.Config and placed the following in it:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <connectionStrings>
    <add
      name="NerdDinners"
      providerName="System.Data.SqlServerCe.4.0"
      connectionString="Data Source =|DataDirectory|NerdDinners.sdf"/>
  </connectionStrings>
</configuration>

However, NerdDinner is not created, and instead a database in SQLExpress is created. I seem to have confused some things here.

like image 284
MedicineMan Avatar asked Mar 20 '26 07:03

MedicineMan


1 Answers

It depends upon the connection string you have set in app.config file. In most cases (DataDirectory) database file will be created at Debug\bin folder (winform app).

Sample app: (Add the ref of System.ComponentModel.DataAnnotations.dll and EntityFramework.dll)

public class Employee
{
    [Key]
    public int EmpNo { get; set; }
    public string Name { get; set; }
}
public class Test : System.Data.Entity.DbContext
{
    public System.Data.Entity.DbSet<Employee> Emps { get; set; }
}

app.config (The name of connection string must match the name of Context class)

<configuration>
<connectionStrings>
  <add  name="Test"
          connectionString="Data source=|DataDirectory|Test.sdf" 
          providerName="System.Data.SqlServerCe.4.0"
        />
</connectionStrings>
</configuration>

Add a record,

Test db = new Test();

db.Emps.Add(new Employee() { EmpNo = 1, Name = "Mr.X" });
db.SaveChanges();
like image 90
KV Prajapati Avatar answered Mar 22 '26 20:03

KV Prajapati



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!