2

I am using MVC3 with Code first approach. In this case, I had to generate my entity classes from the existing databse.

The database was

Database1.mdf

Once I did that, it created DBEntities and added a new connectionstring in my Web.config which looked something like this:

<add name="DATABASE1Entities" connectionString="metadata=res://*/Models.Task.csdl|res://*/Models.Task.ssdl|res://*/Models.Task.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=.\SQLEXPRESS;attachdbfilename=|DataDirectory|\DATABASE1.MDF;integrated security=True;user instance=True;multipleactiveresultsets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />

Now I removed DB Entities and came up with my own DB Context class.

and

Now, I am working with the following connectionstring:

<add name="ApplicationServices" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|DATABASE1.mdf;User Instance=true" providerName="System.Data.SqlClient" />

Name of my DBcontext class is TaskContext.

I am not sure what happeend afer this. My code works. But it works on some blank database and it does not reflect anydata in database.mdf. If I add something using my controller then I see that thing is added. But it does not get reflected in Databse1.mdf.

It seeems to have created a its own databse. But I do not see andy .sdf or .mdf file created anywhere.... I am not sure what is going on?

2
  • Hi I just noticed that EDMX file has something like database1model.store. I suspect, if it has anything to do with my problem? Commented Sep 10, 2011 at 19:40
  • So, you mentioned that your code works. Are you saying that if you add something via your application, it gets added to the DB and you can later retrieve this info from the DB? Commented Sep 10, 2011 at 21:40

2 Answers 2

2

Make the name of your connection string same as TaskContext:

<add name="TaskContext" 
    connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|DATABASE1.mdf;User Instance=true" 
    providerName="System.Data.SqlClient" />

OR, you can use your favorite connectionStringName by following these steps:

1- When you are creating a TaskContext object, changes it's connectionString:

var cn = WebConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString;
// or
var cn = WebConfigurationManager.ConnectionStrings[0].ConnectionString; // use your connection index instead of 0
var _context = new TaskContext();
_context.Database.Connection.ConnectionString = cn;

2- in your context class (TaskContext), you should tell to modelBuilder to remove IncludeMetadataConvention by this code:

protected override void OnModelCreating(DbModelBuilder modelBuilder) {
    modelBuilder.Conventions.Remove<IncludeMetadataConvention>();
    base.OnModelCreating(modelBuilder);
}
Sign up to request clarification or add additional context in comments.

Comments

2

Try making the name of your connection string and DBContext the same. I believe reading at some point that EF Code First employs a lot of conventions so if it doesn't find a connection string named as the DB Context (in your case "TaskContext") it will try to connect to a SQLExpress (whether or not there is one installed) and will try to find or create the database.

I'm assuming you've got SQLExpress installed. What's down in C:\Program Files\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\DATA\? (I think that's the path) Have you got a DB sitting there for this project? Maybe something like [YourNamespace].TaskContext.dbf?

1 Comment

You are right, it myseriously added that .MDf file at the location that you mentioned. I had to recreate the project. now, it works.

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.