0

I'm trying to use entity framework with code first method, but for some reason it does not create a database for me. I suppose the problem is in my app.config configuration file. I never used entity framework before. I have read similar posts on stackoverflow, but however I cannot solve my problem. I do not get any errors during compilation, but when I open microsoft sql server I don't see my database created.

This is my app.config file. I found this code online, but I don't know what should I write inside Database attribute.

<configuration>
  <configSections>
        <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    </configSections>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
  </startup>
  <appSettings>
    <add key="ipadress" value="127.0.0.1" />
    <add key="port" value="14000" />
  </appSettings>

  <connectionStrings>
    <add name="AVLdataContext" connectionString="Server=.\SQLEXPRESS;Database=Blogging;Integrated Security=True;"  providerName="System.Data.SqlClient"/>
  </connectionStrings>
</configuration>

Also here is my model class:

namespace TCPserver
{
    public class DataBaseModel
    {
        public static string LoadToDatabase(byte[] arr, string imei)
        {
            return ReversedBinaryReader.LoadToDatabase(arr, imei);
        }
    }

    public class AVLdataContext : DbContext
    {
        public DbSet<Datas> Data { get; set; }
        public DbSet<GPSelement> GPSelement { get; set; }
        public DbSet<IOelement> IOelement { get; set; }
    }
}

Here is my controller:

using (var db = new AVLdataContext())
{
    db.Data.Add(data);
    db.GPSelement.Add(data.Gps);
    db.IOelement.Add(data.Io);
    db.SaveChanges();
}

What am I doing wrong? I'm completely green at this.

2
  • do you have multiple projects inside your solution? if so try adding that part to the web.config of your web app Commented Nov 30, 2017 at 12:30
  • It does not solve my problem.. Commented Nov 30, 2017 at 12:33

2 Answers 2

2

You need to update the AVLdataContext to include a constructor calls base() with the connection string

public class AVLdataContext : DbContext
{
    public AVLdataContext ()
        : base("AVLdataContext")
    {

    }

    public DbSet<Datas> Data { get; set; }
    public DbSet<GPSelement> GPSelement { get; set; }
    public DbSet<IOelement> IOelement { get; set; }
}

Without this, EF has no way of knowing where the database is.

Have a look at this as well.

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

1 Comment

Useful answer, but still same result. What else can I do?
0

Try to open your package manager Console and write folowing commands:

Enable-Migrations
Add-Migration initial
Update-Database

This will enable migrations for your db, add first migration and create db.

Comments

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.