0
votes

i created an ASP.NET MVC 4 project, everything is by default, how can i write some config and/or c# codes for creating my database using EF5-Code-First?

i find these three nodes in web.config(under configuration root node)

<entityFramework>
  <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
    <parameters>
      <parameter value="v11.0" />
    </parameters>
  </defaultConnectionFactory>
</entityFramework>

config sections:

<configSections>
 <!--Comment-->
   <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>

connectionStrings node:

  <connectionStrings>
    <add name="DefaultConnection" connectionString="Data Source=.\W2012STATIOIN;Initial Catalog=TeamMgmt;Integrated Security=SSPI; User ID=admin; Password = 123 " />
  </connectionStrings>
1
Create a migration. pm> Enable-Migrations pm> Add-Migration Init - Brad Christie
Since you are using Code First, the database should be created automatically based on your entity classes. I think you are not asking the right question, either way, you wouldn't define/customize your database in your config file, you have to do it via models, fluent api, migrations, etc. - SOfanatic
@SOfanatic - strictly speaking the database will only be created when an attempt to access the database is first run. - Greg
@SOfanatic exactly! it works fine now! and i just know a little bit of fluent api(seems like using this to map model to DB table right? or other things i dunt no yet). but why should migrations be used? - paul cheung

1 Answers

1
votes

It sounds like you are using the standard MVC4 template. If you try and register as a user, a database will be created for you.