1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

creating database with "code first" in Entity Framework

Discussion in 'C#' started by johnyjj2, Apr 14, 2013.

  1. #1
    Hello,
    SEMrush
    I would like to create database with "code first" approach in Entity Framework. However, even if I don't get any exceptions, database is not created. What may be the reason for that?

    app.config

    <connectionStrings>
        <add
        name="CodeFirstDatabase"
        providerName="System.Data.SqlClient"
        connectionString="Server=.\SQLEXPRESS;Database=MT4Analyzer;Trusted_Connection=true;"/>
    </connectionStrings>
    Code (markup):
    Database.cs

    public class EntityData : DbContext
    {
        // DbContext required: C:\Program Files (x86)\Microsoft Web Tools\Packages\EntityFramework.5.0.0\lib\net40\EntityFramework.dll
        public List<EntityStringColumn> EntityStringColumns { get; set; }
        public List<EntityDoubleColumn> EntityDoubleColumns { get; set; }
        public EntityData()
        {
            EntityStringColumns = new List<EntityStringColumn>();
            EntityDoubleColumns = new List<EntityDoubleColumn>();
        }
    }
    Code (markup):
    TheOtherFile.cs

    // Here currentDBRow has all values that I want to add to DB.
     
    using (var db = currentDBRow)
    {
        var result = db.SaveChanges();
    }
    Code (markup):
    Variable 'result' has value 0 and database is not created. I can log to SQL Server with Management Studio as current user or as "sa" (however "sa" requires password) because "Mixed Mode" is enabled.

    Regards!
     
    johnyjj2, Apr 14, 2013 IP
    SEMrush
  2. wren11

    wren11 Active Member

    Messages:
    89
    Likes Received:
    10
    Best Answers:
    3
    Trophy Points:
    53
    #2
    if your using a SQLClient Data Context provider, You'll need to explicitly create the database from the context constructor somewhere
     
    wren11, Apr 25, 2013 IP
  3. annaharris

    annaharris Active Member

    Messages:
    119
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    51
    #3
    Wren11 is absolutely right as it is very important to create your database explicitly if you are using SQLClient data context provider.
     
    annaharris, May 6, 2013 IP