1

I try to create a database in SQL Server with Entity Framework code-first in a Windows form application. The program runs with no error, but I can't see any database in my SQL Server. Why?

I created 2 classes

public class Country :System.Object
{
    public int Id { get; set; }
    public string Name { get; set; }

    public Country()
    {

    }

    public Country(int id, string name)
    {
            this.Id = id;
            this.Name = name;
    }
}

class DataBaseContext : System.Data.Entity.DbContext
{
    public DataBaseContext()
        : base("connectionStringName")
    {

    }

    public System.Data.Entity.DbSet<Country> countries { get; set; }
}

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        Models.DataBaseContext ODB = null;

        try
        {
            ODB = new Models.DataBaseContext();
            Models.Country OCountries = new Models.Country();
            OCountries.Id = 1;
            OCountries.Name="AA";
            ODB.countries.Add(OCountries);
            ODB.SaveChanges();
        }
        catch (Exception ex)
        {
            System.Windows.Forms.MessageBox.Show(ex.Message);
        }
        finally   
        {
            if (ODB != null)
            {
                 ODB.Dispose();
                 ODB = null;
            }
        }
    }
}

and app.config is:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>

<section name="entityFramework"             type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<startup>
 <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
 <parameters>
 <parameter value="mssqllocaldb" />
 </parameters>
  </defaultConnectionFactory>
  <providers>
  <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
  <providers>

 </entityFramework>
</configuration>
1
  • Do you get any exceptions when you run the code? Commented Nov 14, 2015 at 20:40

1 Answer 1

1

Base on your connection string the database will be created in localdb.

You can open Sql Server Management Studio and change "Server name" to (localdb)\mssqllocaldb and "Authentication" to "Windows Authentication" and login to your local db instance installed on your system. There you can find the database created by Entity Framework.

Also keep in mind that .NET has unified object model which means you don't need to derived a class from it so change public class Country :System.Object to public class Country

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

2 Comments

thanks doctor , I try But any Database dosen't create in sql. I entered with (localDB)\v11.0 too but i gave same result
Do you get any exception?

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.