0

I'm trying to use Entity Framework 5 Code First. I've created model classes and context.

After that following Microsoft instructions I've enabled code migrations.

Than I skipped running my application (during which DB should be created first time) and use Add-Migration command. Migration was generated successfully.

Update-Database call was also successfull. But I'm not seeing my DB at all. It is absent!

SQL Management Studio and Visual Studio Server Explorer show only 4 default system databases and that's all! I also tried to launch my application - it doesn't change anything. I'm using

public MyContext() : base("name=MyContext") { }

such type of constructor (so I need specify connection string with MyContext name. Here is my app.config example:

<configuration>
    <configSections>
      <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
      <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    </configSections>
    <entityFramework>
      <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
        <parameters>
          <parameter value="v11.0" />
        </parameters>
      </defaultConnectionFactory>
    </entityFramework>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
    <connectionStrings>
      <add name="BettyContext" providerName="System.Data.SqlServerCe.4.0" connectionString="Data Source=MyDB.sdf"/>
    </connectionStrings>
</configuration>

I have no idea what's going on. Seems that no one has such problem. May be I've missed some evident thing, but howbeit I'm waiting for some help.

9
  • 1
    Do you see an exception? If you don't see an exception you probably are not looking at the right database. You can try adding some data to the database with your app and then read it. If the data can be successfully added and read and you still don't see the database it would confirm that you are looking at wrong database. Commented Oct 9, 2012 at 23:07
  • I've written unit test which writes 1 entity to DB and than reads it. It seems everything is OK. I'm feeling stupid now, because I still can't understand where actually is my DB??? Commented Oct 10, 2012 at 19:14
  • (This might be brute force but it should be relatively fast and easy.) I would just run dir C:\MyDB.sdf /s in an elevated command prompt (probably on all your partitions) and it should find all MyDB.sdf files on your computer. Then it should be easy to find out which one is being used. You can also provide an absolute path in your config file (e.g. Data Source=C:\temp\MyDB.sdf) and then you should have no doubts. Commented Oct 10, 2012 at 20:00
  • Thanks. I've found it. But location is strange. It is stored here: "C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE" and in the Bin directory of unit test project which I used to test write\read from DB. And also I can't open it neigher through Visual Studio Server Explorer nor SQL Management Studio. How can I access this DB and why there are two instances? Commented Oct 10, 2012 at 21:00
  • I don't know why you have two instances maybe one is created when you run your tests and the other one when you run your app? I think it's recommended to use an absolute path for data source and this way you can avoid the confusion. You may not have permissions to access this database - try running Sql Server Management Studio as administrator to see if this is the case. Commented Oct 10, 2012 at 21:13

1 Answer 1

1

Do you see an exception? If you don't see an exception you probably are not looking at the right database. You can try adding some data to the database with your app and then read it. If the data can be successfully added and read and you still don't see the database it would confirm that you are looking at wrong database.

Sign up to request clarification or add additional context in comments.

2 Comments

That is the first think I also thought but do you mean looking at wrong database or server?
Either. If you have a connection string that points to a different server and you think your app is using it while it actually cannot find the connection string and uses the default setting (which will be a local sqlexpress or localdb) then it is a different server. If you have both localdb and SqlExpress installed on your box than you may be looking at localdb while the app is using SqlExpress so you have the same machine but different databases. If you rename the namespace your DbContext lives and don't use specific database name CodeFirst will create a new db and you may be looking at old

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.