mary
mary

Reputation: 11

Can't create database with Entity Framework code-first

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>

Upvotes: 1

Views: 1413

Answers (1)

Hamid Pourjam
Hamid Pourjam

Reputation: 20744

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

Upvotes: 1

Related Questions