Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

C# Entity Framework Basics Entity Framework and Databases Where's Our Data?

Frantyk Andrii
Frantyk Andrii
35,423 Points

Entity framework: where's our data?

Look my code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CarShowRoom.Models
{
    public class CarModel
    { 
        public int Id { get; set; } 
        public string CarModelName { get; set; }
        public string BodyType { get; set; }
    }
}

using CarShowRoom.Models;
using System.Data.Entity;

namespace CarShowRoom
{
    public class Context : DbContext
    { 
        public DbSet<CarModel> CarModels { get; set; }
    }
}

using CarShowRoom.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;

namespace CarShowRoom
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void dataGrid_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
        {
            e.Column.Width = new DataGridLength(1, DataGridLengthUnitType.Star);
        } 

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {

        }

        private void Car_Model_Click(object sender, RoutedEventArgs e)
        {
            using (var context = new Context())
            {
                context.CarModels.Add(new CarModel()
                {
                    CarModelName = "X5", 
                    BodyType = "BMW type"
                });
                context.CarModels.Add(new CarModel()
                {
                    CarModelName = "X6",
                    BodyType = "BMW another type"
                });
                context.SaveChanges();
                var carModels = context.CarModels.ToList();
                dataGrid.ItemsSource = carModels;
            } 

        }
    }
}

This all working good. But i don't see my database on sqlserver->localdb->Databases. Debug say that database was created on server but i don't see this.

1 Answer

James Churchill
STAFF
James Churchill
Treehouse Teacher

Frantyk,

How about setting a breakpoint in the Car_Model_Click method and looking at the value of the Context.Database.Connection.ConnectionString property value? That should provide a clue as to where the data is being stored.

The Context.Database.Connection property is of type DbConnection which provides information about the database connection that is being used by the context.

https://msdn.microsoft.com/en-us/library/system.data.common.dbconnection(v=vs.113).aspx

Thanks ~James