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="data source=.\SQLEXPRESS;attachdbfilename=|DataDirectory|\DATABASE1.MDF;integrated security=True;user instance=True;multipleactiveresultsets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
Now I remove开发者_StackOverflowd 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?
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);
}
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?
精彩评论